Connect with us

Get more updates and further details about your project right in your mailbox.

Thank you!
Oops! Something went wrong while submitting the form.
April 8, 2024

Automating AWS Lambda Version Cleanup with Node.js and AWS SDK

The best time to establish protocols with your clients is when you onboard them.

Heading

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique. Duis cursus, mi quis viverra ornare, eros dolor interdum nulla, ut commodo diam libero vitae erat. Aenean faucibus nibh et justo cursus id rutrum lorem imperdiet. Nunc ut sem vitae risus tristique posuere.

Introduction:

In the realm of serverless computing, AWS Lambda functions play a pivotal role. However, as your application grows, you might accumulate numerous versions of these functions, leading to potential storage issues.

AWS Lambda stores your function code in a private S3 bucket, with a generous allocation of 75 GB per account in each region. This space is shared by both Lambda functions and layers. Over time, repeated deployments can eat into this limit, risking deployment failures due to storage overflow.

Objective:

This article aims to simplify the process of maintaining your Lambda environment by automating the removal of the oldest Lambda function versions during each deployment using the AWS SDK.

Prerequisites:

Before we dive into the automation process, ensure you have Node.js set up on your machine. Additionally, you’ll need an AWS account with the necessary permissions to manage Lambda functions.

STEP 1

Start by setting up AWS CodePipeline for your project to enable automatic deployments. This pipeline will be the backbone of your deployment process, ensuring that updates are seamlessly pushed to your serverless application.

Please find the below link for reference:

Creating an Automated Deployment Pipeline - CodeCommit to Lambda

STEP 2

Next, create a scripts folder in your project’s root directory. Here, you’ll add a Node.js script named delete_old_versions.js. This script is designed to methodically clean up the oldest version of each Lambda function in your project.

It leverages the AWS SDK, specifically commands like ListVersionsByFunctionCommand and DeleteFunctionCommand, to manage function versions. The script also reads from a ‘serverless.yml’ file to identify all the functions that need management.

Here’s a simplified version of what the script does:

1. Initializes the Lambda client with your AWS credentials and region.

2. Reads the serverless.yml file to list all the functions.

3. For each function, it lists all versions and removes the oldest, ensuring that the $LATEST version is preserved.

Remember to include the necessary dependencies in your project and authenticate with valid AWS credentials.

Sample serverless.yml file for reference:

STEP 3

Incorporate the cleanup script into your build process by adding it to the pre-build phase of your buildspec.yml file. This ensures that before each deployment, the script runs and cleans up old Lambda versions, keeping your environment lean and efficient.

npm run — stage=$STAGE delete-old-versions

The above command takes the environment variable specified in the build stage of the pipeline

It also runs the delete-old-versions command which is specified in the scripts of package.json file, which in turn runs the script provided in the delete_old_versions.js file.

Conclusion:

Integrating this Node.js script into your deployment pipeline, such as within an AWS CodeBuild project, automates the housekeeping of Lambda function versions. This not only aids in cost management by preventing unnecessary storage use but also ensures the smooth operation and performance of your serverless applications.

CodeStax.Ai
Profile
April 9, 2024
-
6
min read
Subscribe to our newsletter
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Share this article:

More articles