3

Created a nodejs lambda function locally and integrated dotenv, so that env variables are accessed using process.env method. It is working. While deploying the same lambda and when tested inside aws console, it is returning undefined for env variable. Any idea why it so.

3
  • 1
    are you using serverless? did make sure to add the environment variables to the Lambda function? Commented Nov 27, 2018 at 10:03
  • not using any serverless frameworks..added dotenv package for using process.env and it is working locally. After deployment it to aws(uploading zip to S3), it is showing undefined for env variables. Commented Nov 27, 2018 at 10:14
  • is the env variable set when you open the lambda via the aws console console.aws.amazon.com/lambda ? How do you deploy the lambda (CloudFormation, manually...)? Commented Nov 28, 2018 at 13:22

4 Answers 4

1

Download your deployment zip file from S3 and make sure it has the .env file.

Sign up to request clarification or add additional context in comments.

3 Comments

zip file is just a collection of node packages and it has the dotenv package too
I'm talking about the .env FILE not the the package or the node_modules folder.
hi @roby.elan can you accept my answer if it fixed the issue of you? thanks
0

You have to mention in Environment variables at Lambda.

Edit to add the variables in Lambda

either if you are using server-less template to deploy , mentioned under 'Environment'

# serverless.yml

 provider:
  name: aws
  runtime: nodejs4.3
  environment:
   envOne: 12345678

 functions:
  myFunction:
  environment:
   envTwo: 87654321

Reference : https://www.serverless.com/blog/serverless-v1.2.0

https://www.serverless.com/framework/docs/providers/aws/guide/variables/

Comments

0

In case it helps: you need to specify the environment variable in lambda function section of the template.yaml file as well. Example:

requestRetrievalFunction:
 ...
 Properties:
  ...
  Environment:
        Variables:
          EMAILS_TABLE: !Ref EmailsTable
          TIME_TO_EXPIRE: 20
  ...

In my particular case I am using two env variables (EMAILS_TABLE and TIME_TO_EXPIRE). For local testing I defined them in the env.json file too:

{
    "requestRetrievalFunction": {
        "EMAILS_TABLE": "nico-ppot",
        "TIME_TO_EXPIRE": 30
    }
}

Comments

0

In my case AWS and Google Cloud Platform not allow us to use some keywords name in the variable environnement like :

not ok :  AWS_ACCESS_KEY_ID = "XXXXX"
ok : CLOUD_ACCESS_KEY_ID = "XXXXX"
not ok : AWS_SECRET_ACCESS_KEY = "XXX"
ok CLOUD_SECRET_ACCESS_KEY = "XXX"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.