There are two ways you can handle with this. Inside serverless.yml
you’ll want to add a custom stage variable to make it easy to reference the stage you’re deploying to.
custom:
stage: "${opt:stage, self:provider.stage}"
If you want single env file then create env.yml
with a section for each stage and the environment variables below it. For example:
dev:
MY_VAR: 'the dev value'
prod:
MY_VAR: 'the prod value'
Then setup the environment variables inside your serverless.yml
using
environment: ${file(env.yml):${self:custom.stage}}
If you want one file per env then create env-STAGE.yml
(i.e. env-dev.yml, env-prod.yml, etc). Each file should look like:
MY_VAR: 'the value'
The setup the environment variables inside your serverless.yml
so that it uses the correct file.
environment: ${file(env-${self:custom.stage}.yml)}
You’ll probably want to add your env file to the .gitignore.