Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Friday, November 25, 2016

How can I use environmental variables on AWS Lambda?

How can I use environmental variables on AWS Lambda?


I'm writing an application which I want to run as an AWS Lambda function but also adhere to the Twelve-Factor app guidelines. In particular Part III. Config which requires the use of environmental variables for configuration.

However, I cannot find a way to set environmental variables for AWS Lambda instances. Can anyone point me in the right direction?

If it isn't possible to use environmental variables can you please recommend a way to use environmental variables for local development and have them transformed to a valid configuration system that can be accessed using the application code in AWS.

Thanks.

Answer by helloV for How can I use environmental variables on AWS Lambda?


There is no way to configure env variables for lambda execution since each invocation is disjoint and no state information is stored. However there are ways to achieve what you want.

AWS credentials - you can avoid storing that in env variables. Instead grant the privileges to your LambdaExec role. In fact, AWS recommends using roles instead of AWS credentials.

Database details: One suggestion is to store it in a well known file in a private bucket. Lambda can download that file when it is invoked, read the contents which can contain database details and other information. Since the bucket is private, others cannot access the file. The LambdaExec role needs IAM privileges to access the private bucket.

Answer by Tom for How can I use environmental variables on AWS Lambda?


Perhaps the 'custom environment variables' feature of node-lambda would address your concerns:

https://www.npmjs.com/package/node-lambda
https://github.com/motdotla/node-lambda

"AWS Lambda doesn't let you set environment variables for your function, but in many cases you will need to configure your function with secure values that you don't want to check into version control, for example a DB connection string or encryption key. Use the sample deploy.env file in combination with the --configFile flag to set values which will be prepended to your compiled Lambda function as process.env environment variables before it gets uploaded to S3."

Answer by Wim Mostmans for How can I use environmental variables on AWS Lambda?


We also had this requirement for our Lambda function and we "solved" this by generating a env file on our CI platform (in our case this is CircleCI). This file gets included in the archive that gets deployed to Lambda. Now in your code you can include this file and use the variables.

The script that I use to generate a JSON file from CircleCI environment variables is:

cat >dist/env.json <

I like this approach because this way you don't have to include environment specific variables in your repository.

Answer by Hadrien for How can I use environmental variables on AWS Lambda?


Lambda functions must be environment agnostic so that it can run both dev, test and production.

You should never package configuration with your lambda function. 2 methods are I would consider:

  • When using API Gateway, AWS advises to use different stages for different environments and set all configuration variables as stage variables

  • Just store lambda configurations in a S3 bucket or Dynamodb table or any other AWS storage. The name/access to configuration can be then derived from the invoked function ARN and have an alias for each environments:

    invoked_function_arn

    The ARN used to invoke this function. It can be function ARN or alias ARN. An unqualified ARN executes the $LATEST version and aliases execute the function version it is pointing to.

    To minimize the overhead of fetching configuration each time at startup, keep in mind that AWS Lambda maintains the container for some time in anticipation of another Lambda function invocation. Therefore, keep configuration in global scope so that it might remain initialized between invocations and therefore reduce launch time.

Answer by sergio for How can I use environmental variables on AWS Lambda?


As of November 18, 2016, AWS Lambda supports environment variables.

Environment variables can be specified both using AWS console and AWS CLI. This is how you would create a Lambda with an LD_LIBRARY_PATH environment variable using AWS CLI:

aws lambda create-function \    --region us-east-1    --function-name myTestFunction    --zip-file fileb://path/package.zip    --role role-arn    --environment Variables={LD_LIBRARY_PATH=/usr/bin/test/lib64}    --handler index.handler    --runtime nodejs4.3    --profile default  

Answer by Froyke for How can I use environmental variables on AWS Lambda?


AWS just added support for configuration of Lambda functions via environment parameters. Take a look here: enter link description here


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.