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.
-
1are you using serverless? did make sure to add the environment variables to the Lambda function?mostafazh– mostafazh2018-11-27 10:03:39 +00:00Commented 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.roby.elan– roby.elan2018-11-27 10:14:15 +00:00Commented 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...)?ttulka– ttulka2018-11-28 13:22:54 +00:00Commented Nov 28, 2018 at 13:22
4 Answers
Download your deployment zip file from S3 and make sure it has the .env file.
3 Comments
.env FILE not the the package or the node_modules folder.You have to mention in Environment variables at 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
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
}
}
