Created
September 2, 2015 10:37
-
-
Save jacoelho/47a1677c62f08a783c03 to your computer and use it in GitHub Desktop.
ubuntu ami locator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import requests | |
import argparse | |
CLOUD_IMAGES_URL="https://cloud-images.ubuntu.com/releases/streams/v1/com.ubuntu.cloud:released:aws.json" | |
def ubuntu_ami(region, version, arch, storage, virt): | |
req = requests.get(CLOUD_IMAGES_URL) | |
if req.status_code != 200: | |
return None | |
output = req.json() | |
images = output['products']['com.ubuntu.cloud:server:{}:{}'.format(version, arch)]['versions'] | |
latest = images[sorted(images.keys(), reverse = True)[0]]['items'] | |
match = list(filter(lambda x: (latest[x]['root_store'] == storage and latest[x]['crsn'] == region | |
and latest[x]['virt'] == virt), latest)) | |
return latest[match[0]]['id'] | |
parser = argparse.ArgumentParser() | |
parser.add_argument("--region", default="us-west-1", | |
choices=["us-west-1","us-west-2","us-east-1","eu-west-1","eu-central-1"], help="aws region") | |
parser.add_argument("--release", default="14.04", | |
choices=["12.04","14.04", "14.10", "15.04"], help="ubuntu release") | |
parser.add_argument("--storage", default="ebs", | |
choices=["ebs", "ssd"], help="aws storage type") | |
parser.add_argument("--virt", default="hvm", | |
choices=["hvm", "pv"], help="aws virt type") | |
args = parser.parse_args() | |
print(ubuntu_ami(args.region, args.release, 'amd64', args.storage, args.virt)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment