Day 52: Automate EBS Snapshot and Volume Deletion with Lambda for Cost Optimization
#90daysofdevops
🚀 Introduction
AWS provides Elastic Block Store (EBS) for persistent storage, but unused snapshots and unattached volumes can silently inflate costs if left unmanaged. Many organizations accumulate these resources over time, leading to unnecessary storage expenses.
Automating the cleanup of EBS snapshots and unattached volumes is a simple yet effective cost optimization strategy. By using AWS Lambda and Boto3 (AWS SDK for Python), you can implement a scheduled cleanup process that ensures storage efficiency while reducing operational overhead.
In this blog, we'll explore how to set up this automation to optimize your AWS costs, improve resource management, and maintain a lean cloud environment effortlessly.
🔸What is Boto3?
Boto3 is the AWS SDK for Python, allowing developers to interact with AWS services programmatically. It provides an easy-to-use interface for automating tasks like creating, managing, and deleting AWS resources such as EC2 instances, S3 buckets, Lambda functions, RDS databases, and more.
🔸Why Use Boto3?
Automates AWS Operations – Helps manage AWS services via Python scripts.
Interacts with AWS Services – Supports EC2, S3, RDS, Lambda, DynamoDB, and many more.
Efficient and Scalable – Reduces manual tasks and supports large-scale automation.
Secure – Uses AWS Identity and Access Management (IAM) for access control.
🔸What is an EBS Volume and Snapshot?
Amazon Elastic Block Store (EBS) is a type of storage used in AWS that works like a hard drive for cloud servers (EC2 instances). It allows users to store data, run applications, and keep files even if the server is turned off.
An EBS Volume is a virtual storage disk that can be attached to an EC2 instance. Just like a physical hard drive, it stores your data, operating system, and applications. These volumes are persistent, meaning the data remains even if the server is restarted or stopped. AWS offers different types of volumes depending on speed and performance needs, such as General Purpose SSDs, Provisioned IOPS SSDs, and HDDs.
An EBS Snapshot is a backup of an EBS volume taken at a specific point in time. Think of it as a photo of your storage at that moment. If something goes wrong, you can use a snapshot to restore your data. AWS stores snapshots in Amazon S3 in a way that saves space by only storing changes instead of full copies each time. Snapshots can also be copied across regions for disaster recovery.
Over time, unused EBS volumes and old snapshots can pile up and increase storage costs. This is why automating the cleanup of unattached EBS volumes and outdated snapshots is important. It helps businesses save money, reduce clutter, and ensure storage is used efficiently. In this blog, we’ll explore how to automate this process using AWS Lambda and Boto3, making AWS cost optimization easier and hassle-free. 🚀
Launch Instance. We can see volume id under Storage.
2. Create Snapshot.
Select a volume that has been attached to the EC2 Instance.
3. Create Function in lambda
4. After Creating function scroll down and write code given below, in code section.
import boto3
def lambda_handler(event, context):
ec2 = boto3.client('ec2')
# Get all EBS snapshots
response = ec2.describe_snapshots(OwnerIds=['self'])
# Get all active EC2 instance IDs
instances_response = ec2.describe_instances(Filters=[{'Name': 'instance-state-name', 'Values': ['running']}])
active_instance_ids = set()
for reservation in instances_response['Reservations']:
for instance in reservation['Instances']:
active_instance_ids.add(instance['InstanceId'])
# Iterate through each snapshot and delete if it's not attached to any volume or the volume is not attached to a running instance
for snapshot in response['Snapshots']:
snapshot_id = snapshot['SnapshotId']
volume_id = snapshot.get('VolumeId')
if not volume_id:
# Delete the snapshot if it's not attached to any volume
ec2.delete_snapshot(SnapshotId=snapshot_id)
print(f"Deleted EBS snapshot {snapshot_id} as it was not attached to any volume.")
else:
# Check if the volume still exists
try:
volume_response = ec2.describe_volumes(VolumeIds=[volume_id])
if not volume_response['Volumes'][0]['Attachments']:
ec2.delete_snapshot(SnapshotId=snapshot_id)
print(f"Deleted EBS snapshot {snapshot_id} as it was taken from a volume not attached to any running instance.")
except ec2.exceptions.ClientError as e:
if e.response['Error']['Code'] == 'InvalidVolume.NotFound':
# The volume associated with the snapshot is not found (it might have been deleted)
ec2.delete_snapshot(SnapshotId=snapshot_id)
print(f"Deleted EBS snapshot {snapshot_id} as its associated volume was not found.")
5. After writing code don’t deploy or test the code it will show error, before it go in configuration and create policy for describing and deleting snapshot and volume.
6. Select service as EC2.
Select option shown below
7. Write policy name.
8. Here our policy has been attached.
9. Now deploy and test the code.
10. In the given picture we can see that there is one snapshot and volume.
11. Terminate the instance then see in dashboard volume is also get delete.
12. Again test the code and now see snapshot also will get delete.
🚀 Conclusion
In this blog, we have learned how to automate the deletion of unused EBS snapshots and unattached volumes using AWS Lambda and Boto3. This automation helps in cost optimization, reduces manual effort, and keeps your AWS environment clean and efficient.
Thanks for reading to the end; I hope you gained some knowledge.❤️🙌