AWS Cloud Cost Optimization: Identifying and Deleting Stale EBS Snapshots
Amazon Elastic Block Store (EBS) provides persistent block storage volumes for use with Amazon EC2 instances. However, EBS snapshots, which are backups of these volumes, can accumulate over time, leading to unnecessary storage costs. To optimize costs, it's crucial to identify and delete stale EBS snapshots—those no longer associated with active EC2 instances. In this blog, we'll create a Lambda function to automate this process, from identifying to deleting unused snapshots.
Step-by-Step Guide to Automating EBS Snapshot Cleanup with AWS Lambda
Prerequisites
An AWS account with sufficient permissions to manage Lambda, EC2, and EBS.
Basic understanding of AWS services and Python programming.
Step 1: Set Up IAM Role for Lambda
Create an IAM Role:
Go to the IAM service in the AWS Management Console.
Click on "Roles" and then "Create role."
Choose "AWS Service" and select "Lambda."
Click "Next: Permissions" and attach the following policies:
AmazonEC2ReadOnlyAccess
AmazonEC2FullAccess
Click "Next: Tags" (optional) and then "Next: Review."
Provide a role name, e.g.,
Lambda_EC2_EBS_Cleanup
, and create the role.
Step 2: Create the Lambda Function
Navigate to Lambda Service:
Open the AWS Management Console and navigate to the Lambda service.
Click on "Create function."
Configure the Lambda Function:
Choose "Author from scratch."
Function name:
CleanupStaleEBSSnapshots
Runtime:
Python 3.x
Role: Choose the
Lambda_EC2_EBS_Cleanup
role created earlier.Click "Create function."
Write the Lambda Function Code:
Deploy the Code:
- Click "Deploy" to save and deploy your Lambda function.
Step 3: Set Up a CloudWatch Events Rule
Navigate to CloudWatch:
Go to the AWS Management Console and open the CloudWatch service.
Click on "Rules" under the "Events" section and then "Create rule."
Configure Event Source:
Choose "Event Source" as "Schedule."
Configure the schedule (e.g.,
rate(1 day)
to run daily).
Add Target:
Add a target and select "Lambda function."
Choose the
CleanupStaleEBSSnapshots
Lambda function.Click "Configure details," provide a name for the rule, and create the rule.
Step 4: Testing the Lambda Function
Manual Test:
Navigate back to the Lambda function in the AWS Management Console.
Click on "Test" and configure a new test event with any sample JSON (it won't be used).
Click "Test" to execute the function manually and check the CloudWatch logs for any output.
Conclusion
Automating the identification and deletion of stale EBS snapshots can significantly reduce AWS storage costs. By setting up a Lambda function, you can ensure that your environment remains cost-efficient without manual intervention. This guide provided a detailed walkthrough from creating the necessary IAM roles to deploying and testing the Lambda function. Regular execution of this function ensures that your storage usage is optimized, keeping your AWS bill under control.
Subscribe to my newsletter
Read articles from SAI GOUTHAM.K directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
SAI GOUTHAM.K
SAI GOUTHAM.K
M-Tech Software Engineering student with a strong foundation in computer science and a keen interest in DevOps. Over the course of my academic journey, I have developed skills in various programming languages such as Python, Java, and Bash scripting. My coursework and projects have provided me with hands-on experience in software development, system administration, and cloud computing. During my studies, I completed several projects that involved setting up CI/CD pipelines using Jenkins, Docker, and Kubernetes. These projects allowed me to understand the importance of automation, continuous integration, and continuous deployment in modern software development practices. I have also gained familiarity with version control systems like Git and have a basic understanding of monitoring tools such as Prometheus and Grafana. I am enthusiastic about starting my career in DevOps and am eager to contribute to a dynamic team where I can continue to learn and grow. I am particularly interested in the areas of infrastructure as code, automation, and cloud-native applications. My goal is to leverage my skills and passion to help streamline development processes, improve system reliability, and deliver high-quality software solutions.