Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philroche/478b73a1230d9dd2c5f698a8dd082f8c to your computer and use it in GitHub Desktop.
Save philroche/478b73a1230d9dd2c5f698a8dd082f8c to your computer and use it in GitHub Desktop.
Play with AWS EC2 describe_instance_types
"""
Play with AWS EC2 describe_instance_types
"""
from boto3.session import Session
sess = Session()
ec2c = sess.client('ec2')
resp = ec2c.describe_instance_types()
# Gather all instance types from paginated results
instance_types = resp['InstanceTypes']
next = resp['NextToken'] if 'NextToken' in resp else None
while next:
resp = ec2c.describe_instance_types(NextToken=next)
instance_types.extend(resp['InstanceTypes'])
next = resp['NextToken'] if 'NextToken' in resp else None
# What are our key for an instance type? (they can differ however)
print(instance_types[0].keys())
# Sort the instance types by InstanceType name
instance_types = sorted(instance_types, key=lambda i: i['InstanceType'])
for i in instance_types:
print(i['InstanceType'])
# Find the full set of instance families
# The family is the portion before the period e.g. m3 for m3.medium
instance_families = set()
for i in instance_types:
# We're going to ignore i386 and arm64 today
if 'x86_64' in i['ProcessorInfo']['SupportedArchitectures']:
instance_families.add(i['InstanceType'].split('.')[0])
# Create a dictionary of famliies with a list of instances for each
instance_families = {i: [] for i in instance_families}
for i in instance_types:
family = i['InstanceType'].split('.')[0]
if family in instance_families:
instance_families[family].append(i)
# DEBUG: What is the full set of sizes today?
sizes = set()
for i in instance_types:
sizes.add(i['InstanceType'].split('.')[1])
# Convert instance sizes to a numeric value for sorting
def size_convert(a):
size_map = {
'nano': 1,
'micro': 2,
'small': 3,
'medium': 4,
'large': 5,
'xlarge': 10,
'metal': 100,
}
a = a.split('.')[-1]
if 'xlarge' not in a:
# Simple name to value mapping
a_score = size_map[a]
else: # Handle xlarge with possible numeric prefix
# Use 'xlarge' as a starting point
a_score = size_map['xlarge']
# For #xlarge add the # to the score
prefix = a.replace('xlarge', '')
if prefix.isnumeric():
a_score += int(prefix)
return a_score
# Sort lists of instance types in each family by size
for family in instance_families:
instance_families[family].sort(key=lambda i:
size_convert(i['InstanceType']))
# Print the smallest instance type for each family
for family in sorted(instance_families):
print(instance_families[family][0]['InstanceType'])
# DEBUG: What does c1.medium look like?
print(instance_families['c1'][0])
# Print each family and whether it is considered a Current Generation family
for family in sorted(instance_families):
instance = instance_families[family][0]
print(f'{instance["InstanceType"]}\t{instance["CurrentGeneration"]}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment