I store my environment variables in the root of my project in a .env
file that’s in the .gitignore
and I’ll load that into a JS file that can be referenced via serverless.yml
Ex:
/
.env
serverless.yml
/config
envVars.js
serverless.yml
provider:
environment:
VALUE1: ${file(./config/envVars.js):getEnvVars.value1}
VALUE2: ${file(./config/envVars.js):getEnvVars.value2}
.env
VALUE1=foo
VALUE2=bar
/config/envVars.js
const dotenv = require('dotenv');
// require and configure dotenv, will load vars in .env in PROCESS.ENV
dotenv.config();
module.exports.getEnvVars = () => ({
value1: process.env.VALUE1,
value2: process.env.VALUE2
});
This will allow you to pull your environment variables from a local .env
file or straight from the environment (like in CI).