Skip to content

Instantly share code, notes, and snippets.

@bkozora
Last active November 26, 2020 13:13
Show Gist options
  • Save bkozora/819e88ad3544ee2bb2f5 to your computer and use it in GitHub Desktop.
Save bkozora/819e88ad3544ee2bb2f5 to your computer and use it in GitHub Desktop.
Creates AWS AMIs for multiple configured EC2 instance IDs
#!/usr/bin/python
#
# This script will create an AMI for each of the instances listed in the
# instance_ids list. It will also clean up AMIs based on the images_to_keep
# variable. The cleanup logic has no concept of time so the images are just
# deleted based on number. It is recommended to run the script daily from
# cron during off peak time and by default keep 7 days.
#
import boto, boto.ec2
import datetime
import time
#Number of images to keep
images_to_keep = 7
b = boto.ec2.connect_to_region("us-east-1")
#Get instance ID from metadata
metadata = boto.utils.get_instance_metadata()
#instance_id = metadata['instance-id']
instance_ids = ['i-xxxxxxxx','i-xxxxxxxx','i-xxxxxxxx']
#Get instance Name tag
instance_reservation = b.get_all_instances( instance_ids )
for reservation in instance_reservation:
for instance in reservation.instances:
#Get Current Date
date = str( datetime.datetime.now() )
instance_name = instance.tags['Name']
instance_id = instance.id
AMI_name = instance_id + "_" + date
#Replace : with - in the timestamp portion of the AMI Name
AMI_name = AMI_name.replace(":", "-")
print "Creating AMI with these properties:"
print " AMI Name: " + AMI_name
print " Name Tag: " + instance_name
print " Instance ID Tag: " + instance_id
print " Date Tag: " + date
#break
#sys.exit()
#Create the AMI
image_id = b.create_image(instance_id,AMI_name,no_reboot=True)
#Tag the AMI with the instance name tag, id, and date
new_image = b.get_image(image_id)
new_image.add_tag("Name", instance_name)
new_image.add_tag("Instance", instance_id)
new_image.add_tag("Date", date)
print "AMI requested (" + str(image_id) + "), waiting for state to change to available."
#Wait for image to become available
while b.get_image(image_id).state != "available":
#print " current state: " + str(b.get_image(image_id).state)
time.sleep(5)
print "AMI is available."
############################
# Clean up old images
############################
print "Cleaning up old images."
for reservation in instance_reservation:
for instance in reservation.instances:
instance_name = instance.tags['Name']
instance_id = instance.id
#Get list of images for instance ID tag
images = b.get_all_images(owners=['self'], filters={ "tag:Instance" : instance_id })
#Sort function
def getImageDate(image):
return image.tags.get("Date")
images.sort(key=getImageDate, reverse = True)
for image in images:
nametag = image.tags.get("Name")
idtag = image.tags.get("Instance")
datetag = image.tags.get("Date")
for i in range(images_to_keep, len(images)):
print "Deleting " + " " + str(images[i]) + " : " + str(images[i].tags.get("Date"))
b.deregister_image(images[i].id, delete_snapshot=True)
@skpandey8123
Copy link

b.deregister_image(images[i].id, delete_snapshot=True)

delete_snapshot=True is not available in boto3. Any suggestions?

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