Serverless Framework の設定値の上書き・追加 方法

Serverless Framework での上書き設定について詰まった点をまとめました。

基本的には以下公式ドキュメントを参考にしつつ、いくつか実装パターンを試験しました。

環境変数を例にとって設定してみます。

Environment Variables

stage 共通で設定

1
2
3
provider:
environment:
key: 'hello,world'

上記設定の場合、環境変数は以下の様に設定されます。

  • key: hello,world

1つの値を上書きする場合

参考: Overwriting Variables

1
2
3
4
5
6
7
8
9
10
11
provider:
name: openwhisk
stage: dev

custom:
myStage: ${opt:stage, self:provider.stage}

functions:
trigger:
environment:
key: ${self:custom.myStage}

上記設定の場合、デプロイ後の環境変数の設定は以下の様になります。

  • sls deploy でデプロイした場合
    • key: dev
  • sls deploy --stage hoge でデプロイした場合
    • key: hoge

stage 毎に envorinment を追加・上書き

デフォルトの設定を provider.environment で指定し、各 stage 毎の設定は self:custom.${self:provider.stage}.environment で追加・上書きできる様にしています。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
provider:
environment:
project: ${self:custom.${self:provider.stage}.project}
slackChannel: ${self:custom.${self:provider.stage}.slack.channel}
key: 'hello,world'

custom:
hoge:
project: hoge
slack:
channel: '#hoge'
moge:
project: moge
slack:
channel: '#moge'
environment:
key: 'moge-key'
key2: 'moge-key2'

functions:
trigger:
environment: ${self:custom.${self:provider.stage}.environment, self:provider.environment}

上記設定の場合、デプロイ後の環境変数の設定は以下の様になります。

  • sls deploy --stage hoge でデプロイした場合
    • project: hogeproject
    • slackChannel: #hoge
    • key: hello,world
  • sls deploy --stage moge でデプロイした場合
    • project: hogeproject
    • slackChannel: #hoge
    • key: moge-key <-- 更新
    • key2: moge-key2 <-- 追加

functions.trigger.environment を上書き設定する理由は、以下の様にした場合、

1
2
3
4
functions:
trigger:
# environment: ${self:custom.${self:provider.stage}.environment, self:provider.environment}
environment: ${self:custom.${self:provider.stage}.environment}

self:custom.hoge.environment の設定がなく、 Warning が発生する為です。

1
2
3
Serverless Warning --------------------------------------

A valid service attribute to satisfy the declaration 'self:custom.hoge.environment' could not be found.

以上
参考になれば幸いです。

Serverless Framework の設定値の上書き・追加 方法

https://kenzo0107.github.io/2020/03/23/2020-03-24-serverlessfw-environment/

Author

Kenzo Tanaka

Posted on

2020-03-24

Updated on

2020-05-07

Licensed under

コメント