Serverless, a framework for managing cloud applications, is a very powerful tool. As your application grows in complexity, it can be difficult to manage deployment variables based on different stages (development, QA, production). Currently it isn’t easy to reference deployment specific custom or environment variables. I knew there had to be a better way of handling these variables while also making them composable. After some research into serverless’ architecture, I came up with a plugin that does just that:
What does it do?
serverless-plugin-composed-vars lets you define stage specific variable files. It overrides the variables defined in your
serverless.yml or in separate
environment.yml files for custom and environment variables respectively. To define stage specific files, just insert the stage name into the file like so:
environment.stage.yml. For example, if you’d like to create variable files for your stage named “prod”, you’d name the files
Let’s see it in action:
Using the example files above, custom and environment variables are composed and computed into the following for a “prod” deployment stage:
For a cleaner
serverless.yml service file, you can separate your default variables into their own
serverless-plugin-composed-vars will automatically read those files when composing your deployment variables. Note that the
serverless.yml file variables have the least priority when also using default variable files.
How do I use it?
Install the plugin using your favorite package manager:
npm install -D serverless-plugin-composed-vars
yarn add -D serverless-plugin-composed-vars
Add the plugin to your service file:
Note: To ensure compatibility with other plugins, it is recommended that
serverless-plugin-composed-vars be the first plugin in the plugins list.
Create deployment stage variable files. The default file names are
environment.stage.yml for custom and environment variables. Here’s an example project structure for custom variables:
For advanced usage and configuration, see the GitHub Readme.
How does it work?
Under the covers,
serverless-plugin-composed-vars leverages the fact that serverless doesn’t compute variables until hooks are executed. This allows the plugin to compose deployment stage variables and rewrite them before your service is packaged for deployment.
Check it out!
Now that you’ve seen how easy it is to use deployment stage variables, give the plugin a shot. Leave any comments or suggestions here or submit issues on the GitHub repository.