Skip to content

iam-bolla/cloud-cost-optimization

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 

Repository files navigation

AWS Lambda – Automated Snapshot Cleanup

This project demonstrates how to use AWS Lambda with Python (boto3) to automatically clean up unused EC2 snapshots, reducing manual effort and optimizing AWS storage costs.


πŸš€ Project Aim

  • Delete snapshots that are not associated with any volume.
  • Delete snapshots that are associated with a volume but the volume is not attached to any EC2 instance.

πŸ›  Tech Stack

  • AWS Lambda (Serverless Compute)
  • Python 3.x with boto3
  • Amazon EC2 (Snapshots, Volumes, Instances)
  • AWS IAM (Role-based permissions)
  • Amazon CloudWatch (Event trigger)

Fork this repo for later use


βš™οΈ Setup Instructions

1. Create Lambda Function

  • Log in to AWS Console β†’ Lambda β†’ Create function
  • Choose Python 3.x as runtime

2. Configure IAM Role

  • Create a new IAM policy with these permissions:
    • DescribeInstances
    • DescribeVolumes
    • DescribeSnapshots
    • DeleteSnapshot
  • Attach this policy to the Lambda’s execution role

3. Update General Settings

  • Set Timeout = 10 seconds (to avoid unnecessary AWS charges)

4. Add Code

  • Copy code from lambda_function.py in this repo
  • Paste into Lambda code editor β†’ Save β†’ Deploy

5. Test the Function

  • Create an EC2 instance and take a snapshot of its volume
  • Run the Lambda test β†’ Snapshot will remain (since it’s linked to active volume)
Screenshot 2025-09-05 193042
  • Delete the EC2 instance and run Lambda again β†’ Snapshot will be deleted automatically

    Screenshot 2025-09-05 191807

βœ… Expected Output

  • Before deletion: Snapshots exist but are linked to volumes/instances
Screenshot 2025-09-05 192747
  • After deletion: Unused snapshots are automatically removed
Screenshot 2025-09-05 193145

πŸ“Š Key Learning

  • Hands-on with serverless computing
  • Configuring event-driven automation with CloudWatch
  • Securing automation with IAM role-based access
  • Cost optimization by cleaning unused resources

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages