Sub topic 4
Laying out subnets across Availability Zones
This chapter assumes you know what a VPC, a CIDR block, and a route table are, and that you can read a CloudFormation template. A VPC spans every Availability Zone (AZ) in a Region, but a subnet lives in exactly one AZ. So the subnet layout decides which AZs your resources can use.
The core rule
When you create a subnet, you choose its AZ, and you can't change it later. To run a tier in two AZs, you need at least two subnets for that tier, one per AZ. Services such as load balancers and Auto Scaling groups accept a list of subnets. They spread resources across the AZs those subnets belong to.
A typical layout
Give each tier its own subnet in every AZ you use. Three common tiers are:
- Public subnets hold the load balancer nodes and NAT gateways. Their route table sends
0.0.0.0/0to an internet gateway. - Private application subnets hold the instances. Their route tables send outbound traffic to a NAT gateway.
- Private data subnets hold the databases. They have no route to the internet.
Planning CIDR space
Plan address space before you create anything, because you can't resize a subnet. Leave room for a third AZ and for growth. For example, a 10.0.0.0/16 VPC could use a /20 per subnet, which gives 4,091 usable addresses. AWS reserves five addresses in every subnet. Auto Scaling and load balancers use up addresses faster than most people expect, so avoid very small subnets.
Example template
This CloudFormation snippet creates a private application subnet in each of two AZs. It picks the AZs with Fn::GetAZs instead of hardcoding names.
Parameters:
VpcId:
Type: AWS::EC2::VPC::Id
Resources:
PrivateSubnetA:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VpcId
CidrBlock: 10.0.16.0/20
AvailabilityZone: !Select [0, !GetAZs ""]
PrivateSubnetB:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VpcId
CidrBlock: 10.0.32.0/20
AvailabilityZone: !Select [1, !GetAZs ""]Common errors
- Two subnets in the same AZ. The tier looks redundant but one AZ failure takes it all down. Check the AZ of each subnet, not only the count.
- One route table shared by every private subnet. This works until you need a separate NAT gateway per AZ. Use one private route table per AZ so you can point each at its local NAT gateway. The next chapter covers this.
- Hardcoded AZ names across accounts. The name
us-east-1acan map to different physical zones in different accounts. Use AZ IDs such asuse1-az1when two accounts must share a zone, for example for a shared service. - A subnet too small to scale. Instances fail to launch when the subnet runs out of addresses.
Exercise
Your Auto Scaling group lists two private subnets, and both are in us-east-1a. The instance count is 4. Does the group survive the loss of that AZ, and what do you change?
Show answer
No. Both subnets are in the same AZ, so all instances share one failure domain. Add a private subnet in a different AZ, such as us-east-1b, and update the group's subnet list so it spreads instances across both AZs.