Amazon Web Services (AWS) Code Examples
List all availability zones in your AWS region
AWS CLI
To list all availability zones in your AWS region using the AWS Command Line Interface (CLI), you can
use the describe-availability-zones
command.
Here's the command you can use:
aws ec2 describe-availability-zones --region your_region
Replace your_region
with the appropriate AWS region code, such as us-east-1
for US East (N. Virginia).
For example, if you want to list the availability zones in the US East (N. Virginia) region, you would
run the following command:
aws ec2 describe-availability-zones --region us-east-1
This command will provide you with information about all the availability zones available in the specified region, including their names, states, and other details. Here's an example of what the output might look like:
{
"AvailabilityZones": [
{
"ZoneName": "us-east-1a",
"State": "available",
"RegionName": "us-east-1",
"Messages": [],
"OptInStatus": "opt-in-not-required"
}
]
}
Python
To list all availability zones for a specific region using the Boto3 library in Python, you can make use of
the ec2
client and its describe_availability_zones()
method. Here's an example
code snippet that demonstrates how to achieve this:
import boto3
def list_availability_zones(region_name):
ec2_client = boto3.client('ec2', region_name=region_name)
response = ec2_client.describe_availability_zones()
availability_zones = response['AvailabilityZones']
for zone in availability_zones:
print(zone['ZoneName'])
# Specify the region for which you want to list availability zones
region_name = 'us-west-2'
# Call the function to list availability zones
list_availability_zones(region_name)
In the above code, you need to specify the desired region_name
where you want to list the
availability zones. The ec2_client.describe_availability_zones()
method retrieves information
about the availability zones for the specified region. The response is then parsed to extract the
ZoneName
field for each availability zone, which is then printed.
Make sure you have the boto3
library installed and properly configured with your AWS
credentials before running this code.
The output of the code snippet provided would be a list of availability zone names for the specified region. Each availability zone name would be printed on a separate line.
For example, if you specified the region_name
as 'us-west-2', the output might look like:
us-west-2a
us-west-2b
us-west-2c