Create Lambda Environment Variables with AWS CLI

avatar

Borislav Hadzhiev

Sat Sep 18 20212 min read

How to Create Lambda Environment Variables with AWS CLI #

To create Environment Variables on a Lambda function using AWS CLI, run the update-function-configuration command, passing in the --environment parameter.

shell
aws lambda update-function-configuration --function-name YourFunction --environment "Variables={COLOR=green,FRUIT=avocado}" --query "Environment"

create lambda environment variables

In the command above, we've set 2 environment variables on the function, using the --environment parameter, COLOR=green and FRUIT=avocado.

The --query parameter is optional and filters the output from the command, returning only the environment variables.

When creating environment variables with the update-function-configuration command, all of the environment variables the lambda previously had are replaced with the passed in ones.

When trying to add environment variables to a lambda function with the AWS CLI, and not necessarily replace them, it's best to first list the function's environment variables, using the get-function-configuration command:

shell
aws lambda get-function-configuration --function-name testFunction --query '{revisionId: RevisionId, env: Environment}'

list environment variables

In the command above, we've used the optional --query parameter to output only the lambda configuration properties that are relevant to us:

  • the environment variables the function has
  • the revisionId, which you can include when creating environment variables, to make sure no one of your colleagues has created any other environment variables on the function in the meantime.

Depending on how many environment variables your function has, it's often easier to store them in a json file.

Copy and paste the value of the env property of the get-function-configuration command and store it in a file called environment.json, adding in any additional environment variables:

environment.json
{ "Variables": { "FRUIT": "avocado", "COLOR": "green", "FOOD": "pizza" } }

Next, open your terminal in the directory where the environment.json file is stored and run the update-function-configuration command, passing in the file to the --environment parameter:

shell
aws lambda update-function-configuration --function-name YourFunction --environment file://environment.json --revision-id "your-revision-id" --query "Environment"
It only makes sense to include the --revision-id parameter if multiple people are working on the function and there is a risk that they can override the environment variables of the function in the short period of time between you list the function's environment variables and update them.

update lambda environment variables

To verify the environment variables of the lambda function have been updated, we can run the get-function-configuration command again:

shell
aws lambda get-function-configuration --function-name YourFunction --query '{revisionId: RevisionId, env: Environment}'

list updated environment variables

The output shows that the environment variables we stored in the json file have been created on the lambda function and after the update the revisionId has changed.

Further Reading #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee