DEV Community

shimo for AWS Community Builders

Posted on

3 1

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
#!/bin/bash
# This script updates AWS Lambda environment variables using the AWS CLI.
# It can add or update environment variables, and remove specified variables.
#
# Parameters:
# function_name: The name of the Lambda function you want to update
# region: The AWS region where your Lambda function is located
# env_vars_key_value_upsert: An array containing key-value pairs of environment variables to add or update
# env_vars_key_remove: An array containing keys of environment variables to remove
# Set the region variable
region="ap-northeast-1"
# Set your function name directly in the script
function_name="your_lambda_function_name"
# Define your environment variables as key-value pairs to add or update
env_vars_key_value_upsert=(
"MY_ENV1" "value1"
"MY_ENV2" "value2"
)
# Specify environment variables to remove
env_vars_key_remove=("MY_ENV3" "MY_ENV4")
# Retrieve the existing environment variables
existing_env_vars=$(aws lambda get-function-configuration \
--function-name "$function_name" \
--region "$region" \
--query 'Environment.Variables' \
--output json)
# Update or insert new environment variables
updated_env_vars="$existing_env_vars"
for ((i = 0; i < ${#env_vars_key_value_upsert[@]}; i+=2)); do
updated_env_vars=$(echo "$updated_env_vars" \
| jq --arg key "${env_vars_key_value_upsert[$i]}" --arg value "${env_vars_key_value_upsert[$i + 1]}" \
'.[$key] = $value')
done
# Remove specified environment variables
for key in "${env_vars_key_remove[@]}"; do
updated_env_vars=$(echo "$updated_env_vars" \
| jq --arg key "$key" \
'del(.[$key])')
done
# Update the Lambda function configuration with the new environment variables
aws lambda update-function-configuration \
--function-name "$function_name" \
--environment "{\"Variables\": $updated_env_vars}" \
--region "$region"

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.

Image of Datadog

Master Mobile Monitoring for iOS Apps

Monitor your app’s health with real-time insights into crash-free rates, start times, and more. Optimize performance and prevent user churn by addressing critical issues like app hangs, and ANRs. Learn how to keep your iOS app running smoothly across all devices by downloading this eBook.

Get The eBook

Top comments (0)

Best Practices for Running  Container WordPress on AWS (ECS, EFS, RDS, ELB) using CDK cover image

Best Practices for Running Container WordPress on AWS (ECS, EFS, RDS, ELB) using CDK

This post discusses the process of migrating a growing WordPress eShop business to AWS using AWS CDK for an easily scalable, high availability architecture. The detailed structure encompasses several pillars: Compute, Storage, Database, Cache, CDN, DNS, Security, and Backup.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay