Sub topic 3
Carving the VPC into subnets across Availability Zones
This chapter assumes you can read CIDR notation, you know what an Availability Zone (AZ) is, and you have the VPC and CIDR plan from the previous chapters. Here you split that address space into subnets and place them in AZs. You will also see the errors that most often trip people up.
What a subnet is
A subnet is a range of IP addresses carved out of the VPC's CIDR block. Every resource that gets a private IP address, such as an EC2 instance, a network interface, or a load balancer node, lives in exactly one subnet.
Four rules define how subnets work:
- A subnet belongs to exactly one AZ. It cannot span AZs, and it cannot be moved to another AZ later.
- A subnet's CIDR block must fall inside the VPC's CIDR block and cannot overlap another subnet in the same VPC.
- A subnet's IPv4 CIDR block cannot be resized after creation. To change the size, create a new subnet, move the workloads, and delete the old one.
- The allowed sizes run from /16 (65,536 addresses) down to /28 (16 addresses).
Because a subnet is tied to one AZ, the AZ is your unit of fault isolation. If you want a tier to survive the loss of an AZ, you create one subnet per AZ for that tier and spread instances across them.
Subnets are not public or private by themselves
AWS has no "public" or "private" setting on a subnet. The label describes the subnet's route table. A subnet is public if its route table has a route to an internet gateway, and private if it does not. The next chapters cover route tables and gateways. For now, remember that a new subnet is associated with the VPC's main route table until you associate a different one.
Reserved addresses and usable capacity
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 | Amazon-provided DNS resolver (base of the VPC range plus two) |
| 10.0.1.3 | Reserved for future use |
| 10.0.1.255 | Network broadcast (broadcast is not supported, but the address is reserved) |
The usable count is therefore the block size minus five.
| Subnet size | Total addresses | Usable addresses |
|---|---|---|
| /28 | 16 | 11 |
| /27 | 32 | 27 |
| /26 | 64 | 59 |
| /24 | 256 | 251 |
| /20 | 4,096 | 4,091 |
Small subnets run out faster than the arithmetic suggests. Several AWS services consume addresses on your behalf:
- Each NAT gateway uses one address in its subnet.
- Each Application Load Balancer node uses addresses in each subnet it serves. AWS recommends at least a /27 with eight free addresses.
- Lambda functions attached to a VPC, EKS pods, and RDS instances all create network interfaces that take addresses.
- During scale-out or a rolling deployment, you briefly need old and new instances at the same time.
Planning the layout
A common pattern is one subnet per tier per AZ. With a 10.0.0.0/16 VPC and two AZs, you might use:
| Subnet | AZ | CIDR | Tier |
|---|---|---|---|
| public-a | AZ 1 | 10.0.1.0/24 | Load balancers, NAT gateway |
| public-b | AZ 2 | 10.0.2.0/24 | Load balancers, NAT gateway |
| private-a | AZ 1 | 10.0.11.0/24 | Application servers |
| private-b | AZ 2 | 10.0.12.0/24 | Application servers |
This plan leaves 10.0.3.0 through 10.0.10.255 and everything above 10.0.12.0 free. Leaving gaps lets you add a third AZ or a database tier without renumbering. Aligning subnets on tidy boundaries also keeps your route tables and firewall rules readable.
Public subnets usually need far fewer addresses than private ones. Many designs use /26 or /27 for public subnets and larger blocks for the private tiers where the instances live.
Availability Zone names and IDs
An AZ name such as us-east-1a is a label that AWS maps to a physical zone separately for each account. Your us-east-1a may be a different data center than another account's us-east-1a. This spreads load across zones.
The AZ ID, such as use1-az1, is the same physical zone in every account. Use AZ IDs when you need to line up zones across accounts. Typical cases are a VPC endpoint service shared with another account, or a design where you want to avoid cross-AZ data transfer charges between two accounts.
aws ec2 describe-availability-zones \
--query 'AvailabilityZones[].[ZoneName,ZoneId,State]' \
--output tableTraffic between AZs in the same region is billed per gigabyte in each direction. A subnet layout that forces chatty services across AZs costs more and adds latency, so keep tightly coupled components in the same AZ when you can. Do this while keeping at least two AZs per tier for resilience.
Creating subnets in two AZs
This script creates a VPC, picks the first two available AZs in your region, and creates a public and a private subnet in each. It tags each subnet with a name so you can recognize it in the console.
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
AZS=($(aws ec2 describe-availability-zones \
--filters Name=state,Values=available Name=zone-type,Values=availability-zone \
--query 'AvailabilityZones[0:2].ZoneName' --output text))
PUBLIC_SUBNETS=()
PRIVATE_SUBNETS=()
for i in 0 1; do
PUBLIC_SUBNETS[$i]=$(aws ec2 create-subnet \
--vpc-id "$VPC_ID" \
--cidr-block "10.0.$((i + 1)).0/24" \
--availability-zone "${AZS[$i]}" \
--tag-specifications "ResourceType=subnet,Tags=[{Key=Name,Value=public-${AZS[$i]}}]" \
--query Subnet.SubnetId --output text)
PRIVATE_SUBNETS[$i]=$(aws ec2 create-subnet \
--vpc-id "$VPC_ID" \
--cidr-block "10.0.$((i + 11)).0/24" \
--availability-zone "${AZS[$i]}" \
--tag-specifications "ResourceType=subnet,Tags=[{Key=Name,Value=private-${AZS[$i]}}]" \
--query Subnet.SubnetId --output text)
done
aws ec2 describe-subnets \
--filters Name=vpc-id,Values="$VPC_ID" \
--query 'Subnets[].[Tags[?Key==`Name`]|[0].Value,CidrBlock,AvailabilityZone,AvailableIpAddressCount]' \
--output tableThe last command shows AvailableIpAddressCount. For a fresh /24 it reports 251, which confirms the five reserved addresses.
At this point none of the subnets is public. They all use the main route table, which has only the local route. Internet access comes from the route tables and gateway in the following chapters.
Subnet attributes
Two settings control how instances get public addresses at launch:
--map-public-ip-on-launchgives each new instance a public IPv4 address automatically. Turn it on only for public subnets, and note that an instance with a public address is still unreachable from the internet unless a route and a security group rule allow it.- Auto-assign IPv6 gives new network interfaces an IPv6 address when the subnet has an IPv6 CIDR block. This matters for dual-stack designs, covered later.
You can set the first one with:
aws ec2 modify-subnet-attribute \
--subnet-id "${PUBLIC_SUBNETS[0]}" \
--map-public-ip-on-launchAn instance launch request can override this setting for a single instance.
Common errors
InvalidSubnet.Range: The subnet CIDR is not inside the VPC CIDR. Check that you are using the right VPC and the right third octet.InvalidSubnet.Conflict: The new CIDR overlaps an existing subnet. List the current subnets before choosing a block.InsufficientFreeAddressesInSubnet: The subnet has no free addresses left. You cannot resize it. Add a new subnet in the same AZ and spread new resources there, or rebuild with a larger block.DependencyViolationon delete: Something still uses the subnet, usually a network interface left by a NAT gateway, load balancer, Lambda function, or endpoint. Delete or detach those resources first.- Instance launch fails in one AZ: An instance type may not be offered in every AZ. This is another reason to have subnets in several AZs, so an Auto Scaling group can choose another one.
- Everything in one AZ: The design works until that AZ has a problem. A tier with a single subnet has no AZ-level redundancy.
Summary of the working model
- A VPC supplies the address range, and subnets slice it into AZ-scoped pieces.
- Five addresses per subnet are reserved, and AWS services consume more.
- Public or private depends on routing, not on a subnet property.
- Resilience comes from repeating each tier's subnet in at least two AZs.
- Leave unused ranges in your plan, because you cannot resize a subnet.
Exercise
You have a VPC of 10.0.0.0/16 and create a subnet 10.0.5.0/27 in one AZ. How many addresses can you assign to instances, and what do you do when the subnet fills up and you need more capacity in that AZ?
Show answer
A /27 has 32 addresses and AWS reserves 5, so 27 are usable. You cannot resize the subnet. Create a new subnet in the same AZ from unused space in the VPC range (for example 10.0.6.0/24), associate it with the same route table, and launch new resources there. If the tier must be consolidated, migrate it to a larger subnet and delete the old one.
Exercise
A teammate says a subnet is public because they ticked "auto-assign public IPv4 address" on it. Why is that wrong, and what actually decides whether a subnet is public?
Show answer
Auto-assign only gives new instances a public IPv4 address. A subnet is public when its associated route table has a route to an internet gateway, usually 0.0.0.0/0 pointing at the gateway. Without that route, a public address cannot be reached from the internet or used to reach it.