2

With the following AWS SAM template process.env is an empty object. I expect it to contain the environment variable from the template defined as dbURL.

  AWSTemplateFormatVersion: "2010-09-09"
  Transform: "AWS::Serverless-2016-10-31"
  Description: "An example RESTful service"
  Resources: 
   ExampleFunction: 
   Type: "AWS::Serverless::Function"
   Properties: 
    Runtime: "nodejs6.10"
    Handler: "/dist/getTickets/index.handler"
    Events: 
      RootDeveloperHub: 
        Type: "Api"
        Properties: 
          Path: "/new"
          Method: "any"
    Environment:
      Variables:
      dbURL: "dbURL_Value"

handler

exports.handler = (event, context, callback) => {
  // logs {}
  console.log(process.env)
}

Things I've ruled out:

  • I get the expected output of environment variables with the code and template above using the AWS SAM CLI. Are you sure it's reproducible with that code? Maybe it's related to how you package and deploy the code? There's at least another question for a similar issue for somebody using Webpack (https://stackoverflow.com/questions/43786682/aws-lambda-environment-variables-not-set-process-env-undefined-in-node). – Dunedan Jul 09 '18 at 05:01
  • Thanks for seeing if that set up worked for you! I'm going to look more into if how I'm packaging/deploying the code is the issue. – Tom Quinlan Jul 09 '18 at 18:33
  • Thanks @Dunedan https://stackoverflow.com/questions/43786682/aws-lambda-environment-variables-not-set-process-env-undefined-in-node had my answer. I added {target: node} to my webpack config and now I see the environment variable(s) defined in the AWS-SAM template. – Tom Quinlan Jul 09 '18 at 22:32

1 Answers1

1

I should have tested the code I posted...

The handler works when it is set up as

exports.handler = (event, context, callback) => {
  // logs {}
  console.log(process.env)
}

The fix for my code was to add the following to my webpack config.

{
  ...
  target: 'node'
}