Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Download All Lambda Functions
# Parallelly download all aws-lambda functions
# Assumes you have ran `aws configure` and have output-mode as "text"
# Works with "aws-cli/1.16.72 Python/3.6.7 Linux/4.15.0-42-generic botocore/1.12.62"
download_code () {
local OUTPUT=$1
aws lambda get-function --function-name $OUTPUT --query 'Code.Location' | xargs wget -O ./lambda_functions/$OUTPUT.zip
}
mkdir -p lambda_functions
for run in $(aws lambda list-functions | cut -f 6 | xargs);
do
download_code "$run" &
done
echo "Completed Downloading all the Lamdba Functions!"
# If you want to download only ones with specific prefix
# https://github.com/sambhajis-gdb/download_all_lambda_function/blob/master/get_lambda_with_prefix.sh
# Credits to https://stackoverflow.com/users/6908140/sambhaji-sawant for the
@nemani
Copy link
Author

nemani commented Dec 10, 2018

This small bash script downloads the zip for each lambda function in your AWS account.

You need to create a new IAM user in AWS Console, put the access keys into aws configure (set output mode to "text"), then run the script.

@TheNetJedi
Copy link

TheNetJedi commented Feb 4, 2019

Fantastic!

@Selimcse98
Copy link

Selimcse98 commented Jun 14, 2019

Awesome! It worked nicely. Thanks a lot!

@Ton-Anne
Copy link

Ton-Anne commented Jun 20, 2019

"doth" line 11 might be a typo?
Other than that very nice! Thanks a lot.

@arorasahil1207
Copy link

arorasahil1207 commented Oct 7, 2019

I am new here can you tell me where you have put the access key/token ?

@nemani
Copy link
Author

nemani commented Oct 7, 2019

@arorasahil1207 as mentioned aws configure should do the trick

@arorasahil1207
Copy link

arorasahil1207 commented Oct 14, 2019

I don't know how to work with bash can you please elaborate so that i can understand the same,thanks in advance

@nemani
Copy link
Author

nemani commented Oct 14, 2019

First you need a bash shell.

Next you need to install aws cli which allows you to interact with AWS services via the CLI and bash scripts.

https://docs.aws.amazon.com/cli/latest/userguide/install-linux-al2017.html

Once this is done, you need to run the command aws configure to setup the initial configuration.

It will ask you inputs like text mode and the IAM config keys to use with AWS CLI.

Then you can follow the script above.

@SIVA451
Copy link

SIVA451 commented Apr 13, 2020

For me, it showing as an empty folder while unzipping it in windows OS.

@pgorpuni
Copy link

pgorpuni commented May 6, 2020

I am getting below error. I did AWS configure also.
An error occurred (ValidationException) when calling the GetFunction operation: 1 validation error detected: Value 'DB_User:' at 'functionName' failed to satisfy constraint: Member must satisfy regular expression pattern: (arn:(aws[a-zA-Z-]*)?:lambda:)?([a-z]{2}((-gov)|(-iso(b?)))?-[a-z]+-\d{1}:)?(\d{12}:)?(function:)?([a-zA-Z0-9-.]+)(:($LATEST|[a-zA-Z0-9-]+))?
xargs: wget: No such file or directory

@nemani
Copy link
Author

nemani commented May 7, 2020

@pgorpuni looks like the error is due to a lambda named 'DB_User'. The regex constraints for the name are by AWS and this does not match it. Don't think it is an error with the script. Try changing the name of the lambda.

I am getting below error. I did AWS configure also.
An error occurred (ValidationException) when calling the GetFunction operation: 1 validation error detected: Value 'DB_User:' at 'functionName' failed to satisfy constraint: Member must satisfy regular expression pattern: (arn:(aws[a-zA-Z-]*)?:lambda:)?([a-z]{2}((-gov)|(-iso(b?)))?-[a-z]+-\d{1}:)?(\d{12}:)?(function:)?([a-zA-Z0-9-.]+)(:($LATEST|[a-zA-Z0-9-]+))?
xargs: wget: No such file or directory

For me, it showing as an empty folder while unzipping it in windows OS.

@SIVA451 sorry I dont use windows so cant help by writing a script for it.
The above script is meant for bash (or similiar like zsh) shells which are commonly on linux.

@sudeep-quantelai
Copy link

sudeep-quantelai commented Dec 5, 2020

I changed the original code to extract the function (starting with a specified prefix) from the zip file and extract the python code and rename it as <function_name>.lambda

download_code () {
    local OUTPUT=$1
        aws lambda get-function --function-name $OUTPUT --query 'Code.Location' | xargs wget -O ./lambda_functions/$OUTPUT.zip
        unzip ./lambda_functions/$OUTPUT.zip -d ./lambda_functions
        mv ./lambda_functions/lambda_function.py ./lambda_functions/$OUTPUT.lambda
        rm ./lambda_functions/$OUTPUT.zip
}

mkdir -p lambda_functions
for run in $(aws lambda list-functions --query 'Functions[?starts_with(FunctionName, `<your pattern here>`) == `true`].FunctionName' --output text);
do
        download_code "$run"
done

echo "done"

@handeglc
Copy link

handeglc commented May 12, 2021

I am getting below error. I did AWS configure also.
An error occurred (ValidationException) when calling the GetFunction operation: 1 validation error detected: Value 'DB_User:' at 'functionName' failed to satisfy constraint: Member must satisfy regular expression pattern: (arn:(aws[a-zA-Z-]*)?:lambda:)?([a-z]{2}((-gov)|(-iso(b?)))?-[a-z]+-\d{1}:)?(\d{12}:)?(function:)?([a-zA-Z0-9-.]+)(:($LATEST|[a-zA-Z0-9-]+))?
xargs: wget: No such file or directory

this worked on my mac with aws-cli/2.2.3

download_code () {
    local OUTPUT=$1
    aws lambda get-function --function-name $OUTPUT --query 'Code.Location' | xargs wget -O ./lambda_functions/$OUTPUT.zip  
}

mkdir -p lambda_functions

for run in $(aws lambda list-functions  --query 'Functions[].FunctionName' --output text);
do
	download_code "$run" 
	echo $run
done

@natanloterio
Copy link

natanloterio commented Dec 25, 2021

This is another version:


download_code () {
    local OUTPUT=$1
    aws lambda get-function --function-name $OUTPUT --query 'Code.Location' | xargs wget -O ./lambda_functions/$OUTPUT.zip  
}

mkdir -p lambda_functions

for run in $(aws lambda list-functions | grep FunctionName | awk -F'"' '{print $4}');
      do
           download_code "$run" &
      done
  
      echo "Completed Downloading all the Lamdba Functions!"


After that, you could unzip them in separated folders using:


find . -name '*.zip' -exec sh -c 'unzip -d "${1%.*}" "$1"' _ {} \;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment