Last active
November 26, 2019 20:24
-
-
Save aniket91/19492b32f570ece202718153661b1823 to your computer and use it in GitHub Desktop.
Shell script to update lambda code and environment variables to multiple regions and environments using AWS CLI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#---------------------------------------------------- | |
# Author : athakur | |
# Version : 1.0 | |
# Date : 10/02/2018 | |
# Description : Deployment script to update lambda code and env variables | |
# Sample usage : | |
# Local : ./update-lambda.sh test aws-admin fileb://../lambda.zip | |
# Dev : ./update-lambda.sh dev aws-admin fileb://../lambda.zip | |
# QA : ./update-lambda.sh qa aws-admin fileb://../lambda.zip | |
# Prod : ./update-lambda.sh prod aws-admin fileb://../lambda.zip | |
# More details : http://opensourceforgeeks.blogspot.in/2018/02/shell-script-to-update-lambda-code-and.html | |
#---------------------------------------------------- | |
echo "Updating lambda code for ENV : $1 PROFILE : $2 ZIP_FILE_PATH : $3" | |
ENV=$1 | |
PROFILE=$2 | |
ZIP_FILE_PATH=$3 | |
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] | |
then | |
echo "Incorrect arguments supplied. Format - ./update-lambda.sh ENV PROFILE ZIP_FILE_PATH" | |
exit | |
fi | |
FUNCTION_NAME="my-lambda-$ENV" | |
ENV_VAR_1=1991 | |
ENV_VAR_2="test" | |
SNS_ENDPOINT='' | |
SUPPORTED_REGIONS=("us-east-1" "ap-northeast-1" "ap-southeast-1" "ap-southeast-2") | |
for REGION in ${SUPPORTED_REGIONS[@]} | |
do | |
echo 'Region : $REGION' | |
case "$REGION" in | |
"us-east-1") | |
SNS_ENDPOINT="arn:aws:sns:us-east-1:123456789123:my-sns-notification" | |
;; | |
"ap-northeast-1") | |
SNS_ENDPOINT="arn:aws:sns:ap-northeast-1:123456789123:my-sns-notification" | |
;; | |
"ap-southeast-1") | |
SNS_ENDPOINT="arn:aws:sns:ap-southeast-1:123456789123:my-sns-notification" | |
;; | |
"ap-southeast-2") | |
SNS_ENDPOINT="arn:aws:sns:ap-southeast-2:123456789123:my-sns-notification" | |
;; | |
*) | |
echo "Environment not provided" | |
exit 1 | |
;; | |
esac | |
env_variables="{\"Variables\":{\"ENV_VAR_1\":\"${ENV_VAR_1}\",\"ENV_VAR_2\":\"${ENV_VAR_2}\",\"SNS_ENDPOINT\":\"${SNS_ENDPOINT}\",\"ENV\":\"${ENV}\"}}" | |
echo "Env variables : $env_variables" | |
lambda_update_env_command="aws lambda update-function-configuration --function-name $FUNCTION_NAME --region $REGION --profile $PROFILE --environment '$env_variables' --timeout 300 --memory-size 3008" | |
echo "Executing command : $lambda_update_env_command" | |
eval $lambda_update_env_command | |
lambda_update_code_command="aws lambda update-function-code --function-name $FUNCTION_NAME --region $REGION --zip-file $ZIP_FILE_PATH --profile $PROFILE" | |
echo "Executing command : $lambda_update_code_command" | |
eval $lambda_update_code_command | |
echo "Completed Lambda function update for region $REGION" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment