Shawn Shawn - 5 months ago 44
Python Question

How to list available regions with Boto3 (Python)

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()

My output is JSON like so:

{'Regions': [{'Endpoint': '', 'RegionName': 'ap-south-1'}, {'Endpoint': '', 'RegionName': 'eu-west-1'}, {'Endpoint': '', 'RegionName': 'ap-southeast-1'}]}

I've trimmed off the repeating data and the ResponseMetadata for the sake of space.

How can I parse the RegionName into a list?


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']]