amazon-web-services - 如何通过CLI获取基于Nitro系统的EC2实例类型列表?

标签 amazon-web-services amazon-ec2

我知道this page列出了基于 Nitro 系统的实例类型,但我想使用 CLI 以动态方式了解该列表。 (例如,使用aws ec2描述实例)。除了解析静态页面之外,是否可以获得基于 Nitro 的实例类型?如果是的话,你能告诉我怎么做吗?

最佳答案

您必须编写一些额外的代码才能获取该信息。 aws ec2 describe-instances 将为您提供 InstanceType 属性。您应该使用编程语言来解析 JSON,提取 InstanceType,然后调用 describe-instances,如下所示: https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instance-types.html?highlight=nitro

从您返回的 JSON 中提取虚拟机管理程序。如果实例是 Nitro,这将为您提供 Nitro。

这是一个可能有效的 Python 代码。我尚未对其进行全面测试,但您可以对其进行调整以获得您想要的结果。

"""List all EC2 instances"""
import boto3

def ec2_connection():
    """Connect to AWS using API"""

    region = 'us-east-2'

    aws_key = 'xxx'
    aws_secret = 'xxx'

    session = boto3.Session(
        aws_access_key_id = aws_key,
        aws_secret_access_key = aws_secret
    )

    ec2 = session.client('ec2', region_name = region)

    return ec2


def get_reservations(ec2):
    """Get a list of instances as a dictionary"""

    response = ec2.describe_instances()

    return response['Reservations']


def process_instances(reservations, ec2):
    """Print a colorful list of IPs and instances"""

    if len(reservations) == 0:
        print('No instance found. Quitting')
        return

    for reservation in reservations:
        for instance in reservation['Instances']:

            # get friendly name of the server
            # only try this for mysql1.local server
            friendly_name = get_friendly_name(instance)
            if friendly_name.lower() != 'mysql1.local':
                continue

            # get the hypervisor based on the instance type
            instance_type = get_instance_info(instance['InstanceType'], ec2)

            # print findings
            print(f'{friendly_name} // {instance["InstanceType"]} is {instance_type}')
            break


def get_instance_info(instance_type, ec2):
    """Get hypervisor from the instance type"""

    response = ec2.describe_instance_types(
        InstanceTypes=[instance_type]
    )

    return response['InstanceTypes'][0]['Hypervisor']


def get_friendly_name(instance):
    """Get friendly name of the instance"""

    tags = instance['Tags']
    for tag in tags:
        if tag['Key'] == 'Name':
            return tag['Value']

    return 'Unknown'


def run():
    """Main method to call"""

    ec2 = ec2_connection()
    reservations = get_reservations(ec2)
    process_instances(reservations, ec2)


if __name__ == '__main__':

    run()
    print('Done')

关于amazon-web-services - 如何通过CLI获取基于Nitro系统的EC2实例类型列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69967812/

相关文章:

amazon-web-services - 亚马逊 RDS : Backup and restore into new database on existing DB instance

web-applications - Tomcat6 webapp 未部署在亚马逊 Ec2 上

R 尽管内存似乎可用,但无法分配内存

amazon-web-services - Cloudfront 分配如何使用 AWS KMS key 来获取静态加密的 S3 镜像?

amazon-web-services - 使用 Amazon AWS 按价格排序

node.js - 如何解析微服务世界中书籍的作者姓名?

php - 无法在新服务器上执行 Ajax 查询

amazon-web-services - 如何在aws堆栈中添加多个ec2实例

amazon-ec2 - 如何以编程方式在正在运行的 Ubuntu EC2 计算机中更新应用程序的新版本

amazon-ec2 - Amazon EC2 和 Linode 之间的性能差异