A serverless solution for managing AWS EBS snapshots, implementing automated cleanup and cost optimization.
- Automated EBS snapshot cleanup based on retention policies
- AMI dependency checking to prevent deletion of in-use snapshots
- SNS notifications for operation reports
- Comprehensive logging and error handling
- Resource tagging for audit trails
- Clone the repository:
git clone https://github.com/yourusername/snapshot-manager.git
cd snapshot-manager
- Create a virtual environment and install dependencies:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -r requirements.txt
- Configure AWS credentials:
- Set up AWS credentials using AWS CLI or environment variables
- Ensure proper IAM roles are configured for Lambda execution
- Set up environment variables:
SNS_TOPIC_ARN
: ARN of the SNS topic for notificationsRETENTION_DAYS
: Number of days to retain snapshots (default: 30)
Run tests using pytest:
pytest tests/
- Package the application:
zip -r function.zip src/ requirements.txt
- Deploy to AWS Lambda:
- Create a new Lambda function
- Upload the function.zip package
- Configure environment variables
- Set the handler to
src.handlers.lambda_handler
- Configure appropriate IAM roles and permissions
The Lambda function requires the following permissions:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ec2:DescribeSnapshots",
"ec2:DeleteSnapshot",
"ec2:DescribeInstances",
"ec2:DescribeVolumes",
"ec2:CreateTags",
"ec2:DescribeImages",
"sns:Publish"
],
"Resource": "*"
}
]
}
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request