If you wish to supply environment-specific variables to your [[Serverless Framework]] service, you can use the techniques described below to dynamically load the correct value based on the stage.
The most common uses for environment variables in your serverless app are:
- References to IDs/ARNs/URLs of other AWS resources (often defined within the same stack)
- URLs to third party APIs
⚠️ You should only use environment variables for non-sensitive configuration settings. For secrets, you should store these in either SSM Parameter Store or Secrets Manager and dynamically fetch their values at runtime from your Lambda handler.
## Fixed values per-stage
The following example shows how to configure a `THIRD_PARTY_SERVICE_ROOT_URL` environment variable which will be set to a specific value based on the current stage.
If the stage doesn't match one of the three listed, the value will be undefined.
```yml
# serverless.yml
...
provider:
environment:
THIRD_PARTY_SERVICE_ROOT_URL: ${self:custom.thirdPartyServiceUrl.${sls:stage}}
custom:
thirdPartyServiceUrl:
dev: https://dev.api.example.com
test: https://test.api.example.com
prod: https://api.example.com
```
## Fixed values per-stage with a default fallback
The following example shows how to configure a `LOG_LEVEL` environment variable which will be set to `INFO` if the stage is set to `prod` but will fallback to `DEBUG` if stage is unset or set to another value.
```yml
# serverless.yml
provider:
environment:
LOG_LEVEL: ${self:custom.logLevel.${sls:stage}, '${self:custom.logLevel.default}'}
custom:
logLevel:
default: DEBUG
prod: INFO
```