I’m trying to enumerate all AWS regions available to me in Python.
AWS documentation suggests the following method for EC2:
ec2 = boto3.client('ec2')
# Retrieves all regions/endpoints that work with EC2
response = ec2.describe_regions()
print('Regions:', response['Regions'])
However running it results in an exception
botocore.exceptions.NoRegionError: You must specify a region.
When I am specifying a region to boto3.client request, I’m getting 11 regions out of 18 available.
Apart from the obvious mistake in AWS documentation, and apart from lack of logic of requiring to provide a region to get a full list of regions, how do I get around this?
2
Answers
Found it - all that is required is to add AllRegions=True to describe_regions():
The AWS docs are technically correct.
ec2.describe_regions()
retrieves all regions that are ‘available to you’.In clearer terms, this means the response will only include regions that are enabled for your account & thus exclude any regions that are disabled within your account.
While not on the same page, the documentation for
describe_regions
explicitly states this:You most likely have 15 regions disabled within your account, which is why not all 26 regions (excluding 2 GovCloud regions) are being returned.
As you’ve discovered, setting the
AllRegions
parameter toTrue
will return all regions regardless of their status within your account but please note that just because the API now returns them all, does not mean you can interact with them.P.S. I agree that the AWS docs could be improved probably by rewording ‘Retrieves all regions/endpoints that work with EC2’ to ‘Retrieves all enabled regions/endpoints within your account that work with EC2’. This is the source for the page you’ve linked – feel free to open a pull request suggesting an improvement.