Sub topic 4
Laying Out Subnets Across Availability Zones
This chapter assumes you know what a VPC, a CIDR block, a route table, and an Availability Zone (AZ) are. It also assumes you can read a CloudFormation template and run AWS CLI commands. You will learn how to divide a VPC into subnets so that every tier of a workload exists in at least two AZs, and which mistakes undermine that layout.
Why subnet layout matters for availability
A VPC spans every AZ in its Region, but a subnet lives in exactly one AZ. You choose that AZ when you create the subnet, and you cannot move it later. This means the subnet is the unit that connects your resources to a failure domain. If all of your instances sit in subnets in us-east-1a, a VPC that "spans the Region" gives you no protection when that zone fails.
The rule for this chapter is simple: each tier gets one subnet per AZ, in at least two AZs. A tier is a group of resources with the same job and the same network exposure, such as load balancers, application servers, or databases.
Choosing AZs by AZ ID
Zone names such as us-east-1a are mapped to physical zones separately for each account. Your us-east-1a may not be the same physical zone as another account's us-east-1a. The AZ ID, such as use1-az1, is the same for everyone. List both:
aws ec2 describe-availability-zones \
--query 'AvailabilityZones[].[ZoneName,ZoneId]' \
--output tableUse AZ IDs when you need to line up zones across accounts. Two examples are a shared services account and a workload account that connect through a VPC endpoint service, or a PrivateLink provider and its consumers. Placing both sides in the same AZ ID avoids cross-zone data transfer charges and keeps a zone failure from hitting only one side.
A three-tier, two-or-more-AZ layout
A common layout uses three tiers, each with a subnet in every AZ.
| Tier | Subnet type | Typical contents | Route to the internet |
|---|---|---|---|
| Public | Public | Application Load Balancer nodes, NAT gateways | Internet gateway |
| Application | Private | EC2 instances in an Auto Scaling group | NAT gateway in the same AZ |
| Data | Private (isolated) | RDS or Aurora instances | None |
A subnet is public only because its route table has a route to an internet gateway. Nothing else about the subnet makes it public. The data tier has no default route at all, so it cannot start connections to the internet.
Sizing the CIDR blocks
Plan the address space before you create subnets, because you cannot resize a subnet's CIDR block. Keep these rules in mind:
- AWS reserves five addresses in every subnet: the network address, the VPC router, the DNS server, one for future use, and the broadcast address. A
/24gives 251 usable addresses, not 256. - Give every AZ's subnet in a tier the same size. Symmetric subnets make capacity math simple: when one AZ is lost, you know the remaining AZs can absorb the same number of instances.
- Size the application tier for growth and for scale-out events. Auto Scaling, rolling deployments, and failover to the remaining AZs all consume extra addresses. Running out of IPs in one subnet is a common cause of failed scaling.
- Leave unused ranges so you can add a third or fourth AZ later without renumbering.
An example plan for a 10.0.0.0/16 VPC across three AZs:
| Tier | AZ A | AZ B | AZ C |
|---|---|---|---|
| Public (`/24`) | 10.0.0.0/24 | 10.0.1.0/24 | 10.0.2.0/24 |
| Application (`/22`) | 10.0.16.0/22 | 10.0.20.0/22 | 10.0.24.0/22 |
| Data (`/24`) | 10.0.32.0/24 | 10.0.33.0/24 | 10.0.34.0/24 |
The application tier gets larger subnets because it grows the most. Load balancers need few addresses, but an Application Load Balancer needs at least eight free addresses per subnet to scale its nodes, so do not make public subnets too small.
Defining the layout in CloudFormation
Write the layout as a template so that every AZ is built the same way. This fragment creates the application subnets and ties them to explicit AZs. It uses Fn::GetAZs to pick zones from the current Region.
Parameters:
VpcId:
Type: AWS::EC2::VPC::Id
Resources:
PrivateSubnetA:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VpcId
AvailabilityZone: !Select [0, !GetAZs ""]
CidrBlock: 10.0.16.0/22
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: app-a
PrivateSubnetB:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VpcId
AvailabilityZone: !Select [1, !GetAZs ""]
CidrBlock: 10.0.20.0/22
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: app-bLater chapters pass these two subnets to an Auto Scaling group through VPCZoneIdentifier, which is how the group spreads instances across both zones.
<br>
Note that Fn::GetAZs returns zone names, so the index you select may map to a different physical zone in another account. That is fine for a single-account workload. If you must pin to a specific AZ ID, create the subnet with the AvailabilityZoneId property instead of AvailabilityZone.
Route tables: one per AZ for private tiers
Public subnets can share one route table, because every one of them sends 0.0.0.0/0 to the same internet gateway. Private subnets are different. A NAT gateway lives in a single AZ, so a private subnet should send its default route to the NAT gateway in its own AZ. That requires one private route table per AZ.
If you use a single shared private route table pointing at one NAT gateway, traffic from every AZ crosses into that gateway's zone. You pay cross-AZ data charges, and a failure of that AZ cuts off outbound access for the whole VPC. The next chapter covers this in detail. For now, build the layout so the per-AZ route tables are possible:
- Create one route table for each AZ's application subnet.
- Associate each route table with exactly one subnet in that AZ.
- Add the
0.0.0.0/0route to the NAT gateway in the same AZ.
The data tier needs no default route. Keep its route table limited to the local VPC route.
Common mistakes
- Two subnets in the same AZ. Console and template defaults can place both subnets in one zone. Check the AZ column in the subnet list.
- A tier with one subnet. The load balancer, instances, and database each need a subnet in every AZ you use. A database subnet group needs subnets in at least two AZs, and RDS rejects a group that has fewer.
- Mismatched subnet sizes. One small subnet becomes the bottleneck when scaling or failover shifts capacity to it.
- Hard-coded zone names. A template that names
us-east-1afails in Regions or accounts where that zone is unavailable. Select zones dynamically or take them as parameters. - Relying on the default VPC. Default subnets are public and fit quick tests, not tiered production designs.
- Forgetting that a new AZ means a new subnet. Adding capacity in a new zone does nothing until each tier has a subnet there.
Checking your layout
After you deploy, confirm each tier covers the AZs you expect:
aws ec2 describe-subnets \
--filters "Name=vpc-id,Values=$VPC_ID" \
--query 'Subnets[].[Tags[?Key==`Name`]|[0].Value,AvailabilityZone,CidrBlock,AvailableIpAddressCount]' \
--output tableEvery tier name should appear once per AZ, with the same CIDR size. The AvailableIpAddressCount column also shows how close a subnet is to running out of addresses.
Exercise
A VPC has one private route table shared by the application subnets in three AZs. The route table sends 0.0.0.0/0 to a single NAT gateway in AZ A. Name two problems this causes and say how to fix the layout.
Show answer
Traffic from AZ B and AZ C crosses AZ boundaries to reach the NAT gateway, which adds cross-AZ data transfer charges. If AZ A fails, the NAT gateway fails with it, so instances in AZ B and AZ C lose outbound internet access even though their own zones are healthy. To fix it, create a NAT gateway in a public subnet in each AZ, create one private route table per AZ, and point each route table's 0.0.0.0/0 route to the NAT gateway in the same AZ.
Exercise
You plan a /16 VPC with application subnets in three AZs. Why should all three application subnets have the same size, and what happens to usable addresses in a /24 subnet?
Show answer
Equal sizes make capacity predictable. If one AZ is lost, the remaining AZs hold the same amount of capacity each, and no single small subnet limits scaling. A /24 has 256 addresses, and AWS reserves five of them, so 251 are usable.