如何使用Boto3(Python)列出可用区域 [英] How to list available regions with Boto3 (Python)
问题描述
随着AWS扩展并添加新区域,我想让我的代码自动检测到这一点.目前,选择您的地区"是硬编码的,但是我想仅针对 RegionName 解析以下内容.
As AWS expands and adds new regions, I'd like to have my code automatically detect that. Currently, the "Select your region" is hard coded but I would like to parse the following for just the RegionName.
import boto3
ec2 = boto3.client('ec2')
regions = ec2.describe_regions()
print(regions)
我的输出是JSON,如下所示:
My output is JSON like so:
{'Regions':[{'Endpoint':'ec2.ap-south-1.amazonaws.com','RegionName':'ap-south-1'},{'Endpoint':'ec2.eu -west-1.amazonaws.com","RegionName":"eu-west-1"},{'Endpoint":"ec2.ap-southeast-1.amazonaws.com","RegionName":"ap-southeast" -1'}]}
{'Regions': [{'Endpoint': 'ec2.ap-south-1.amazonaws.com', 'RegionName': 'ap-south-1'}, {'Endpoint': 'ec2.eu-west-1.amazonaws.com', 'RegionName': 'eu-west-1'}, {'Endpoint': 'ec2.ap-southeast-1.amazonaws.com', 'RegionName': 'ap-southeast-1'}]}
为了节省空间,我已经修剪了重复数据和ResponseMetadata.
I've trimmed off the repeating data and the ResponseMetadata for the sake of space.
如何将RegionName解析为列表?
How can I parse the RegionName into a list?
推荐答案
以下内容将为您返回每个区域的RegionName和Endpoint.
The following will return you the RegionName and Endpoint for each region.
# List all regions
client = boto3.client('ec2')
regions = [region['RegionName'] for region in client.describe_regions()['Regions']]
这篇关于如何使用Boto3(Python)列出可用区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!