Skip to content

Instantly share code, notes, and snippets.

@anuragmathur1
Last active December 21, 2017 03:58
Show Gist options
  • Save anuragmathur1/6c0c5667041c1523c3de75ef52f53100 to your computer and use it in GitHub Desktop.
Save anuragmathur1/6c0c5667041c1523c3de75ef52f53100 to your computer and use it in GitHub Desktop.
Delete AMIs and associated snapshots created on a given date
#!/usr/bin/python
import boto3
client = boto3.client('ec2')
account_number = boto3.client('sts').get_caller_identity()['Account']
response = client.describe_images(Owners=[account_number])
for image in response['Images']:
if "2017-08-21" in image['CreationDate']:
image_id = image['ImageId']
snapshot_id = image['BlockDeviceMappings'][0]['Ebs']['SnapshotId']
print image['CreationDate']
print image_id, " To be deleted"
print snapshot_id, " To be deleted"
response = client.deregister_image(ImageId=image_id)
print response
response = client.delete_snapshot(SnapshotId=snapshot_id)
print response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment