现在是否可以在boto3中获取“ AWS区域名称”?

时间:2019-04-29 18:57:55

标签: amazon-web-services boto3

boto3中现在有一种方法可以将 AWS区域代码转换为 AWS区域名称,例如将('us-west-1', 'us-east-1', 'us-west-2')转换为{{1 }}?

我可以使用以下代码段获取 AWS地区代码的列表:

('N. California', 'N. Virginia', 'Oregon')

是否存在等效的代码段,可以给我 AWS区域名称?

3 个答案:

答案 0 :(得分:5)

AWS刚刚发布了一项允许query for AWS Regions, Endpoints, and More Using AWS Systems Manager Parameter Store的功能。

使用此功能和boto3,您可以执行以下操作:

import boto3

client = boto3.client('ssm')

response = client.get_parameter(
    Name='/aws/service/global-infrastructure/regions/us-west-1/longName'
)

region_name = response['Parameter']['Value'] # US West (N. California)

要获取所有可用区域,您可以首先使用get_parameters_by_path()并使用以下路径/aws/service/global-infrastructure/regions

注意:即使这是公共数据,也需要适当的IAM权限。

答案 1 :(得分:1)

这是我到达的解决方案,其中显示了所有AWS / EC2区域ID和名称:

$ cat so_regions2.py
import boto3

region = "us-east-1"
print("Using region:", region)

ec2 = boto3.client("ec2", region_name=region)
ec2_responses = ec2.describe_regions()
ssm_client = boto3.client('ssm', region_name=region)
for resp in ec2_responses['Regions']:
    region_id = resp['RegionName']
    tmp = '/aws/service/global-infrastructure/regions/%s/longName' % region_id
    ssm_response = ssm_client.get_parameter(Name = tmp)
    region_name = ssm_response['Parameter']['Value'] 
    print ("region_id:",region_id,"region_name:",region_name)

$ python3 so_regions2.py
Using region: us-east-1
region_id: eu-north-1 region_name: EU (Stockholm)
region_id: ap-south-1 region_name: Asia Pacific (Mumbai)
region_id: eu-west-3 region_name: EU (Paris)
region_id: eu-west-2 region_name: EU (London)
region_id: eu-west-1 region_name: EU (Ireland)
region_id: ap-northeast-2 region_name: Asia Pacific (Seoul)
region_id: ap-northeast-1 region_name: Asia Pacific (Tokyo)
region_id: sa-east-1 region_name: South America (Sao Paulo)
region_id: ca-central-1 region_name: Canada (Central)
region_id: ap-southeast-1 region_name: Asia Pacific (Singapore)
region_id: ap-southeast-2 region_name: Asia Pacific (Sydney)
region_id: eu-central-1 region_name: EU (Frankfurt)
region_id: us-east-1 region_name: US East (N. Virginia)
region_id: us-east-2 region_name: US East (Ohio)
region_id: us-west-1 region_name: US West (N. California)
region_id: us-west-2 region_name: US West (Oregon)
$ 

答案 2 :(得分:0)

编辑:正如@jogold所提到的,随着Query for AWS Systems Manager参数存储的最新启动,我建议使用它直接从AWS查询而不是答案中的自定义脚本。


根据boto3 docs,没有用于描述区域俗称的本地功能。

这是一个带有函数convertRegionCodesToNames()的小脚本,该脚本获取有效区域ID的列表并将其转换为通用名称。根据需要添加错误处理,以处理无效输入,零长度数组或boto3返回的其他可能响应。

# replace `regions` variable with the output from the get_available_instances() response

regions = ['ap-northeast-1', 'ap-northeast-2', 'ap-south-1', 'ap-southeast-1', 'ap-southeast-2', 'ca-central-1', 'eu-central-1', 'eu-west-1', 'eu-west-2', 'eu-west-3', 'sa-east-1', 'us-east-1', 'us-east-2', 'us-west-1', 'us-west-2']

def convertRegionCodesToNames(regions):
    # static dict of all region key-value pairs
    region_dict = {
      "us-east-1": "N. Virginia",
      "us-east-2": "Ohio",
      "us-west-1": "N. California",
      "us-west-2": "Oregon",
      "ca-central-1": "Central",
      "eu-west-1": "Ireland",
      "eu-central-1": "Frankfurt",
      "eu-west-2": "London",
      "eu-west-3": "Paris",
      "eu-north-1": "Stockholm",
      "ap-northeast-1": "Tokyo",
      "ap-northeast-2": "Seoul",
      "ap-southeast-1": "Singapore",
      "ap-southeast-2": "Sydney",
      "ap-south-1": "Mumbai",
      "sa-east-1": "São Paulo",
      "us-gov-west-1": "US Gov West 1",
      "us-gov-east-1": "US Gov East 1"
    } 

    for i in range(len(regions)):
        regions[i] = region_dict[regions[i]]

    return regions

converted_regions = convertRegionCodesToNames(regions)
print("regions:", converted_regions) 

添加后,运行$ python3 regions.py将输出:

regions: ['Tokyo', 'Seoul', 'Mumbai', 'Singapore', 'Sydney', 'Central', 'Frankfurt', 'Ireland', 'London', 'Paris', 'São Paulo', 'N. Virginia', 'Ohio', 'N. California', 'Oregon']