Last active
December 24, 2024 17:44
-
-
Save TheNetJedi/0847ba0ec1da9879b4fa1d8f3276b004 to your computer and use it in GitHub Desktop.
Bash script to download all your Lambda functions.
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
#!/usr/bin/env bash | |
#You need to have aws-cli installed and configured | |
#Credits to Reddit user u/aa93 for the suggestions | |
mkdir code | |
aws lambda list-functions | \ | |
grep FunctionName | \ | |
cut -d '"' -f4 | \ | |
while read -r name; do | |
aws lambda get-function --function-name $name | tail -n 3 | egrep -o 'https?://[^ ]+' | sed 's/"//' | xargs wget -O ./code/$name.zip | |
done |
This worked for me using git-bash terminal on windows.
#!/usr/bin/env bash
#You need to have aws-cli installed and configured
#You need to have CURL installed
#Credits to Reddit user u/aa93 for the suggestions
mkdir -p code
aws lambda list-functions | \
grep FunctionName | \
#Select only matching prefix functions
grep <prefix> | \
cut -d '"' -f4 | \
while read -r name; do
aws lambda get-function --function-name $name | tee ./code/$name.json | egrep -o 'https?://[^ ]+' | sed 's/"//' | xargs curl --output ./code/$name.zip
done
The scripts above were not working, below is a script which is working like a charm. It has the ability to search through all regions or you can enter a region yourself. Hope it helps!
#!/usr/bin/env bash
set -euo pipefail
# Check for dependencies: aws, jq, unzip
if ! command -v aws &>/dev/null; then
echo "ERROR: aws CLI is not installed. Please install it and try again."
exit 1
fi
if ! command -v jq &>/dev/null; then
echo "ERROR: jq is not installed. Please install it (e.g. brew install jq) and try again."
exit 1
fi
if ! command -v unzip &>/dev/null; then
echo "ERROR: unzip is not installed. Please install it (e.g. brew install unzip) and try again."
exit 1
fi
# You can hard-code specific regions if desired, for example:
# REGIONS=("us-east-1" "us-west-2")
# Or dynamically discover them:
REGIONS=($(aws ec2 describe-regions --query "Regions[].RegionName" --output text))
for region in "${REGIONS[@]}"; do
echo ">>> Processing region: $region"
# Get all Lambda function names in this region
FUNCTIONS=($(aws lambda list-functions \
--region "$region" \
--query "Functions[].FunctionName" \
--output text))
if [ ${#FUNCTIONS[@]} -eq 0 ]; then
echo "No Lambda functions found in region: $region"
continue
fi
# Create a directory for this region (if you want them grouped by region)
mkdir -p "$region"
for function_name in "${FUNCTIONS[@]}"; do
echo " - Downloading source for function: $function_name"
# Create a subdirectory for each function to store source files
function_dir="$region/$function_name"
mkdir -p "$function_dir"
# Get the presigned URL for the Lambda function code
code_url=$(aws lambda get-function \
--region "$region" \
--function-name "$function_name" \
--query "Code.Location" \
--output text)
# Download the ZIP
zip_file="$function_dir/source.zip"
curl -s -o "$zip_file" "$code_url"
# Unzip into the function's directory
echo " => Unzipping to $function_dir"
unzip -oq "$zip_file" -d "$function_dir"
# Optional: Remove the ZIP after unzipping
rm "$zip_file"
echo " => Source code retrieved for $function_name"
done
echo "Finished region: $region"
done
echo "All done!"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using Wget didnt worked really well for me i instead replaced it by curl and it works well
aws lambda get-function --function-name $name | tee ./code/$name.json | jq -r '.Code.Location' | xargs curl --output ./code/$name.zip