Serverless-offline: Environment Variable for local development only via .env file

Created on 9 Jun 2018  路  5Comments  路  Source: dherault/serverless-offline

I am wondering if there is a recommended way for someone to have local environment variable that do not get deployed via serverless. Most local env vars are different for each developer for our setup and we are using SSM to deploy env vars to the lambda.

I looked into a couple dotenv plugins for serverless but they seem to either deploy the env vars to the lambda or create a .env file from the environment variables in the serverless.yml file. That seems kind of backwards to me.
https://www.npmjs.com/package/serverless-dotenv-plugin
https://www.npmjs.com/package/serverless-plugin-dotenv
https://www.npmjs.com/package/@jimdo/serverless-dotenv
https://www.npmjs.com/package/serverless-dynamic-dotenv
https://www.npmjs.com/package/serverless-plugin-package-dotenv-file
https://www.npmjs.com/package/serverless-dotenv

I also looked in the issues here and found #259 and #324 which might be related but the explanation is a bit short.

I am hoping there is an easy way to do this and I just haven't found it yet.

Most helpful comment

@justhamade

Settled on a setup that uses serverless-pseudo-parameters and dotenv-load.

Public keys are stored on .env.staging .env.development etc files with secrets on ssm.

Serverless files contain:

 environment:
    NODE_ENV: ${env:NODE_ENV}
    DYNAMO_ENDPOINT: ${env:DYNAMO_ENDPOINT}
    SECRET_KEY: ${ssm:/${env:NODE_ENV}/SECRET_KEY}

Which are accessed in the code as process.env.SECRET_KEY

All 5 comments

@justhamade

I noticed this plugin https://github.com/lolcookie/serverless-offline-ssm but couldn't get it working on my setup.

There is also the --noEnvironment flag which I also couldn't get working with load-env package.

Thanks @lolcookie I will look into those

@justhamade

Settled on a setup that uses serverless-pseudo-parameters and dotenv-load.

Public keys are stored on .env.staging .env.development etc files with secrets on ssm.

Serverless files contain:

 environment:
    NODE_ENV: ${env:NODE_ENV}
    DYNAMO_ENDPOINT: ${env:DYNAMO_ENDPOINT}
    SECRET_KEY: ${ssm:/${env:NODE_ENV}/SECRET_KEY}

Which are accessed in the code as process.env.SECRET_KEY

https://www.npmjs.com/package/serverless-dotenv-plugin has worked for me, it is essentially reading the .env file and append those values to provider.environment, but you need to put the plugin before serverless-webpack and serverless-offline.

closing. please re-open if the issue still exists.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stunningpixels picture stunningpixels  路  3Comments

Looveh picture Looveh  路  4Comments

JimLynchCodes picture JimLynchCodes  路  4Comments

davidroman0O picture davidroman0O  路  4Comments

MEGApixel23 picture MEGApixel23  路  4Comments