This Python script helps manage AWS Elastic Block Store (EBS) snapshots and volumes. It provides functionalities to find unused snapshots and volumes, generate reports, and clean up resources interactively.
- Generate a CSV report of all EBS snapshots
- Interactively clean up unreferenced snapshots
- Find and delete unused EBS volumes
- Delete individual snapshots by ID
- Multi-region support
- Python 3.x
- AWS account and credentials
- Boto3 library
- Click library
-
Clone this repository or download the
snapshots.pyfile. -
Install required Python packages:
pip install boto3 click -
Ensure you have the AWS CLI installed and configured (see AWS CLI Setup section below).
Open snapshots.py and modify the regions list to include the AWS regions you want to manage:
regions = ['us-west-1', 'eu-central-1'] # Change this to your regionsMake the script executable (Unix-like systems):
chmod +x snapshots.py
-
Generate a snapshot report:
./snapshots.py snapshot-reportThis creates a
report.csvfile with details of all snapshots. -
Interactively clean up snapshots:
./snapshots.py snapshot-cleanupThis command will show you snapshots that might be unused and ask if you want to delete them.
-
Clean up unused volumes:
./snapshots.py volume-cleanupThis finds and allows you to delete unused EBS volumes.
-
Delete a specific snapshot:
./snapshots.py snapshot-delete <snapshot-id>Replace
<snapshot-id>with the ID of the snapshot you want to delete. -
Show help:
./snapshots.py --helpThis displays all available commands and their descriptions.
./snapshots.py --help
Usage: snapshots.py [OPTIONS] COMMAND [ARGS]...
Helper commands for Snapshots management.
Options:
--help Show this message and exit.
Commands:
snapshot-cleanup Find and delete unreferenced snapshots.
snapshot-delete Delete single snapshot by id.
snapshot-report Find unreferenced snapshots.
volume-cleanup Find and delete unused volumes.
To use this script, you need to have the AWS CLI installed and configured. Here's how to set it up:
-
Install the AWS CLI:
- On macOS or Linux, use pip:
pip install awscli - On Windows, download and run the AWS CLI MSI installer.
- On macOS or Linux, use pip:
-
Configure AWS CLI: Run the following command and follow the prompts:
aws configureYou'll need to provide:
- AWS Access Key ID
- AWS Secret Access Key
- Default region name
- Default output format (json recommended)
-
Verify the configuration:
aws sts get-caller-identityThis should return your AWS account information.
- This script performs deletions of AWS resources. Use with caution.
- Ensure your AWS credentials have appropriate permissions.
- Always review the resources before confirming deletion.
This script is provided as-is. Always test in a non-production environment first.