Correct .env file with serverless-dotenv-plugin
Asked Answered
B

3

3

I'm using the following as a custom serverless-dotenv-plugin plugin configuration:

custom: dotenv: path: .env-${opt:stage, 'local'}

But what I'm really trying to get is that the environment be loaded from .env file when I give no arguments and .env.staging file when I use staging as a CLI argument.

I don't know how this can be reflected in path above. Any help please?

Buster answered 4/7, 2018 at 9:35 Comment(0)
C
5

I am the author of serverless-dotenv-plugin. There was a logistical problem when trying to dynamically load env files from the provider or other options. I have since updated the plugin however so that you can dynamically load env files based what environment is set.

For example, if you run "NODE_ENV=production sls deploy" it will look for a file called .env.production. If it is not found, it will fallback to .env.

See the README for more detail https://github.com/infrontlabs/serverless-dotenv-plugin

Cabaret answered 24/9, 2018 at 19:25 Comment(0)
I
3

I got your use case to work by just using the normal dotenv plugin.

In my serverless.yaml, I specify environment variables to be loaded from a file based on the stage parameter (dev is default):

provider: 
  stage: ${opt:stage, 'dev'}
  environment:
    FOO: ${file(./config.${self:provider.stage}.js):getEnvVars.FOO}
    BAR: ${file(./config.${self:provider.stage}.js):getEnvVars.BAR}

Then one file per stage that loads the environment variables from the right .env file:

config.dev.js:

require('dotenv').config({path: __dirname + '/dev.env'});
const config = require('./environmentVariables.js');
module.exports.getEnvVars = config.getEnvVars;

config.production.js:

require('dotenv').config({path: __dirname + '/production.env'});
const config = require('./environmentVariables.js');
module.exports.getEnvVars = config.getEnvVars;

Instead of exporting every environment variables in each of the above config files, I created a helper file for this (environmentVariables.js):

module.exports.getEnvVars = () => ({
    FOO: process.env.FOO,
    BAR: process.env.BAR
});

Last but not least the .env files containing the actual variables. I named the files dev.env and production.env.

FOO=foo
BAR=bar

It works like a charm, the only downside being that you have to edit several different files whenever you want to add a new environment variable.

Ignominy answered 20/9, 2018 at 13:34 Comment(2)
Why are you requiring staging.env inside config.staging.js but then you say the file is named production.env?Sierrasiesser
Thanks for spotting Adrián, it has been corrected now.Ignominy
C
0

Unfortunately the serverless-dotenv-plugin plugin no longer works as nicely since serverless now loads env variables before processing the plugins. See FAQ section in the plugin readme.

To get around this, I used dotenvx to load env variables from a .env file in a different folder than my serverless.yml, and pass those on to a different script/command being run.

Example:

// package.json
{
  "scripts": {
    "start": "dotenvx run -f path/to/.env -- sls offline start"
  }
}
Chariness answered 11/9 at 12:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.