How to Override and Add Configuration Values in the Serverless Framework
This is a summary of the points I got stuck on when overriding settings in the Serverless Framework.
Basically, I referred to the official documentation below while testing a few implementation patterns.
Let’s try configuring things using environment variables as an example.
Common Setting Across Stages
1 | provider: |
With the configuration above, the environment variable is set as follows.
- key: hello,world
Overriding a Single Value
Reference: Overwriting Variables
1 | provider: |
With the configuration above, the environment variable settings after deployment are as follows.
- When deployed with
sls deploy- key: dev
- When deployed with
sls deploy --stage hoge- key: hoge
Adding and Overriding the environment per Stage
The default settings are specified in provider.environment, and the per-stage settings can be added or overridden via self:custom.${self:provider.stage}.environment.
1 | provider: |
With the configuration above, the environment variable settings after deployment are as follows.
- When deployed with
sls deploy --stage hoge- project: hogeproject
- slackChannel: #hoge
- key: hello,world
- When deployed with
sls deploy --stage moge- project: hogeproject
- slackChannel: #hoge
- key: moge-key
<-- updated - key2: moge-key2
<-- added
The reason for overriding functions.trigger.environment is that if you write it as follows,
1 | functions: |
there is no self:custom.hoge.environment setting, which causes a Warning.
1 | Serverless Warning -------------------------------------- |
That’s all.
I hope this helps.
