Automate AWS Cost Savings: Clean Up Stale EBS Snapshots with Terraform & Lambda

Bilal AmjadBilal Amjad
4 min read

In cloud environments, accumulating unneeded resources can quickly drive up costs, often stemming from overlooked components. A common scenario arises when developers create Amazon EBS snapshots for backup, intrinsically linked to their EC2 instances. However, when these EC2 instances are eventually terminated, the associated snapshots are frequently forgotten, silently incurring charges for unused storage. Manually identifying and purging these stale EBS snapshots across a growing infrastructure becomes a tedious and error-prone task, leading to avoidable expenditure.

This article provides a hands-on guide to building a robust, automated solution for Automating AWS Cost Savings: Stale EBS Snapshot Cleanup with Terraform & Lambda. You will learn how to leverage Infrastructure as Code (Terraform) to deploy a powerful, serverless AWS Lambda function. This “smart” function will intelligently examine your EBS snapshots, identify those no longer attached to active EC2 instances, and automatically delete them. This proactive automation is essential for maintaining a clean, efficient, and truly cost-optimized AWS environment, addressing a pervasive challenge of cloud waste. While this guide demonstrates the core automation, this process can be further enhanced for continuous, hands-off operation by integrating time-based triggers using Amazon EventBridge.

Prerequisites

To begin, ensure you have the following in place. Click here to view the detailed list of requirements.

Prepare the Target: Create a Stale Snapshot (Manual)

Launch an EC2 Instance:

  • Navigate to the EC2 service.

  • Click “Launch instance” -> “Launch instance”.

  • Choose a simple AMI (e.g., “Amazon Linux 2 AMI”, Free tier eligible).

  • Select an instance type (e.g., t2.micro, Free tier eligible).

  • Proceed through the steps, leaving storage defaults (which creates a root EBS volume).

  • Launch the instance. Wait for it to be in running state.

Create a Snapshot of its Root Volume:

When we create ec2 instance, the volume gets created along with it. You can see:

  • In the EC2 console, navigate to Snapshots (under “Elastic Block Store” in the left menu).

  • Select the volume.

  • Click Create snapshot.

  • Go to Snapshots and wait for your new snapshot to reach completed state.

Terminate the EC2 Instance (Makes Snapshot Stale):

  • Go back to Instances in the EC2 console.

  • Select the EC2 instance you just launched.

  • Click Instance state -> Terminate instance. Confirm termination.

  • Crucial: When you terminate an EC2 instance, its root EBS volume is usually deleted automatically. This action makes the snapshot you just created “stale” because its source volume no longer exists and it’s not attached to any running instance.

  • Verify this by checking Volumes (the volume should be gone) and Snapshots (your snapshot should remain, but its Volume ID will refer to a non-existent volume).

Now, the snapshot has become stale.


Deploying & Verifying

Project Structure

For this example, we’ll maintain a clean project structure:

.
├── main.tf
└── python/
    └── lambda_function.py

Clone the Repository

https://github.com/bilal-amjad-dev/terraform-aws-lambda-ebs-stale-snapshot-cleanup

Local Setup & Terraform Deployment

Navigate to your Project Folder: Open a New Terminal in VS Code (Terminal > New Terminal)

terraform init
terraform plan
terraform apply
Enter a value: yes

Confirm the Action: Test the Lambda Function (Manual)

Now that the Lambda is deployed, let’s run it.

  • Navigate to the Lambda Console: In the AWS Management Console, go to the Lambda service.

  • Select Your Function: Find and click on your function, DeleteStaleEBSSnapshot.

  • Access the Test Tab: Click on the “Test” tab to prepare for invocation.

Configure a Test Event:

  • Click “Create new event”.

  • Event name: Type a name, e.g., EBSStaleSnapshotEvent.

  • Click the “Test” button.

The Lambda function has executed successfully.

Witness the Cleanup: Verify Snapshot Deletion (Manual)

Confirm that the Lambda function did its job.

  • Go to the EC2 Console: Navigate back to the EC2 service in your AWS Management Console.

  • Check Snapshots: In the left menu, under “Elastic Block Store,” click on Snapshots.

  • Confirm Deletion: Refresh the page. Your stale snapshot should now be GONE!

If it’s gone, congratulations! Your Terraform-deployed Lambda function successfully performed the cleanup.


Wrapping Up

Responsible Cloud Management: Clean Up Lab Resources

Always destroy lab resources to prevent unnecessary AWS costs.

  • Return to Terminal: Navigate to your project directory.

  • Destroy Resources: Run terraform destroy. Type yes to confirm.

terraform destroy
Enter a value: yes

Conclusion

You’ve successfully deployed and tested your first serverless cost optimization solution using Terraform and AWS Lambda! This lab demonstrates the power of Infrastructure as Code for automating meaningful tasks and managing your cloud resources efficiently.

0
Subscribe to my newsletter

Read articles from Bilal Amjad directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Bilal Amjad
Bilal Amjad

Hi, I'm Bilal Amjad, a passionate DevOps enthusiast specializing in AWS, Terraform, and Kubernetes. I create beginner-friendly tutorials on my blog (bilalamjad.hashnode.dev) and YouTube channel (@bilal-amjad-dev) to share my learning journey and help others master cloud technologies. Explore my projects on GitHub (bilal-amjad-dev) and connect with me on LinkedIn for DevOps collaboration! 🚀