Skip to content

Instantly share code, notes, and snippets.

@fizz
Forked from monkut/update_awslambda_envars.bash
Last active January 16, 2021 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fizz/f455843d72afa0e661463ffe47106338 to your computer and use it in GitHub Desktop.
Save fizz/f455843d72afa0e661463ffe47106338 to your computer and use it in GitHub Desktop.
update aws lambda environment variables via awscli and jq
#!/usr/bin/env bash
# the `update-function-configuration` overwrites the existing set envars.
# In order to *ADD* variables we need to read the existing envars and add to that.
# This command uses `jq` to read and transform the json result to an envar then update the lambda configuration
# create the updated envar set
FUNCTION_NAME=trip-analytics-prod-app
UPDATED=.env.prod
UPDATE=$(jq --raw-input --slurp 'split("\n")[:-1]|map(split("=") as [$key, $value] | {$key, $value}) | from_entries' $UPDATED)
export UPDATED_ENVIRONMENT_VARIABLES=$(aws --region us-west-2 lambda get-function-configuration --function-name ${FUNCTION_NAME} | \
jq --compact-output ".Environment + {\"Variables\": (.Environment.Variables + ${UPDATE})}")
# check
env | grep UPDATED_ENVIRONMENT_VARIABLES
# update current lambda configuration
aws --region us-west-2 lambda update-function-configuration --function-name ${FUNCTION_NAME} \
--environment ${UPDATED_ENVIRONMENT_VARIABLES} | jq .Environment.Variables
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment