【问题标题】:How to get number of vCPU in AWS boto python EC2 instance?如何获取 AWS boto python EC2 实例中的 vCPU 数量?
【发布时间】:2020-02-13 18:54:50
【问题描述】:

有没有办法通过 boto python api 获取 EC2 实例中的 vCPU(核心)的数量?

到目前为止我的代码是:

在 boto3 中找到它:

import boto3     
ec2 = boto3.resource('ec2', region_name=REGION_NAME)
instance = ec2.Instance(id)
instance.cpu_options.get(u'CoreCount','1')

返回的是我的物理核心数,但是通过 htop 我可以看到我的 VCPU 是两倍,总是这样吗?也许它与 ThreadsPerCore 的数量有关?

【问题讨论】:

  • 获取实例类型,然后关注docs.aws.amazon.com/AWSEC2/latest/UserGuide/…
  • 只能获取实例类型。 type 是一些可以用来识别 vcpu 信息等的东西
  • 你是对的,在boto3中是可能的
  • 旁注:现在,您应该使用boto3 而不是原来的boto。旧版本无法访问最新服务。

标签: python amazon-web-services amazon-ec2 aws-sdk boto


【解决方案1】:

我是这样做的,需要最新版本的boto3:

import boto3
# You may have to update boto3 to get this to work, describe_instance_types is quite new:
# pip install boto3 --upgrade
ec2 = boto3.client('ec2')

reservations = ec2.describe_instances()['Reservations']
vcpus = 0
for reservation in reservations:
    for instance in reservation['Instances']:
        instance_id = instance['InstanceId']
        instance_type_name = instance['InstanceType']
        instance_type = ec2.describe_instance_types(InstanceTypes=[instance_type_name])
        current_vcpu_count = instance_type['InstanceTypes'][0]['VCpuInfo']['DefaultVCpus']
        print(f'{instance_id} - {instance_type_name} - {current_vcpu_count}')
        vcpus += current_vcpu_count

print(vcpus)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-24
    • 1970-01-01
    相关资源
    最近更新 更多