boto3 aws api - Listing available instance types

Oliver picture Oliver · Oct 14, 2015 · Viewed 11.1k times · Source

Instance types: (t2.micro, t2.small, c4.large...) those listed here: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html

I want to access a list of these through boto3. something like:

conn.get_all_instance_types()

or even

conn.describe_instance_types()['InstanceTypes'][0]['Name']

which everything seems to look like in this weird api.

I've looked through the docs for client and ServiceResource, but i can't find anything that seems to come close. I haven't even found a hacky solution that lists something else that happen to represent all the instance types.

Anyone with more experience of boto3?

Answer

Tometzky picture Tometzky · Jan 27, 2020

There's now boto3.client('ec2').describe_instance_types() and corresponding aws-cli command aws ec2 describe-instance-types:

'''EC2 describe_instance_types usage example'''

import boto3

def ec2_instance_types(region_name):
    '''Yield all available EC2 instance types in region <region_name>'''
    ec2 = boto3.client('ec2', region_name=region_name)
    describe_args = {}
    while True:
        describe_result = ec2.describe_instance_types(**describe_args)
        yield from [i['InstanceType'] for i in describe_result['InstanceTypes']]
        if 'NextToken' not in describe_result:
            break
        describe_args['NextToken'] = describe_result['NextToken']

for ec2_type in ec2_instance_types('us-east-1'):
    print(ec2_type)

Expect about 3s of running time.