0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

SAM CLI 環境変数読み込み方法 メモ

Posted at
  • SAM CLI利用時に定義した環境変数を読み込む方法をメモする

準備

  • env.json

    • 環境変数ファイル
    • template.yamlに合わせてGlobalsResourcesはセクションを分けて記述する
    {
        "Parameters": {
            "MYSQL_HOST":"db",
            "MYSQL_DATABASE":"test_db",
            "MYSQL_PORT":3308,
            "MYSQL_USER":"mysqluser",
            "MYSQL_PASSWORD":"mysqlpass"
        },
        "HelloWorldFunction":{
            "MYSQL_TABLE":"test_table"
        }
        
    }
    
  • template.yaml

    • Environmentセクションにenv.jsonに対応する変数名を定義する
    Globals:
      Function:
        Timeout: 3
        Environment:
          Variables:
            MYSQL_HOST: MYSQL_HOST
            MYSQL_DATABASE: MYSQL_DATABASE
            MYSQL_PORT: MYSQL_PORT
            MYSQL_USER: MYSQL_USER
            MYSQL_PASSWORD: MYSQL_PASSWORD
    ...
    Resources:
      HelloWorldFunction:
        Type: AWS::Serverless::Function
        Properties:
          CodeUri: hello_world/
          Handler: app.lambda_handler
          Runtime: python3.9
          Architectures:
            - x86_64
          Events:
            HelloWorld:
              Type: Api
              Properties:
                Path: /hello
                Method: get
          Environment:
          	variables:
          	 MYSQL_TABLE: MYSQL_TABLE
    

環境変数読み込み(実行)

  • 下記のコマンドを実行する

    sam local start-api --env-vars env.json
    

    --env-vars {環境変数ファイル}で指定する

参考情報

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?