Commit dec2207e authored by Nagarjuna D N's avatar Nagarjuna D N
Browse files

AWS AMI copy and cleanup

parent fc536c7e
#This script copies the AMI to other region and tag copied AMI 'DeleteOnCopy' with retention days specified.
import boto3
import json
from dateutil import parser
import dateutil
import datetime
import collections
#Specify the source region of AMI's created and the destination region to which AMI's to be copied
source_image_resource = boto3.resource('ec2','ap-south-1')
dest_image_client = boto3.client('ec2','ap-southeast-1')
dest_image_resource = boto3.resource('ec2','ap-southeast-1')
#AMI to be retained in the destination region.
ami_retention = 15
def copy_latest_image():
images = source_image_resource.images.filter(Owners=["XXXXX"]) # Specify your AWS account owner id in place of "XXXXX" at all the places in this script
#Retention days in DR region, its for 15 days.
retention_days = int(ami_retention)
to_tag = collections.defaultdict(list)
for image in images:
image_date = parser.parse(image.creation_date)
#Copy todays images
if image_date.date() == (datetime.datetime.today()).date():
#Copy previous day images
#if image_date.date() == (datetime.datetime.today()-datetime.timedelta(1)).date():
if not dest_image_client.describe_images(Owners=['XXXXX',],Filters=[{'Name':'name','Values':[image.name]}])['Images']:
#if not dest_image_client.describe_images(Owners=['XXXXX',])['Images']:
print "Copying Image {name} - {id} to Singapore".format(name=image.name,id=image.id)
new_ami = dest_image_client.copy_image(
DryRun=False,
SourceRegion='ap-south-1',
SourceImageId=image.id,
Name=image.name,
Description=image.description
)
to_tag[retention_days].append(new_ami['ImageId'])
print "New Image Id {new_id} for Mumbai Image {name} - {id}".format(new_id=new_ami,name=image.name,id=image.id)
print "Retaining AMI %s for %d days" % (
new_ami['ImageId'],
retention_days,
)
for ami_retention_days in to_tag.keys():
delete_date = datetime.date.today() + datetime.timedelta(days=retention_days)
delete_fmt = delete_date.strftime('%d-%m-%Y')
print "Will delete %d AMIs on %s" % (len(to_tag[retention_days]), delete_fmt)
#To create a tag to an AMI when it can be deleted after retention period expires
dest_image_client.create_tags(
Resources=to_tag[retention_days],
Tags=[
{'Key': 'DeleteOnCopy', 'Value': delete_fmt},
]
)
else:
print "Image {name} - {id} already present in Singapore Region".format(name=image.name,id=image.id)
def lambda_handler(event, context):
copy_latest_image()
if __name__ == '__main__':
lambda_handler(None, None)
\ No newline at end of file
#This script deregister the AMI and deletes the associated snapshots for the AMI date of "DeleteOnCopy" expired.
import boto3
import collections
import datetime
import time
import sys
#specify the destination region to AMI's copied to cleanup. Ex: Singapore region (ap-southeast-1)
ec = boto3.client('ec2', 'ap-southeast-1')
ec2 = boto3.resource('ec2', 'ap-southeast-1')
images = ec2.images.filter(Owners=["XXXXX"]) # Specify your AWS account owner id in place of "XXXXX" at all the places in this script
def lambda_handler(event, context):
to_tag = collections.defaultdict(list)
date = datetime.datetime.now()
date_fmt = date.strftime('%d-%m-%Y')
print "Today's date and time:" + date.strftime('%d-%m-%Y:%H.%m.%s')
imagesList = []
# Loop through each image of our current instance
for image in images:
try:
if image.tags is not None:
deletion_date = [
t.get('Value') for t in image.tags
if t['Key'] == 'DeleteOnCopy'][0]
delete_date = time.strptime(deletion_date, "%d-%m-%Y")
#print ("deletion_date %s" %delete_date)
#today = datetime.datetime.now()
#today_date = today.strftime('%d-%m-%Y')
today_time = datetime.datetime.now().strftime('%d-%m-%Y')
today_date = time.strptime(today_time, '%d-%m-%Y')
# print ("today_date %s" %today_date)
# If image's DeleteOn date is less than or equal to today,
# add this image to our list of images to process later
if delete_date < today_date:
imagesList.append(image.id)
except IndexError:
deletion_date = False
delete_date = False
print "============="
print "About to process the following AMIs:"
print imagesList
snapshotList = []
# Loop through each image of our current instance
for image in imagesList:
#print image
desc_image_snapshots = ec.describe_images(ImageIds=[image],Owners=['XXXXX',])['Images'][0]['BlockDeviceMappings']
# print (desc_image_snapshots)
try:
for desc_image_snapshot in desc_image_snapshots:
snapshot = ec.describe_snapshots(SnapshotIds=[desc_image_snapshot['Ebs']['SnapshotId'],], OwnerIds=['XXXXX'])['Snapshots'][0]
#if snapshot['Description'].find(image) > 0:
snapshotList.append(snapshot['SnapshotId'])
#else:
# continue
# print "Snapshot is not associated with an AMI"
except Exception as e:
print "Ignore Index Error:%s" % e.message
print "Deregistering image %s" % image
amiResponse = ec.deregister_image(
DryRun=False,
ImageId=image,
)
print "============="
print "About to process the following Snapshots associated with above Images:"
print (snapshotList)
print "The timer is started for 5 seconds to wait for images to deregister before deleting the snapshots associated to it"
time.sleep(5)# This should be set to higher value if the image in the imagesList takes more time to deregister
for snapshot in snapshotList:
try:
snap = ec.delete_snapshot(SnapshotId=snapshot)
print "Deleted snapshot " + snapshot
except Exception as e:
print "%s" % e.message
print "-------------"
"LambdaCleanupCopiedAMI.py" script copies the AMI to other region and tag copied AMI 'DeleteOnCopy' with retention days specified.
"LambdaCleanupCopiedAMI.py" script deregister the AMI and deletes the associated snapshots for the AMI date of "DeleteOnCopy" expired.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment