5

I want to download AWS Lambda source package as Zip. I know that there is an option to download lambda function as SAM file or deployment package in lambda console. But I don't have access to AWS console in the production environment. See the attached screens.

enter image description here

Below are the two available options.

enter image description here I want to do same functionality in AWS CLI with minimum shell script commands. After downloading the lambda source in zip format, I will create lambda function in production environment via AWS CLI.

aws lambda create-function --region [AWSREGION] --function-name [FUNCTION] --zip-file fileb://[ZIPFILE] --role [ROLEARN] --handler [FILENAME].lambda_handler  --description="[FUNCTIONDESCRIPTION]"  --runtime [RUNTIME] --timeout [TIMEOUT] --memory-size [MEMORYSIZE] --profile [PROFILENAME]

Please help me with this matter, help in linux shell script commands will be highly appreciated.

5 Answers5

5

bash one liner

aws lambda get-function --function-name function_name --query 'Code.Location'  | xargs wget -o function_name.zip
Freibuis Orth
  • 51
  • 1
  • 1
2

You may find the answer here :

Download an already uploaded Lambda function

A simple bash solution is also provided at https://gist.github.com/nemaniarjun/defdde356b6678352bcd4af69b7fe529

# 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 | head -n 1 | cut -f 2 | xargs wget -O ./lambda_functions/$OUTPUT.zip  
}

mkdir lambda_functions
for run in $(aws lambda list-functions | cut -f 6 | xargs);
doth
    download_code "$run" &
done

Edit: Credits to the original author. Just sharing the code since the URL may get unreachable later.

Dhananjai Pai
  • 5,914
  • 1
  • 10
  • 25
2

I've created a simple Bash script based off the original Python script shared earlier.

The difference being that it accepts JSON input and downloads the files in sequence rather than in parallel.

AWS Lambda Download Bash Script

# !/bin/sh
## List the names of all Lambda functions. Can be constrained by using --max-items
for i in `aws lambda list-functions | grep FunctionName | cut -d ":" -f2 | cut -d '"' -f2`
do
  echo 'Fetching code for function:' $i
  ## Using each name, get the function details and then download the zip file containing the source code.
  aws lambda get-function --function-name $i | grep Location | awk -F' ' '{print $2}' | xargs wget -O $i.zip
  echo 'Code downloaded to' $i.zip
done
Reegz
  • 511
  • 1
  • 6
  • 13
  • if you don't have wget, just use curl in the above `aws lambda get-function --function-name $i | grep Location | awk -F' ' '{print $2}' | xargs curl --output $i.zip` – justdan0227 Jun 01 '22 at 14:15
0

Looking at your requirements you can use aws lambda get-function CLI command to download lambda function deployment package.

See Synopsis.

  get-function
--function-name <value>
[--qualifier <value>]
[--cli-input-json <value>]
[--generate-cli-skeleton <value>]

You can also see full detail.

But this command will not give you the zip file. If you execute the command.

aws lambda get-function --function-name MyLambdaFunction

It will give you similar to below result.

{
    "Code": {
        "RepositoryType": "S3",
        "Location": "https://awslambda-eu-west-1-tasks.s3.eu-west-1.amazonaws.com/snapshots/014747066885/MyLambdaFunction-aa227fd0-4d4a-4690-9447-6e1818aaa752?versionId=HoQu5vbudzRpYLe0laIVQIahVN2NVxET&X-Amz-Security-Token=FQoGZXIvYXdzEIr%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaDB%2FdpZU6fCyQG%2ByhJyK3A7Dycy5L9hVWmExELuh6f0jFskmKJ62GhGf3J7LC94wB5E5CU2jplsLhw%2Fd%2FmmmJktzo07wI3XLWvSj6zxbHvJFdscCAqF7AYZOhRQR4mOIN6HkanRrHMBHeoTeDqOT6Yk8elhQYfno7dSHP%2FwdNVutS9P0SNmDLDhrxNLAxceDz8nBj1N9AZqhfMwV65OCtTubgLaLSFei75DosXIUaylWsrXgrz4B%2F6bo8LmeDxhNcYefGOBMvwKtyFSdPAP1TulcJpwQIUIC3losjtcTnRt9CSTxhn7TPMDfw4QI5ITKvxgNzO5T2TF2cJVqbotFFVdqPQNHuL2XLMNU24BwjSwF%2FsKWlV6tygXhdQWpTrJFRW%2FqxV%2BX2C1yq0sjpWtc5SerkrmqHvvDjA0L7GlOpG8Q1BLHyQWj0FPmuhrrPyjyFCNqVkpo6eUl35yK%2BHWa1hsXoEPyccoqdHa4gU%3D&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20190203T092717Z&X-Amz-SignedHeaders=host&X-Amz-Expires=600&X-Amz-Credential=ASIA54NGUQSHZ4CZTFNT%2F20190203%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=ee3bbef557cff32f86d26abc769b14"
    },
    "Configuration": {
        "TracingConfig": {
            "Mode": "PassThrough"
        },
        "Version": "$LATEST",
        "CodeSha256": "l6q5ldtk0YEhEv3wnJhhCiAPyRd2XB1/8nT+ZWk=",
        "FunctionName": "MyLambdaFunction",
        "MemorySize": 3008,
        "RevisionId": "a3bdbef4-8616-4c6a-ba19-074acb80b143",
        "CodeSize": 6083880,
        "FunctionArn": "arn:aws:lambda:us-east-1:014747066885:function:MyLambdaFunction",
        "Handler": "lambda_function.lambda_handler",
        "Role": "arn:aws:iam::014747066885:role/lambda_admin",
        "Timeout": 900,
        "LastModified": "2019-01-30T10:09:50.283+0000",
        "Runtime": "python3.6",
        "Description": "Test MyLambdaFunction"
    }
}

Now you need to convert it to zip format file with further tweaks.

You can use below two commands to get a lambda function in zip file format.

aws lambda get-function --function-name MyLambdaFunction --profile [AWS_ROFILE] | grep "Location" | awk -F ": " '{ print $2}' | sed 's/"//g' > ~/MyLambdaFunction.txt
wget -i ~/MyLambdaFunction.txt -O ~/MyLambdaFunction.zip

Here is a full shell script commands.

FUNCTION_NAME=${1}
AWS_PROFILE=[AWSPROFILE]
aws lambda get-function --function-name ${FUNCTION_NAME} --profile ${AWS_PROFILE} | grep "Location" | awk -F ": " '{ print $2}' | sed 's/"//g' > ~/${FUNCTION_NAME}.txt
wget -i ~/${FUNCTION_NAME}.txt -O ~/${FUNCTION_NAME}.zip

You can convert it to a shell script (e.g getLambdaFunction.sh) and execute it with below command.

./getLambdaFunction.sh [FUNCTIONNAME]

After getting lambda package as zip file, you can create lambda function.

aws lambda create-function --region us-east-1 --function-name MyLambdaFunction --zip-file fileb://MyLambdaFunction.zip --role arn:aws:iam::[AWSACCOUNT]:role/service-role/[LAMBDAROLE] --handler lambda_function.lambda_handler --description="My Lambda Function" --runtime "python3.6" --profile [AWSPROFILE]

As a sample: it is assumed that MyLambdaFunction is lambda function name, us-east-1 is aws region and run-time is python 3.6.

I Bajwa PHD
  • 1,708
  • 1
  • 20
  • 42
0

If the chosen answer didn't work for you too, try this:

Install jq:

# for e.g on a Debian based distro (ubuntu, mint, MXlinux, ..)
sudo apt install jq

Use this script (past it in a a file named download_all_lambda_functions.sh then run bash download_all_lambda_functions.sh)

download_code () {
    # clean double quotes if found
    local OUTPUT=${1//\"/}
    local dest=./lambda_functions/$OUTPUT.zip
    local URL=$(aws lambda get-function --function-name $OUTPUT --query 'Code.Location')

    # Using curl instead of wget
    echo $URL | xargs curl -o $dest
}

mkdir -p lambda_functions

for run in $(aws lambda list-functions | jq -r .Functions[].FunctionName);
do
    echo Found lambda function: $run
    download_code "$run" 
done

echo "Completed Downloading all the Lamdba Functions!"

I believe the chosen answer's code got broken because AWS CLI is returning a JSON file, which is better explored using tools like jq

Find gist here

Mossab
  • 818
  • 8
  • 13