Last active
December 21, 2017 03:58
-
-
Save anuragmathur1/6c0c5667041c1523c3de75ef52f53100 to your computer and use it in GitHub Desktop.
Delete AMIs and associated snapshots created on a given date
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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