Skip to content

Instantly share code, notes, and snippets.

@murarisumit
Created May 19, 2015 04:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save murarisumit/b590addb54c2f615bd0f to your computer and use it in GitHub Desktop.
Save murarisumit/b590addb54c2f615bd0f to your computer and use it in GitHub Desktop.
AWS boto delete AMI after specific Days.
import boto
import boto.ec2
import sys
import time
import os
import datetime
from datetime import date
#Filter used to filter my instances
vEnvironment = "Prod"
vType = "DB"
vLogging = "Log"
#To Test the job, without making any changes. True: Willn't make any changes
#Command line paramamter.
vdryRun = sys.argv[1]
#Retention time for keeping the image. I've kept it for 7 days
vRetentionTime = 7
conn = boto.connect_ec2(os.environ['AWSAccessKeyId'],os.environ['AWSSecretKey'])
images = conn.get_all_images(filters={"tag:Environment":vEnvironment,"tag:Type": vType, "tag:Purpose": vLogging})
for image in images:
creation_date = image.creationDate.split('T')[0].split('-')
split_time = (date.today() - date(int(creation_date[0]), int(creation_date[1]), int(creation_date[2]))).days
if split_time > vRetentionTime :
print image.id+ " : is going to be deregistered"
print "Split_time is : "+str(split_time)
if vdryRun == "false":
image.deregister()
sys.stdout.flush()
@juliosmelo
Copy link

Great! Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment