本文介绍了如何使用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:
为了节省空间,我已经修剪了重复数据和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)列出可用区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!