Skip to content

Instantly share code, notes, and snippets.

@robert-mcdermott
Created July 12, 2016 20:52
Show Gist options
  • Save robert-mcdermott/a9901aaafe208a6eb76e0fc3b9fc47c9 to your computer and use it in GitHub Desktop.
Save robert-mcdermott/a9901aaafe208a6eb76e0fc3b9fc47c9 to your computer and use it in GitHub Desktop.
Find AMI image ID of latest Amazon Windows 2012R2 image
def find_image(region):
"""
Finds the latest Windows 2012R2 offical Amazon AMI and returns the ID
"""
sys.stdout.write("\nFinding latest Windows 2012R2 image...")
ec2 = boto3.resource('ec2', region_name = "{0}".format(region))
images = ec2.images.filter(
Owners=['amazon'],
Filters=[
{'Name': 'name','Values': ['Windows_Server-2012-R2_RTM-English-64Bit-Base-*']},
{'Name': 'state', 'Values': ['available']},
{'Name': 'architecture', 'Values': ['x86_64']},
{'Name': 'root-device-type', 'Values': ['ebs']},
{'Name': 'virtualization-type', 'Values': ['hvm']},
{'Name': 'platform', 'Values': ['windows']},
]
)
candidates = {}
for image in images:
candidates[image.creation_date] = image.image_id
cDate = sorted(candidates.keys(), reverse=True)[0]
ami = candidates[cDate]
print(" Selected {0}".format(ami))
return(ami)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment