https://github.com/bizon/serverless-plugin-env-stage-config
Serverless plugin to define environment variables files for stages
https://github.com/bizon/serverless-plugin-env-stage-config
environment-variables serverless serverless-framework serverless-plugin
Last synced: 9 months ago
JSON representation
Serverless plugin to define environment variables files for stages
- Host: GitHub
- URL: https://github.com/bizon/serverless-plugin-env-stage-config
- Owner: bizon
- License: mit
- Created: 2021-07-28T16:44:28.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2025-05-26T05:20:28.000Z (10 months ago)
- Last Synced: 2025-06-09T12:40:20.573Z (10 months ago)
- Topics: environment-variables, serverless, serverless-framework, serverless-plugin
- Language: JavaScript
- Homepage:
- Size: 822 KB
- Stars: 4
- Watchers: 0
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# serverless-plugin-env-stage-config
[](https://www.npmjs.com/package/serverless-plugin-env-stage-config)
[](https://github.com/xojs/xo)
This [Serverless](http://www.serverless.com/) plugin allows to define environment variable configuration files for stages.
It exposes a new variable resolver (`$esc`) that automatically picks the correct value based on the selected stage.
For development stages (`local`, `dev`, `development`), the variables will default to the system’s environment variables.
## CI
[](https://github.com/bizon/serverless-plugin-env-stage-config/actions/workflows/tests.yml)
[](https://github.com/bizon/serverless-plugin-env-stage-config/actions/workflows/release.yml)
## Installation
```sh
pnpm add -D serverless-plugin-env-stage-config
```
```yaml
plugins:
- serverless-plugin-env-stage-config
```
## Usage
For non-development stages, the variables will be retrieved from a `serverless.env.${stage}.yml` file, alongside the `serverless.yml`.
Let’s consider the following `serverless.yml`:
```yaml
name: my-service
useDotenv: true
resources:
Resources:
MyQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: some-queue
provider:
name: aws
environment:
MYSQL_HOST: ${esc:MYSQL_HOST}
MYSQL_PORT: ${esc:MYSQL_PORT}
functions:
run-something:
handler: handler.run
environment:
QUEUE_URL: ${esc:QUEUE_URL}
SECRET_TOKEN: ${esc:SECRET_TOKEN}
```
## Local stages
In a local environment, the variables resolved with the `esc:` prefix will be equivalent to using `env:`. Using `useDotenv: true` alongside a `.env` file will alow you to define your environement variables.
The supported local stages are:
- `local`
- `dev`
- `development`
## Other stages
If you need to define a different source for the environement variables for your `prod` stage, you can define a `serverless.env.prod.yml` to redefine the variables.
For example, if you’re using AWS SSM Parameter Store, you could create the following file:
```yaml
MYSQL_HOST: ${ssm:/my-service/prod/MYSQL_HOST}
QUEUE_URL: !Ref MyQueue
SECRET_TOKEN: ${ssm:/my-service/prod/SECRET_TOKEN}
```
Any variable that is not included in the `serverless.env.prod.yml` file will produce a warning and fallback to using `env:`.
In this case, you will get the following warning:
> Serverless: env-stage-config: WARNING: the MYSQL_PORT variable is not defined in serverless.env.prod.yml, defaulting to ${env:MYSQL_PORT, null}.
If a variable is not defined in the stage environment configuration file, or the environment (`process.env`), it will default to `null`.
## License
MIT
## Miscellaneous
```
╚⊙ ⊙╝
╚═(███)═╝
╚═(███)═╝
╚═(███)═╝
╚═(███)═╝
╚═(███)═╝
╚═(███)═╝
```