如何获取Amazon EC2上可用实例类型的列表? [英] How to get a list of available instance types on Amazon EC2?

查看:140
本文介绍了如何获取Amazon EC2上可用实例类型的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个API可以获取EC2上可用实例类型的列表?

Is there an API to get a list of available instance types on EC2?

我可以看到有一个价格列表JSON文件可以下载,但是它是70兆字节,将其作为请求包含在应用程序中并不实际。

I can see that there is a price list JSON file that you can download but it's 70 megabytes which is not that practical to include as a request in an application.

推荐答案

此信息可作为EC2的一部分获得API。该API定义了各种语言形状,这些语言可以由特定于语言的SDK公开。例如,Python库 botocore (它也为AWS CLI提供了支持)通过EC2服务模型公开实例类型。 :

This information is available as part of the EC2 API. The API defines various resource shapes which can be exposed by language-specific SDKs. For example, the Python library botocore (which also powers the AWS CLI) exposes instance types via the EC2 service model:

import botocore.session

sess = botocore.session.Session()
available_types = sess.get_service_model('ec2').shape_for('InstanceType').enum

如果正在使用Python的EC2,很有可能您正在使用更高级别的 boto3 库。 boto3 EC2客户端已经具有对基础botocore服务模式的引用,因此您可以改为这样做:

If you're working with EC2 from Python, there's a good chance you're working with the higher level boto3 library. A boto3 EC2 client will already have a reference to the underlying botocore service mode, so you can do this instead:

client = boto3.client('ec2')
client._service_model.shape_for('InstanceType').enum

其他语言的SDK还应公开 InstanceType 资源形状。由于这直接来自API,并且往往会自动推送到SDK存储库,因此只要您使用的是相同的API版本,数据就应该是通用的(比较 botocore JavaScript SDK

SDKs for other languages should also expose the InstanceType resource shape. Since this comes directly from the API and tends to be automatically pushed to the SDK repos, the data should be common as long as you're using the same API version (compare the JSON API definition in botocore and the JavaScript SDK, for instance).

这篇关于如何获取Amazon EC2上可用实例类型的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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