LoginSignup
24
17

More than 3 years have passed since last update.

AWS SAMの環境変数設定方法

Last updated at Posted at 2019-11-10

AWS SAM とは

https://docs.aws.amazon.com/ja_jp/serverless-application-model/latest/developerguide/what-is-sam.html

AWS SAM でGoogle先生に聞けば、色んな回答が出ます。

Background

Lambda のローカルテストよく使われるツールで、template.ymlLambdaAPI Gateway の設定を書いて、AWS SAM CLI から実行し、テストします。

Example

template.yml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Runtime: nodejs10.x
    Handler: index.handler
    Timeout: 10
    MemorySize: 1024
    Environment:
      Variables:
        DYNAMO_ENDPOINT: DUMMY_ENDPOINT

Resources:
  A001:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: ../build/A001/
      Events:
        HelloWorld:
          Type: Api
          Properties:
            Path: /groups
            Method: GET
      Environment:
        Variables:
          TABLE_GROUP_WORDS: DUMMY_GROUP_WORDS

Goal

DUMMY_ENDPOINTDUMMY_GROUP_WORDS を置き換えます。

Start Command

sam local start-api -t template.yml --env-vars vars.json

Implement

GlobalsResources sectionの書き方が違います。

vars.json
{
  "Parameters": { ## Globals Section
    "DYNAMO_ENDPOINT": "http://host.docker.internal:4569"
  },
  "A001": {  ## Resources Section
    "DUMMY_GROUP_WORDS": "T_GROUP_WORDS"
  }
}

Summay

GlobalsParameters で設定し、Resources は実際の Resource 名となります。

24
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
17