How to create multiple stages in serverless framwork
Use Serverless' $self
reference interpolation which can include further interpolation.
Define a custom variable where necessary. You can also use a default value if the variable doesn't exist.
Example:
service: some-cache-updater
custom:
functimeout:
prod: 120
uat: 60
provider:
name: aws
runtime: nodejs8.10
stage: ${opt:stage, 'dev'}
functions:
scheduledUpdater:
handler: handler.scheduledUpdater
# Lookup stage key from custom.functimeout. If it doesn't exist
# default to 10
timeout: ${self:custom.functimeout.${self:provider.stage}, '10'}
Then, when you deploy you can pass the --stage prod
or --stage uat
argument. In this example, no setting the stage will default to dev
serverless.yml:
...
provider:
stage: ${opt:stage, 'dev'}
...
Command line:
sls deploy --stage prod
${opt:stage, 'dev'} takes the value passed from command line --stage option. In this case prod. If no option is passed dev is taken as default.
More info here: https://serverless.com/framework/docs/providers/aws/guide/variables/#recursively-reference-properties