Sub topic 3
Availability Zones: isolated locations within a Region
Prerequisites
This chapter assumes you have an AWS account, can run commands in a terminal, and know what a virtual server and an IP address are. It also assumes you know that a Region is an isolated geographic area, which the previous chapter covered.
What an Availability Zone is
An Availability Zone (AZ) is one or more data centers inside a Region. Each AZ has its own power, cooling, and physical security. AZs in the same Region are separate enough that a fire, flood, or power failure in one is unlikely to affect another. They are also close enough to each other for fast, low-latency links between them.
Most Regions have three or more AZs. Some older or smaller Regions have fewer, so check your target Region before you design around a specific number.
AWS names AZs by adding a letter to the Region code. For example, us-east-1a and us-east-1b are two AZs in the us-east-1 Region.
What isolation means in practice
Isolation shapes how you build on AWS. A failure is usually contained to one AZ, so you can survive it if your workload runs in more than one AZ. A single AZ is a single point of failure for anything you place only there.
Some resources are tied to one AZ:
- An EC2 instance runs in one AZ.
- An EBS volume lives in one AZ and can attach only to instances in that same AZ.
- A subnet in a VPC lives in exactly one AZ.
Other services spread across AZs for you. For example, Amazon S3 Standard stores objects across multiple AZs automatically. The next chapter covers which resources are global, regional, or zonal.
Inspecting AZs from the CLI
The describe-availability-zones command lists the AZs for a Region. This example prints the name, the ID, the state, and the zone type:
aws ec2 describe-availability-zones --region us-east-1 \
--query "AvailabilityZones[].[ZoneName,ZoneId,State,ZoneType]" \
--output tableThe output has one row per zone. State is normally available. ZoneType is availability-zone for standard AZs. Local Zones and Wavelength Zones also appear in results for some Regions, with a different zone type. Later chapters cover them.
The ZoneId column holds a value such as use1-az1. This ID is different from the name. The chapter on AZ names versus AZ IDs explains why that matters.
Inspecting AZs with boto3
With the AWS SDK for Python, you can filter by zone type so that only standard AZs are returned:
import boto3
ec2 = boto3.client("ec2", region_name="us-east-1")
response = ec2.describe_availability_zones(
Filters=[{"Name": "zone-type", "Values": ["availability-zone"]}]
)
for az in response["AvailabilityZones"]:
print(az["ZoneName"], az["ZoneId"], az["State"])The call returns only AZs in the Region you set on the client. If you omit region_name, boto3 uses the Region from your configuration.
Placing a resource in an AZ
When you create a subnet, you choose its AZ. Every resource you launch into that subnet then runs in that AZ. This command creates a subnet in a specific AZ by ID:
aws ec2 create-subnet \
--vpc-id vpc-0123456789abcdef0 \
--cidr-block 10.0.1.0/24 \
--availability-zone-id use1-az1The VPC ID here is a placeholder. Replace it with the ID of a VPC in your account.
Common errors
- Attaching an EBS volume to an instance in another AZ fails, because the volume and the instance must share an AZ.
- Running everything in one AZ means one AZ failure takes the whole workload down.
- Hard-coding an AZ name such as
us-east-1ecan fail in an account where that name does not exist. List the AZs first, then choose from the result.
Exercise
You create an EBS volume in us-east-1a and try to attach it to an EC2 instance running in us-east-1b. Why does the attach fail, and what can you do?
Show answer
An EBS volume lives in a single AZ and can attach only to instances in that same AZ. You can launch an instance in us-east-1a, or create a snapshot of the volume and restore it as a new volume in us-east-1b.