Sub topic 3
Carving the VPC into subnets across Availability Zones
This chapter assumes you know CIDR notation, have an AWS account with the AWS CLI configured, and know what a region and an Availability Zone (AZ) are. It builds on the previous chapter's CIDR plan.
What a subnet is
A subnet is a slice of the VPC's CIDR block that lives in exactly one Availability Zone. The VPC spans every AZ in its region, but a subnet does not. To run in two AZs, you create at least two subnets.
A subnet's CIDR must fit inside the VPC's CIDR and must not overlap another subnet in the same VPC. The size range is /16 (largest) to /28 (smallest).
Reserved addresses
AWS reserves five addresses in every subnet. For 10.0.1.0/24 they are:
| Address | Purpose |
|---|---|
| 10.0.1.0 | Network address |
| 10.0.1.1 | VPC router |
| 10.0.1.2 | DNS server (VPC base address plus two) |
| 10.0.1.3 | Reserved for future use |
| 10.0.1.255 | Broadcast address (broadcast is not supported, but the address is still reserved) |
A /24 has 256 addresses, so 251 are usable. A /28 has 16 addresses, so only 11 are usable. Plan for this when you size small subnets.
Creating subnets
The commands below create a VPC and two /24 subnets in one AZ. We will use the second subnet as the private tier later.
VPC_ID=$(aws ec2 create-vpc --cidr-block 10.0.0.0/16 --query Vpc.VpcId --output text)
aws ec2 modify-vpc-attribute --vpc-id "$VPC_ID" --enable-dns-hostnames
PUBLIC_SUBNET_ID=$(aws ec2 create-subnet --vpc-id "$VPC_ID" --cidr-block 10.0.1.0/24 --availability-zone us-east-1a --query Subnet.SubnetId --output text)
PRIVATE_SUBNET_ID=$(aws ec2 create-subnet --vpc-id "$VPC_ID" --cidr-block 10.0.2.0/24 --availability-zone us-east-1a --query Subnet.SubnetId --output text)Note that the names "public" and "private" are only labels at this point. Both subnets behave identically until you attach route tables (covered in the next chapters). A subnet becomes public only when its route table has a route to an internet gateway.
Spreading across AZs
One AZ is a single point of failure. A common pattern is one subnet per tier per AZ, with a CIDR scheme that makes the AZ and tier readable from the address.
Here the second digit group tells you the tier and AZ: 1 and 2 for zone a, 11 and 12 for zone b. Any consistent scheme works. Leave gaps so you can add subnets later, because you cannot resize a subnet once it exists.
Common errors
- InvalidSubnet.Conflict: the new CIDR overlaps an existing subnet. Check
aws ec2 describe-subnets --filters Name=vpc-id,Values=$VPC_ID. - InvalidSubnet.Range: the CIDR falls outside the VPC block, or the prefix is outside /16 to /28.
- Running out of IPs: services such as load balancers, Lambda in a VPC, and EKS consume addresses from your subnets. Give those subnets more room than the instance count suggests.
- AZ names differ per account:
us-east-1ain your account may map to a different physical zone than in another account. Use AZ IDs (for exampleuse1-az1) when coordinating across accounts.
Exercise
A /27 subnet is created in a VPC. How many addresses can you assign to instances, and why is the number not 32?
Show answer
27 addresses. A /27 contains 32 addresses, and AWS reserves five of them in every subnet (network address, VPC router, DNS server, one for future use, and broadcast).
Exercise
You need to survive the loss of one AZ with web and database tiers separated. What is the minimum number of subnets, and why?
Show answer
Four: one public (web) and one private (database) subnet in each of two AZs. A subnet exists in only one AZ, so each tier needs a subnet in both zones.