If you end up with a lot of unused EBS snapshots in your AWS account in the interests of good housekeeping, you will often want to purge these to clean up and save some money.
However, if you have inherited an AWS account full of EBS and you aren’t 100% sure whether it is safe to delete the unattached volumes – you can take the precaution of creating a snapshot of the volume before deleting. Trouble is, it can be a pretty laborious process if you have more than a handful of EBS volumes to snapshot.
This is where python and boto can help you out. The following is a python / boto script that will find all of your volumes in available and unattached state, take a snapshot and then delete them.
It also checks for volumes that are over or equal a certain size, in the example case below that is 100GB. You can change that easily enough by altering the volume_size variable from 100 to your desired size.
#!/usr/bin/python import boto.ec2 import time #minimum size of the volume we want to delete volume_size = 100 #Connect to EC2 ec2 = boto.ec2.connect_to_region('ap-southeast-2') available_volumes = ec2.get_all_volumes(filters={'status': 'available',''AttachmentSet':'None'}) for volume in available_volumes: if volume.size >= volume_size: print 'Volume id: ' + volume.id + ' Volume size: ' + str(volume.size) new_snap = ec2.create_snapshot(volume.id,'snapshot of ' + volume.id) print new_snap.id snap = ec2.get_all_snapshots(snapshot_ids=[new_snap.id])[0] while snap.status != 'completed': time.sleep(2) snap.update() print snap.status ec2.delete_volume(volume.id) |
Comments are closed.