paint-brush
Making serverless variables work for youby@chris.feist
2,000 reads
2,000 reads

Making serverless variables work for you

by Chris FeistJanuary 12th, 2019
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

<a href="https://serverless.com/" target="_blank">Serverless</a>, 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: <code class="markup--code markup--p-code"><a href="https://github.com/chris-feist/serverless-plugin-composed-vars" target="_blank">serverless-plugin-composed-vars</a></code>.

Company Mentioned

Mention Thumbnail
featured image - Making serverless variables work for you
Chris Feist HackerNoon profile picture

Photo by Caspar Camille Rubin on Unsplash

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: [serverless-plugin-composed-vars](https://github.com/chris-feist/serverless-plugin-composed-vars).

What does it do?

[serverless-plugin-composed-vars](https://github.com/chris-feist/serverless-plugin-composed-vars) lets you define stage specific variable files. It overrides the variables defined in your serverless.yml or in separate variables.yml and environment.yml files for custom and environment variables respectively. To define stage specific files, just insert the stage name into the file like so: variables.stage.yml or environment.stage.yml. For example, if you’d like to create variable files for your stage named “prod”, you’d name the files variables.prod.yml and environment.prod.yml.

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:



custom:googlesWebsite: www.google.commyEndpoint: api.endpoint.com




environment:THE_ANSWER_IS: 42USER_TABLE_NAME: UsersMY_ENDPOINT: api.endpoint.com

For a cleaner serverless.yml service file, you can separate your default variables into their own variables.yml and environment.yml files. [serverless-plugin-composed-vars](https://github.com/chris-feist/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

Install the plugin using your favorite package manager:

npm install -D serverless-plugin-composed-vars

or

yarn add -D serverless-plugin-composed-vars

Enable

Add the plugin to your service file:

# serverless.yml

plugins:

  • serverless-plugin-composed-vars
  • other-serverless-plugin

Note: To ensure compatibility with other plugins, it is recommended that [serverless-plugin-composed-vars](https://github.com/chris-feist/serverless-plugin-composed-vars) be the first plugin in the plugins list.

Create

Create deployment stage variable files. The default file names are variables.stage.yml and environment.stage.yml for custom and environment variables. Here’s an example project structure for custom variables:

Advanced Usage

For advanced usage and configuration, see the GitHub Readme.

How does it work?

Under the covers, [serverless-plugin-composed-vars](https://github.com/chris-feist/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.