Commit 5d1accc9 authored by Nagarjuna D N's avatar Nagarjuna D N
Browse files


parent 316f5c10
Pipeline #10939372 passed with stage
in 0 seconds
#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_region = 'ap-south-1'
source_image_resource = boto3.resource('ec2',source_region)
dest_image_client = boto3.client('ec2','ap-southeast-1')
dest_image_resource = boto3.resource('ec2','ap-southeast-1')
#AMI to be retained for the number of days 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 == (
#To Copy previous day images
#if == (
if not dest_image_client.describe_images(Owners=['XXXXX',],Filters=[{'Name':'name','Values':[]}])['Images']:
#if not dest_image_client.describe_images(Owners=['XXXXX',])['Images']:
print "Copying Image {name} - {id} to Singapore".format(,
new_ami = dest_image_client.copy_image(
print "New Image Id {new_id} for Mumbai Image {name} - {id}".format(new_id=new_ami,,
print "Retaining AMI %s for %d days" % (
for ami_retention_days in to_tag.keys():
delete_date = + 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
{'Key': 'DeleteOnCopy', 'Value': delete_fmt},
print "Image {name} - {id} already present in Singapore Region".format(,
def lambda_handler(event, context):
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 =
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:
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 =
#today_date = today.strftime('%d-%m-%Y')
today_time ='%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:
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)
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:
# 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(
#print "For testing, commented ami de-register"
except Exception as e:
print "%s" % e.message
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:
snap = ec.delete_snapshot(SnapshotId=snapshot)
print "Deleted snapshot " + snapshot
except Exception as e:
print "%s" % e.message
print "-------------"
# LambdaAMICopyAcrossRegion
• This script will first search for the AMI created for the date specified in the source AWS region (Ex: Mumbai (ap-south-1)).
• As soon as it loops the AMI, it checks for the same AMI already exists in the destination AWS region (Ex: Singapore (ap-southeast-1)).
• If the AMI doesn’t exist or already copied then it copies AMI to the destination region.
• After copying the AMI it creates a "DeleteOnCopy" tag on the AMI-indicating when it will be deleted using the Retention value and another Lambda function.
# LambdaCleanupCopiedAMI
• It checks and stores the every image that's reached its “DeleteOnCopy” tag's date for deletion.
• It then loops through the AMIs, de-registers them and removes all the snapshots associated with those AMI.
# Important Note:
Please specify your AWS Account Number in the place of "XXXXX" where ever in the code
\ No newline at end of file
AMIRegionCopy @ ccfe55c5
Subproject commit ccfe55c566d9fc8e52b1bb5359e5892e7f76f8e5
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