Serverless framework is a fantastic framework to simplified your serverless management. Most of the time, we have some configurations which are different among different environments. Let’s say how could we achieve this with Serverless framework. Including manage tests in Jest.
1. Overview
The application should be environment agnostic. So we manage all the values in the environment variables and use it in your lambda.
- Separate environment variables in their own
.json
file. - Load the above
.json
file conditionally in yourserverless.yml
- Add all the environment variables from the loaded
.json
file
For the following sections, we will setup two environments, dev
and prod
, this setup is very scalable, adding more env like uat
is just a matter of adding new uat.json
.
2. Create .json
file for your environment variables
This is the ./src/config/dev.json
1 | { |
This is the ./src/config/prod.json
1 | { |
3. Setup your serverless.yml
1 | custom: |
You will add this at the top level. Let’s digest it line by line.
- We first created a
custom
section. This is how we DIY the settings. stage: ${opt:stage, 'dev'}
: Means we will add astage
undercustom
, we would like to get the value from the CLI option--stage BLAHBLAH
, if no value from the CLI option, the fallback value will bedev
.- finally, we created
config
, and it is afile()
, the source of the file is./src/config/${self:custom.stage}.json
,self:custom.stage
refers to the current stage, which will be dynamic. Which means we will loaddev.json
orprod.json
conditionally according to the CLI option or fallback value.
4. Map all the environment variables
1 | provider: |
After loading the file, the rest is as easy as map them to the provider
section of your serverless.yml
. Here, we created a SECRET
, the value is from the SECRET
property in the .json
file.
In your lambda,
process.env.SECRET
will give you the value. And the value will be dynamic according to the currentstage
.
The values under provider
section will be shared across all functions. If you want a function-specific environment variable, you can add it under the very function that you want to configure.
5. What about unit test
I use Jest to test, the problem is when we run the tests via jest
, we lose the support of the framework, so all our previous setup is not working anymore.
This is easy to solve. At the top of your test.
1 | import config from "../config/dev.json"; |
You can use test.json
if you have a different setup. It’s very easy to understand, we import this json, and manually assign them to the process.env
.
6. End
Hope it helps.
Thanks for reading!
Follow me (albertgao) on twitter, if you want to hear more about my interesting ideas.