Having trouble setting environment variables for AWS Lambda using Serverless v1.4.0

2.7k views Asked by At

Following the documentation, I tried declaring environment variables in the serverless.yml file under provider:

provider:
  cfLogs: true
  name: aws
  runtime: nodejs4.3
  stage: dev
  region: eu-west-1
  profile: serverless-admin
  environmnent:
    IS_REMOTE: ${file(./config.yml):IS_REMOTE}
    REMOTE_ENV: "YES"

None of these are available to me when trying to get them using process.env.IS_REMOTE or process.env.REMOTE_ENV.

This is the log of trying to console.log them:

2017-01-01 06:22:57.777 (+02:00)        undefined       REMOTE_ENV:  undefined
2017-01-01 06:22:57.777 (+02:00)        undefined       IS_REMOTE:  undefined

This is inside Lambda when using serverless invoke (not locally).

Hope someone can help me figure this out, as it seems like I'm following the docs about right.

2

There are 2 answers

1
Zanon On BEST ANSWER

This feature works fine for me. I believe that you have mistyped something.

Could you please create a new project and test the following steps? Maybe we can find what is your issue through a MCVE. Give me a feedback if this code does not work for you.

  1. Check your Serverless version (expected: 1.4.0)

    serverless --version
    
  2. Create a new project

    serverless create --template aws-nodejs --name test-project
    
  3. Use the following serverless.yml

    service: test-project
    
    provider:
      name: aws
      runtime: nodejs4.3
      environment:
        VAR_1: foo
    
    functions:
      hello:
        handler: handler.hello
    
  4. Use the following handler.js

    module.exports.hello = (event, context, callback) => {
    
      console.log(process.env.VAR_1);
    
      const response = {
        statusCode: 200,
        body: JSON.stringify({
          message: process.env.VAR_1
        }),
      };
    
      callback(null, response);
    };
    
  5. Deploy

    serverless deploy
    
  6. Test

    serverless invoke --function hello
    

HTTP result:

{
    "statusCode": 200,
    "body": "{\"message\":\"foo\"}"
}

Log:

2017-01-02T20:13:58.551Z    fg57ea3c-e127-11e6-bf5a-93b2958503d8    foo
0
GeeDawg On

From the code, it looks like environmnent is misspelt.