AWS Lambda Environment Variables Tutorial (Console + CLI)

Updated on
dog with fan env behind

What are environment variables in Lambda?

Environment variables in AWS Lambda are key pairs (string), which get stored in a Lambda function’s configuration.

Environment variables are version-specific, i.e. the environment variables set would differ with each AWS Lambda function version.

How to set Lambda Environment Variables?

There are 2 ways to set key-value pairs in your Lambda function config, and access them from your function code easily at execution time.

Set environment variables using the Lambda Console

  • Go to AWS Lambda Function in your AWS Console
  • Select Configuration tab
  • Click on Edit on the left

AWS Lambda console configuration tab in environment variables

Once you click on Edit you will be redirected to Edit environment variables page.

AWS Lambda function edit environment variables page

  • Click on Add environment variable button
  • Enter the name/key for your variable
  • Enter the value (string) of the variable in value
  • Click on Save

Preview Lambda environment variables

Once you save the changes you should be able to see the variables list in the Configuration page.

AWS Lambda console configuration page with updated environment variables

Manage Lambda environment variables with AWS CLI

Get environment variables

We can use get-function-configuration common in aws lambda CLI to get the environment variables.

aws lambda get-function-configuration --function-name not-a-function --query "Environment.Variables"

If you have some variables defined, it will come in the output:

{
  "DOMAIN": "https://learnaws.io"
}

Set environment variables

We can use update-function-configuration command from aws lambda CLI to update our Lambda function’s environment variables.

aws lambda update-function-configuration --function-name not-a-function --environment 'Variables={SETTER=CLI}'

Syntax

Variables={KeyName1=string,KeyName2=string}

Warning

The previously set environment variables will be overwritten.

Read AWS Lambda environment variables from code

Below are some of the examples for popular programming languages on reading the environment variables from code.

Node.js

let region = process.env.AWS_REGION;

Python

region = os.environ['AWS_REGION']

Ruby

region = ENV["AWS_REGION"]

Java

String region = System.getenv("AWS_REGION");

Go

var region = os.Getenv("AWS_REGION")

C#

string region = Environment.GetEnvironmentVariable("AWS_REGION");

Hills 🏔 and Skills, What's Common?

They both need you to be on top.

You will get lifetime access with:

All yours, just at:

$149

Just type and your search result will magically appear here