Backing up AWS lambda functions

There might be a built-in method to do so, but I haven’t found one. I wanted to download packages of all the lambda files I have in AWS and ended up creating a script to do it.

Of course you should replace the –region parameter with the AWS region you run your AWS lambda functions and the –profile parameter with the appropriate AWS lambda profile in your ~/.aws/credentials file (the correct way would have been to give those as parameters to the bash script but this will be left as an exercise to the reader 😉

Feel free to copy. Don’t forget to chmod u+x …

#!/bin/bash

# Get all the function names
list=`aws lambda list-functions --region us-west-2 | jq -r .Functions[].FunctionName | perl -pe 's/\n/ /g'`

# For each lambda function, get the function's download url and download it
for val in $list; do
    url=`aws lambda get-function --region us-west-2 --function-name $val --profile amnon | jq -r .Code.Location`
    shortname=`echo $url | perl -pe's/^.+\/(.+?)-[\w]{8}.+/\1.zip/g'`
    echo $shortname
    wget -nv $url -O $shortname
done

Leave a Reply

Your email address will not be published. Required fields are marked *