如何使用Boto3(Python)列出可用区域 [英] How to list available regions with Boto3 (Python)

查看:426
本文介绍了如何使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆