question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

${env:MYVAR} not working in serverless.yml

See original GitHub issue

When I run in offline mode (serverless-offline) I’m unable to access the loaded env variables in my serverless.yml. For example in the config below, the ${env:STAGE} always rolls over to the default.

# serverless.yml
service: serverless-framework-starter

plugins:
  - serverless-dotenv-plugin
  - serverless-webpack
  - serverless-offline #serverless-offline needs to be last in the list

custom:
  webpack:
    webpackConfig: 'webpack.config.js'
    includeModules: true
    packager: 'npm'
  serverless-offline:
    port: 4000
  dotenv:
    path: "./.env.${opt:NODE_ENV, 'development'}"

provider:
  name: aws
  runtime: nodejs8.10
  stage: ${env:STAGE, 'dev'}
  region: ${env:REGION, 'us-east-1'}
  versionFunctions: false
  iamRoleStatements:
  -  Effect: "Allow"
     Action:
       - "s3:*"
     Resource: "*"

functions:
  test-func:
    handler: src/test-func/handler.default
    name: ${self:provider.stage}-${self:service}-upload
    memorySize: 128
    events:
      - http:
          path: hello
          method: get
          cors: true

The test handler has no trouble accessing SOMEVAL and it uses the proper value if I change the NODE_ENV at runtime.

// handler.js
const hello = (event, context, callback) => {
  const response = {
    statusCode: 200,
    body: JSON.stringify({
      message: `Go Serverless v1.0! Your function executed successfully! ENV: ${process.env.SOMEVAL}`,
      input: event,
    }),
  };

  callback(null, response);

  // Use this code if you don't use the http event with the LAMBDA-PROXY integration
  // callback(null, { message: 'Go Serverless v1.0! Your function executed successfully!', event });
};

export default hello;

It seems like a timing issue. It seems like the serverless.yml is processed before the serverless-dotenv-plugin is able to load the environment variables into scope. Any idea how I can prove this or whether this is the right concept?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:11

github_iconTop GitHub Comments

1reaction
orels1commented, Aug 5, 2018

Have the same issue, and it’s pretty annoying =(

1reaction
ds17fcommented, May 1, 2018

Oh, yeah, if it helps:

.env.development

STAGE=dev
REGION=us-east-1
SOMEVAL=dev val
HELLO_PATH=hello

.env.production

STAGE=prod
REGION=us-east-1
SOMEVAL=prod val
HELLO_PATH=hello_world
Read more comments on GitHub >

github_iconTop Results From Across the Web

Tests do not load .env or serverless.yml variables #51 - GitHub
So, it seems the tests are expecting this variable under process.env.SAMPLE_ENV_VAR . To get it to work correctly, I had to change the...
Read more >
Variables - Serverless Framework
To reference environment variables, use the ${env:SOME_VAR} syntax in your serverless.yml configuration file. It is valid to use the empty string in place...
Read more >
Facing problem to set service name from env variable in ...
We are deploying to AWS Lambda and using container image. Serverless Framework version: 2.32.0 We could set the service name dynamically from ...
Read more >
Serverless Environment Variables - SST.Dev
Serverless Framework builds on these ideas to make it easier to define and work with environment variables in our serverless.yml by generalizing the...
Read more >
Lambda was unable to configure your environment variables
Problem. Lambda functions come with a set of default environment variables. These environment variable names are reserved, and users will not be able...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found