Skip to content

Instantly share code, notes, and snippets.

@robperc
Created October 31, 2018 19:48
Show Gist options
  • Save robperc/e83bf085cc213bbad1e563b79b72b4c2 to your computer and use it in GitHub Desktop.
Save robperc/e83bf085cc213bbad1e563b79b72b4c2 to your computer and use it in GitHub Desktop.
Lists, snapshots, then deletes unattached EBS volumes. If NO_OP is set to True then only lists unattached EBS volumes.
import boto3
NO_OP = True
REGION_NAME = 'us-west-2'
ec2r = boto3.resource('ec2', region_name=REGION_NAME)
ec2c = boto3.client('ec2', region_name=REGION_NAME)
vols = ec2r.volumes.filter(Filters=[{'Name': 'status', 'Values': ['available']}])
for vol in vols:
print("Found unattached volume with id: {}".format(vol.id))
if not NO_OP:
print("Snapshotting volume...")
ec2c.create_snapshot(VolumeId=vol.id)
print("Deleting volume...")
ec2c.delete_volume(VolumeId=vol.id)
print("Proceeding to next volume...")
print("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment