Serverless-webpack: Env variables set in serverless.yml not being built

Created on 12 Sep 2017  路  7Comments  路  Source: serverless-heaven/serverless-webpack

I need to read environment variables in my lambda function. It looks like the environment variables from the serverless config are not actually set at build time when the code is transpiled - therefore undefined in the output. I have created a bash script to set them then run serverless deploy, but is there already a solution for this?

I have the following in my function config:

    environment:
      MONGO_URI:  ${file(${opt:secrets}):MONGO_URI}

and my webpack config for EnvironmentPlugin:

    plugins: [
      new webpack.EnvironmentPlugin(['NODE_ENV', 'MONGO_URI']),
    ],
    "serverless-webpack": "^3.0.0-rc.1",
    "webpack": "^3.5.6"

Most helpful comment

馃憢 Just wanted to note that you forgot the lib property in your example to access the top level env object:

slsw.lib.serverless.service.provider.environment

All 7 comments

Hi @realseanp, thanks for asking 馃憤
The environment variables are not set at build time, but only at runtime of the lambda. That's a basic concept of Serverless + Lambda.
But there is a way to fetch them during the build process:

If you use the following in your webpack.config.js it might work:

// webpack.config.js
const slsw = require('serverless-webpack');

const myFunc = slsw.serverless.service.getFunction(<< function name >>);
process.env.NODE_ENV = myFunc.environment.NODE_ENV;
process.env.MONGO_URI = myFunc.environment.MONGO_URI;
...
  new webpack.Environment([ 'NODE_ENV', 'MONGO_URI' ]);
...

So you have full access to the serverless state in your webpack config, and thus to the resolved variables of your service. You can do anything you want in your webpack.config.

I'm not sure if the code I've written above works, but it should be enough, so that you can get what I mean here 馃槃

That's helpful. Is there a way to access the top level environment property in the serverless config? Where can I find all the useful methods like getFunction. I've got it working with the bash script setting the env vars before running sls deploy, but pulling them all from the serverless config would be a little nicer. Thanks for the response.

The top environment object can be found at slsw.serverless.service.provider.environment, where serverless.service should be the complete resolved serverless.yaml with all variables replaced.

You can find the methods here: https://github.com/serverless/serverless/blob/master/lib/classes/Service.js. That's the class prototype for the service object.

@realseanp Any news here? Did you manage to achieve access to the resolved variables through the environment object?

Hey - yes I've got it working. I'm still using the bash script and setting env variables before building, but good to know I can grab them from the config! Thanks for your response.

馃憢 Just wanted to note that you forgot the lib property in your example to access the top level env object:

slsw.lib.serverless.service.provider.environment

@realseanp Oh, sorry. You're right 馃憤 . Thanks for the hint.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tommedema picture tommedema  路  4Comments

deftomat picture deftomat  路  5Comments

wooooooak picture wooooooak  路  5Comments

rvaidya picture rvaidya  路  4Comments

bebbi picture bebbi  路  3Comments