DEV Community

shimo for AWS Community Builders

Posted on

Manage AWS Lambda Environment Variables with CLI Shell Script

In this blog post, I introduce a script that allows you to efficiently manage the environment variables of an AWS Lambda function.

This script provides the capability to:

  1. Register a new variable: Add a new environment variable to the Lambda function.
  2. Update an existing variable: Modify the value of an existing environment variable.
  3. Delete a variable: Remove an environment variable from the Lambda function.

Important Note on Updating Environment Variables

When using the aws lambda update-function-configuration command to update the existing environment variables, it is crucial to pass both the environment variable you want to add or update and all existing environment variables. Failing to do so will result in the deletion of the existing environment variables. The script provided in this blog post takes this into consideration and ensures the proper handling of environment variables during the update process.

Parameters

function_name: The name of the Lambda function.

region: The AWS region where the Lambda function is deployed.

env_vars_upsert: Key-value pairs of environment variables to be upserted (updated or inserted).

  • During the upsert process, the environment variable will be updated if it exists; otherwise, a new one will be registered.

  • You can include any number of key-value pairs.

env_vars_upsert=(
  "MY_ENV1" "value1"
  "MY_ENV2" "value2"
)
Enter fullscreen mode Exit fullscreen mode

env_vars_remove: Keys of environment variables to be removed.

  • If the specified key does not exist, nothing will happen.
env_vars_remove=("MY_ENV3" "MY_ENV4")
Enter fullscreen mode Exit fullscreen mode

Output

The script will response in JSON format. The "Environment" section will display the updated state of the environment variables.

{
    "FunctionName": "xxxx",
    ...

    "Environment": {
        "Variables": {
            "MY_ENV2": "value2",
            "MY_ENV1": "value1"
        }
    },
    ...
}
Enter fullscreen mode Exit fullscreen mode

Summary

I introduced a shell script for managing AWS Lambda environmental variables, enabling users to register, update, and delete variables with ease. This script also takes into consideration the prevention of accidental deletions, ensuring safe and efficient management of your Lambda function's environmental variables.

Caution

Before using this script in a production environment, please create a test Lambda function and test it thoroughly to ensure it works as expected.

Top comments (0)