boto3 aws api-列出可用的实例类型 [英] boto3 aws api - Listing available instance types

查看:353
本文介绍了boto3 aws api-列出可用的实例类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实例类型:(t2.micro,t2.small,c4.large ...)此处列出的实例类型: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html

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

我想通过boto3访问这些列表. 像这样:

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

conn.get_all_instance_types()

甚至

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

在这个怪异的api中一切看起来都像这样.

which everything seems to look like in this weird api.

我已经查看了有关client和ServiceResource的文档,但找不到任何似乎接近的东西. 我什至还没有找到一种骇人听闻的解决方案,该解决方案列出了恰好代表所有实例类型的其他内容.

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.

有人对boto3有更多的经验吗?

Anyone with more experience of boto3?

推荐答案

现在有boto3.client('ec2').describe_instance_types()和相应的aws-cli命令aws ec2 describe-instance-types:

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)

大约需要3秒钟的运行时间.

Expect about 3s of running time.

这篇关于boto3 aws api-列出可用的实例类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆