This project uses an AWS Lambda function to automatically identify and delete stale EBS snapshots — including those from unattached, stopped, or deleted volumes. If a snapshot is older than 30 days or hasn’t been in use for 30 days, it qualifies for deletion. For testing purposes, the threshold is currently set to 0 days to allow immediate evaluation. The function also estimates monthly cost savings based on snapshot size and notifies users via Amazon SNS. It’s a fully serverless, cost-optimization solution aligned with modern cloud infrastructure and DevOps best practices.
- Reduces AWS costs by removing unused EBS snapshots that silently incur charges.
- Production-style architecture with alerting and permission boundaries.
- Production-relevant tools: AWS Lambda, IAM, EC2, SNS, boto3, infrastructure scripting.
- AWS Lambda – Serverless function to automate cleanup
- Amazon EC2 & EBS – Source of snapshots
- Amazon SNS – Sends email alerts when snapshots are deleted
- IAM – Access control via custom roles and policies
- Python (boto3) – AWS SDK to interact with EC2, Volumes, and SNS
- Launch a test EC2 instance in any AWS region (e.g.,
us-east-1). - Go to EC2 → Snapshots → Select volume → Create Snapshot.
- Go to AWS Lambda → Create Function.
- Set runtime to Python 3.13 or later.
- Use the code from
lambda_function.pyin this repository. - Under Configuration → General, set timeout to 10 seconds.
- Deploy the function.
Screenshot: Lambda function created

Attach the following permissions to your Lambda's execution role:
ec2:DescribeInstancesec2:DescribeSnapshotsec2:DescribeVolumesec2:DeleteSnapshotsns:Publish
Screenshot: Custom IAM policy for ec2:DescribeInstances

Screenshot: SNS Publish Permission

- Go to Amazon SNS → Create Topic → Standard.
- Name it something like
snapshot-alerts. - Add a subscription method (e.g., Email).
- Confirm the subscription via the email link.
Screenshot: Subscription Notification

Screenshot: Subscription Confirmed

- Keep the EC2 instance running.
- Run the Lambda function from the Test tab.
- Expected: No snapshots should be deleted.
- Terminate the EC2 instance.
- Run Lambda again.
- It will detect stale snapshots and:
- Delete them
- Send alert via SNS to your email
Screenshot: Successful Lambda Test

Screenshot: Email Notification

Permissions required for Lambda role:
ec2:DescribeInstancesec2:DescribeSnapshotsec2:DescribeVolumesec2:DeleteSnapshotsns:Publish
- Automating snapshot cleanup workflows
- Working with IAM and cross-service policies
- Serverless scripting with Python and boto3
- Integrating notifications via SNS
- Implementing cloud cost optimization logic
- Implement tag-based retention filtering
- Add security checks to avoid deleting tagged backups
Feel free to fork this project, use it in production, or extend its functionality. If it helped you save on AWS billing — give it a ⭐ on GitHub! Testing trigger

