Created
May 19, 2015 04:38
-
-
Save murarisumit/b590addb54c2f615bd0f to your computer and use it in GitHub Desktop.
AWS boto delete AMI after specific Days.
This file contains hidden or 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
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great! Thank you.