14
4

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 3 years have passed since last update.

Serverless Frameworkで環境変数を扱いたい

Last updated at Posted at 2021-05-11

背景

Serverless Frameworkを使っていて秘匿情報どうしようって悩んだときに、公式でサポートされていたのでその設定方法を書く。

環境

Serverless Framework 2.41.0

設定方法

環境変数用のファイルを用意

  • .env.env.devなど
  • .gitignoreに追記するのも忘れずに
AWS_ACCOUNT_ID=xxxxxxxxxxxx
MY_SECRET_KEY=xxxxx

serverless.ymlでの設定

useDotenv: true

...

iam:
  role:
    statements:
      - Effect: "Allow"
        Action:
          - dynamodb:Query
          - dynamodb:Scan
        Resource: !Join
          - ":"
          - - "arn:aws:dynamodb:ap-northeast-1"
            - ${env:AWS_ACCOUNT_ID}
            - "table/myTable"

...

functions:
  my-function:
    handler: src/my_function.lambda_handler
    # Lambdaの環境変数に渡したい場合
    environment:
      MY_SECRET_KEY: ${env:MY_SECRET_KEY}
  • useDotenv: trueを指定して
  • ${env:変数名}で使うことができる

バージョン3.0.0以降はデフォルトになり、useDotenv: trueの指定をする必要がなくなるらしい

Links

14
4
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
14
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?