现在有办法在boto3中获取RDS的所有“AWS区域名称”吗?

问题描述 投票:0回答:1

我正在尝试使用boto3获取RDS的所有AWS区域名称。 但是,似乎我得到的答案取决于我查询的区域。

我的脚本是:

$ cat so_regions2.py                                                                                                                                                         
import boto3

region = "us-west-1"
region = "us-east-1"

print("Using region:", region)
rds = boto3.setup_default_session(region_name=region)
client = boto3.client("rds")
rds_responses = client.describe_source_regions()['SourceRegions']

ssm_client = boto3.client('ssm', region_name=region)
for resp in rds_responses:
    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: ap-northeast-1 region_name: Asia Pacific (Tokyo)
region_id: ap-northeast-2 region_name: Asia Pacific (Seoul)
region_id: ap-south-1 region_name: Asia Pacific (Mumbai)
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: eu-west-1 region_name: EU (Ireland)
region_id: eu-west-2 region_name: EU (London)
region_id: sa-east-1 region_name: South America (Sao Paulo)
region_id: us-west-1 region_name: US West (N. California)
region_id: us-west-2 region_name: US West (Oregon)

但是,如果我更改区域名称,我会得到不同的结果(注意最后一行但是一行):

$ python3 so_regions2.py
Using region: us-west-1
region_id: ap-northeast-1 region_name: Asia Pacific (Tokyo)
region_id: ap-northeast-2 region_name: Asia Pacific (Seoul)
region_id: ap-south-1 region_name: Asia Pacific (Mumbai)
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: eu-west-1 region_name: EU (Ireland)
region_id: eu-west-2 region_name: EU (London)
region_id: sa-east-1 region_name: South America (Sao Paulo)
region_id: us-east-1 region_name: US East (N. Virginia)
region_id: us-west-2 region_name: US West (Oregon)

$ python3 so_regions2.py
Using region: ap-northeast-1
region_id: ap-northeast-2 region_name: Asia Pacific (Seoul)
region_id: ap-south-1 region_name: Asia Pacific (Mumbai)
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: eu-west-1 region_name: EU (Ireland)
region_id: eu-west-2 region_name: EU (London)
region_id: sa-east-1 region_name: South America (Sao Paulo)
region_id: us-east-1 region_name: US East (N. Virginia)
region_id: us-west-1 region_name: US West (N. California)
region_id: us-west-2 region_name: US West (Oregon)

有没有办法使用boto3获取RDS的所有AWS区域名称?

amazon-web-services amazon-rds boto3 aws-ssm
1个回答
0
投票

代码应修改如下。

import boto3

#region = "us-west-1" # 
region = "us-east-1" # this effectively assigning us-east-1 to region in any case.

print("Using region:", region)
#rds = boto3.setup_default_session(region_name=region)
client = boto3.client("rds",region_name=region)
rds_responses = client.describe_source_regions()['SourceRegions']

ssm_client = boto3.client('ssm', region_name=region)
for resp in rds_responses:
    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)

修改的行是

#region = "us-west-1" # 
region = "us-east-1" # this effectively assigning us-east-1 to region in any case.

#rds = boto3.setup_default_session(region_name=region)
client = boto3.client("rds",region_name=region)
© www.soinside.com 2019 - 2024. All rights reserved.