Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bf4/825009c3e49bf53259e8c18c4c38f190 to your computer and use it in GitHub Desktop.
Save bf4/825009c3e49bf53259e8c18c4c38f190 to your computer and use it in GitHub Desktop.
Download All Lambda Functions
#!/bin/bash
#
# Usage:
# ./download_all.sh
# ./download_all.sh download us-east-1 my-function
# ./download_all.sh help
#
# Downloads all aws-lambda functions to a subdirectory
# Assumes you have a role that has at least read access to lambda.
# Credits to https://gist.github.com/nemani/defdde356b6678352bcd4af69b7fe529
set -euo pipefail
IFS=$'\n\t'
# Influenced by https://dev.to/thiht/shell-scripts-matter
# And see https://github.com/bf4/Notes/wiki/Shell-Scripting
abort=0
show_help() {
abort=${abort:0}
sed -ne '/^#/!q;s/.\{1,2\}//;1d;p' < "$0" >&$((abort+1))
exit $abort
}
# shellcheck disable=SC2034
export AWS_PROFILE="${AWS_PROFILE:-your-profile-here}"
readonly DOWNLOAD_PATH_BASE="./lambda_functions"
# trap ctrl-c and call ctrl_c()
trap ctrl_c INT
ctrl_c() {
echo "** CTRL-C Detected - aborting..."
exit 1
}
get_function_names() {
local region; region="$1"
aws lambda list-functions --region "$region" --query 'Functions[].FunctionName' --output text
}
get_function() {
local region; region="$1"
local function_name; function_name="$2"
local download_path
download_path="${DOWNLOAD_PATH_BASE}/$region/$function_name"
mkdir -p "$download_path"
aws lambda get-function --function-name "$function_name" --region "$region" --query 'Code.Location' \
| xargs wget --quiet -O "$download_path/$function_name.zip"
unzip -o -f -d "$download_path" "$download_path/$function_name.zip"
}
download_region(){
local region; region="$1"
echo "searching region: $region"
for function_name in $(get_function_names "$region"); do
get_function "$region" "$function_name"
done
}
get_regions(){
aws ec2 describe-regions --query "Regions[].{Name:RegionName}" --output text | sort -r
}
main() {
# search all regions
for region in $(get_regions); do
download_region "$region"
done
echo "Completed Downloading all the Lamdba Functions!"
}
case "${1:-}" in
help|-h|--help)
show_help
;;
download)
shift
get_function "$1" "$2"
;;
*)
shift
main
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment