代码之家  ›  专栏  ›  技术社区  ›  Yaroslav Bulatov

如何获取对给定实例类型有效的区域列表?

  •  0
  • Yaroslav Bulatov  · 技术社区  · 6 年前

    有些情况下需要启动到特定区域,但并非所有实例都在所有区域中。特别地 p3dn.24xlarge 在弗吉尼亚州只有两个地区有这样的例子。

    enter image description here

    0 回复  |  直到 6 年前
        1
  •  2
  •   Allen Luce Bastian Ballmann    6 年前

    此代码段提供了支持实例点类型的AZs列表(以及价格):

    import boto3
    
    instanceType = 'p3dn.24xlarge'
    product = 'Linux/UNIX (Amazon VPC)'
    
    for region in boto3.client('ec2').describe_regions()['Regions']:
        client = boto3.client('ec2', region_name=region['RegionName'])
        for zone in [z['ZoneName'] for z in client.describe_availability_zones()['AvailabilityZones'] if z['State'] == 'available']:
            try:
                price = client.describe_spot_price_history(InstanceTypes=[instanceType],
                                                           MaxResults=1,
                                                           ProductDescriptions=[product],
                                                           AvailabilityZone=zone)['SpotPriceHistory'][0]['SpotPrice']
                print("%s: %s" % (zone, price))
            except IndexError: pass
    
    推荐文章