Sub topic 2
Choosing CIDR blocks and planning address space
This chapter assumes you can read CIDR notation (for example, that /24 means 256 addresses) and have run basic AWS CLI commands. It covers how to pick a VPC's address range and divide it so later growth and connections don't force a rebuild.
Why the choice is hard to undo
A VPC's IPv4 range is a CIDR block between /16 (65,536 addresses) and /28 (16 addresses). You can add secondary CIDR blocks later, but you can't resize or replace the primary block. Two VPCs with overlapping ranges also can't be peered, and they are awkward to join through Transit Gateway or a VPN. Plan the ranges before you create anything.
Use the private ranges from RFC 1918:
| Range | Size | Typical use |
|---|---|---|
| 10.0.0.0/8 | 16.7 million addresses | Many VPCs, large organizations |
| 172.16.0.0/12 | 1 million addresses | Mid-sized setups |
| 192.168.0.0/16 | 65,536 addresses | Small setups. Often overlaps home and office networks |
The default VPC uses 172.31.0.0/16. Don't reuse it for networks you plan to connect.
Subnet sizing
Each subnet takes a slice of the VPC block. AWS reserves five addresses in every subnet: the first four and the last one. A /24 therefore gives 251 usable addresses, not 256. A /28 gives only 11.
Services such as EKS and Lambda can consume many addresses from a subnet, so give those subnets more room. A common starting point is one /24 or larger per tier per Availability Zone, taken from a /16 VPC.
Checking a plan before building it
Write the plan down and test it for overlaps. This script uses only the Python standard library:
import ipaddress
from itertools import combinations
vpcs = {
"prod": "10.0.0.0/16",
"staging": "10.1.0.0/16",
"dev": "10.2.0.0/16",
"onprem": "10.1.128.0/17", # deliberately conflicts with staging
}
nets = {name: ipaddress.ip_network(cidr) for name, cidr in vpcs.items()}
for (a, na), (b, nb) in combinations(nets.items(), 2):
if na.overlaps(nb):
print(f"OVERLAP: {a} {na} and {b} {nb}")
prod_subnets = list(nets["prod"].subnets(new_prefix=24))
print(len(prod_subnets), "possible /24 subnets in prod")
print("usable per /24:", prod_subnets[0].num_addresses - 5)It prints one overlap (staging and onprem), then 256 possible /24 subnets in prod and usable per /24: 251. Fix conflicts on paper, not after resources exist.
Creating the VPC and subnets
Once the plan is clean, create the VPC and two /24 subnets from the earlier plan:
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)Common errors
InvalidVpc.Range: the block is outside/16to/28, or it isn't a valid CIDR.InvalidSubnet.Conflict: the new subnet overlaps an existing subnet in the same VPC.InvalidSubnet.Range: the subnet falls outside the VPC's CIDR blocks.- Peering requests that fail or give no working routes because both VPCs use
10.0.0.0/16.
Exercise
A team has a VPC of 10.0.0.0/16 and wants to peer it with a second VPC. Which of 10.0.128.0/17, 10.1.0.0/16 and 172.31.0.0/16 can the second VPC use? How many usable addresses does a /26 subnet have?
Show answer
10.0.128.0/17 can't be used because it falls inside 10.0.0.0/16. Both 10.1.0.0/16 and 172.31.0.0/16 are fine because neither overlaps 10.0.0.0/16. A /26 has 64 addresses, minus 5 reserved by AWS, so 59 are usable.