Skip to content

Instantly share code, notes, and snippets.

@deinarson
Last active February 12, 2016 12:32
Show Gist options
  • Save deinarson/f532fe91e6c2cec874b3 to your computer and use it in GitHub Desktop.
Save deinarson/f532fe91e6c2cec874b3 to your computer and use it in GitHub Desktop.
I have needed this for a while but I would have thought someone would have done this by now, yet I have never seen any code that does this. Get the latest AMI. -- In this case we are hard coding everything but, merely change the `parse` and `filters` phrases, and you can grep for the latest on what you want.
#!/usr/bin/env python
import boto
import sys
from parse import *
import datetime
ec2 = boto.connect_ec2(debug=0)
image_meta = ec2.get_all_images(filters={'name': 'amzn-ami-hvm-*gp2'})
date_list = []
number_images = 0
while number_images != len(image_meta):
image_name = parse('amzn-ami-hvm-{}.x86_64-gp2',
image_meta[number_images].name)[0]
date_list.append(image_name)
latest_image = sorted(date_list, reverse=True)[0]
number_images += 1
if latest_image == image_name:
image = image_meta[number_images].id
print image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment