DEV Community

Cover image for Bulk Backup and Restoration of Numerous AWS Lambda Functions
Atsushi Suzuki
Atsushi Suzuki

Posted on

Bulk Backup and Restoration of Numerous AWS Lambda Functions

I'm currently sorting through a multitude of redundant Lambda functions (about 50) created by my predecessor. While I'm inclined to delete them, I've decided to back them up just to be safe.

Typically, the backup and restoration of Lambda functions involves the use of SAM (Serverless Application Model) or CloudFormation. However, creating individual templates can be tedious. Thus, I decided to try a method where I could handle the code and configuration of functions in bulk using AWS CLI.

Backup

First, I listed all the Lambda function names in a file called lambda_functions.txt.



aws lambda list-functions --query 'Functions[*].[FunctionName]' --output text > lambda_functions.txt


Enter fullscreen mode Exit fullscreen mode

Next, I created a shell script named backup_lambda.sh to download the settings and code for each Lambda function.



while read function; do
  echo "Backing up $function"
  aws lambda get-function --function-name $function --query 'Configuration' > $function-config.json
  aws lambda get-function --function-name $function --query 'Code.Location' --output text | xargs wget -O $function.zip
done <lambda_functions.txt


Enter fullscreen mode Exit fullscreen mode

This script performs the following operations for each Lambda function listed in lambda_functions.txt:

  • It downloads the function's settings in JSON format and saves them in a JSON file named after the function ($function-config.json).
  • It downloads the function's deployment package (which includes the code necessary for execution and the dependencies libraries) and saves it in a ZIP file named after the function ($function.zip).

This way, I was able to back up all the Lambda functions at once.

  • Note: You can choose the backup storage location according to your requirements. To ensure quick access to the objects at the time of restoration, I chose S3 Glacier Instant Retrieval. I enabled Server-Side Encryption (SSE) but did not enable versioning or access logging (considering the importance of the data and the acceptable risk level).

Image description

Restoration

You can manually or automatically restore each function using the saved code (ZIP) and configuration file (JSON).

Manual Restoration of Individual Functions

  1. Log in to the AWS Lambda console
  2. Click on "Create Function"
  3. Select "Author from Scratch"
  4. Refer to the backed-up configuration file (~.json) to enter the function name, runtime, role, etc.
  5. Click on "Create Function"
  6. Navigate to the newly created function's page and in the "Function Code" section, select "Actions" -> "Upload a .zip file" and upload the backed-up ZIP file (~.zip)

Automatic Restoration of Multiple Functions via Shell Script

If you want to automatically restore multiple Lambda functions, you can use a shell script.

First, create a list (.txt) of the names of the Lambda functions you want to restore.



lambda-aaa
lambda-bbb
lambda-ccc


Enter fullscreen mode Exit fullscreen mode

Create a shell script named restore_lambda.sh.



while read function; do
    echo "Restoring $function"
    role=$(jq -r '.Role' $function-config.json)
    handler=$(jq -r '.Handler' $function-config.json)
    runtime=$(jq -r '.Runtime' $function-config.json)
    aws lambda create-function --function-name $function --handler $handler --runtime $runtime --role $role --zip-file fileb://$function.zip
done <lambda_functions.txt


Enter fullscreen mode Exit fullscreen mode

Grant execution permission to the shell script.



chmod +x restore_lambda.sh


Enter fullscreen mode Exit fullscreen mode

Execute the shell script.



./restore_lambda.sh


Enter fullscreen mode Exit fullscreen mode

Top comments (0)