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 ec2 commands. The VPC's address range is the one setting you cannot change later, so it pays to plan it before you create anything.
What the VPC CIDR block constrains
When you create a VPC you give it a primary IPv4 CIDR block. Every subnet in that VPC must take its range from inside that block. A subnet cannot be larger than the VPC, and two subnets in the same VPC cannot overlap.
- The VPC prefix length must be between
/16(65,536 addresses) and/28(16 addresses). - A subnet uses the same limits,
/16to/28, and always lives in exactly one Availability Zone. - You cannot resize or replace the primary CIDR block after creation. You can only add secondary blocks.
- AWS reserves 5 addresses in every subnet: the first four and the last one. The network address, the VPC router, the AWS DNS server, and one address held for future use take the first four. The last address is the reserved broadcast address, which VPCs do not support.
The reserved addresses change how you size small subnets:
| Subnet prefix | Total addresses | Usable addresses |
|---|---|---|
| /28 | 16 | 11 |
| /27 | 32 | 27 |
| /26 | 64 | 59 |
| /24 | 256 | 251 |
| /20 | 4,096 | 4,091 |
| /16 | 65,536 | 65,531 |
A /28 loses almost a third of its space to reservations. Services such as load balancers, interface endpoints, and container networking also take addresses from your subnets, so a subnet that looks big enough for your instances may run out.
Pick from private address space
Use the private ranges defined in RFC 1918 unless you have a specific reason not to.
| Range | Size | Typical use |
|---|---|---|
| 10.0.0.0/8 | 16.7 million addresses | Large organizations, many VPCs |
| 172.16.0.0/12 | 1 million addresses | Mid-size setups (note the default VPC uses 172.31.0.0/16) |
| 192.168.0.0/16 | 65,536 addresses | Home and office networks, often already used on-premises |
The 10.0.0.0/8 range gives the most room to divide among many VPCs. The 192.168.0.0/16 range is the riskiest choice for a VPC. Office networks and home routers use it constantly, and that causes overlaps as soon as someone connects over VPN.
Why overlap is the expensive mistake
Overlapping address space is the most common reason a VPC design has to be rebuilt. Two networks with the same range cannot reach each other, because a router cannot tell which side an address belongs to.
- VPC peering refuses to create a connection between VPCs whose CIDR blocks overlap.
- Transit Gateway cannot route to two attachments that advertise the same range.
- A VPN or Direct Connect link to an office that uses
10.0.0.0/16fails if your VPC also uses10.0.0.0/16. - The default VPC uses
172.31.0.0/16in every account and Region, so any two default VPCs overlap.
Even if you do not connect anything today, assume you will. Give every VPC, every account, and every on-premises site its own non-overlapping block from a single plan.
Sizing: start larger than you think
You can always leave addresses unused, but you cannot shrink or move a VPC. A /16 costs nothing more than a /24, and addresses you do not use are not billed. A good default is a /16 for each VPC that will hold real workloads. Use a smaller block only when address space is scarce, for example when your company has a tight on-premises allocation to share.
If you do run out, you can add up to five IPv4 CIDR blocks to a VPC by default (a quota you can raise). Secondary blocks cannot overlap the primary block or any other block associated with the VPC. They also have to meet AWS restrictions on reserved ranges. Adding one looks like this:
aws ec2 associate-vpc-cidr-block \
--vpc-id "$VPC_ID" \
--cidr-block 10.1.0.0/16Secondary blocks are a rescue tool, not a plan. Routes, peering, and on-premises firewalls all need updating to know about the new range. A common legitimate use is adding a block from 100.64.0.0/10 for container pods, which keeps them off your main address space.
A worked allocation plan
Suppose you run three environments and expect an on-premises network at 10.100.0.0/16. Give each environment its own /16, and reserve the rest:
| Network | CIDR |
|---|---|
| Production VPC | 10.0.0.0/16 |
| Staging VPC | 10.1.0.0/16 |
| Development VPC | 10.2.0.0/16 |
| Reserved for future VPCs | 10.3.0.0/16 to 10.99.0.0/16 |
| On-premises office | 10.100.0.0/16 |
Writing this table down and keeping it somewhere shared is the planning step that matters most. For many accounts, Amazon VPC IP Address Manager (IPAM) can track allocations and flag overlaps automatically.
Dividing one VPC
Inside a /16, a regular pattern keeps subnets easy to read and leaves room to grow. One approach is to cut the VPC into /20 blocks (16 of them) and give each Availability Zone a few of those. Within a block, carve /24 subnets for each tier. The diagram shows the first levels of that split.
Public subnets can be small because they hold only load balancers and NAT gateways. Private subnets hold the workloads, so give them more space. The pattern also leaves most of each /20 free, so you can add database or other tiers later without renumbering.
Checking a plan with code
You can verify a plan before creating anything. This Python script uses only the standard library. It splits the VPC into /20 blocks, takes the first /24 of each for public subnets and the second for private ones, and tests for overlap with another network.
import ipaddress
vpc = ipaddress.ip_network("10.0.0.0/16")
onprem = ipaddress.ip_network("10.100.0.0/16")
azs = ["us-east-1a", "us-east-1b", "us-east-1c"]
print("VPC overlaps on-premises:", vpc.overlaps(onprem))
az_blocks = list(vpc.subnets(new_prefix=20))
for az, block in zip(azs, az_blocks):
public, private = list(block.subnets(new_prefix=24))[:2]
usable = public.num_addresses - 5
print(f"{az}: block={block} public={public} private={private} usable_per_subnet={usable}")Running it prints False for the overlap check and one line per Availability Zone, ending with usable_per_subnet=251. Swap in a different on-premises range such as 10.0.128.0/17 and the check prints True, which is the signal to change your plan.
Once the plan checks out, create the VPC and subnets from it. This matches the first code example in the plan, using one Availability Zone for brevity:
VPC_ID=$(aws ec2 create-vpc --cidr-block 10.0.0.0/16 --query Vpc.VpcId --output text)
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)Common errors
- Choosing a range because it is easy to type.
10.0.0.0/16is the default in countless tutorials, so using it for every VPC guarantees overlap. Pick distinct blocks from your plan. - Sizing subnets to today's instance count. Add headroom for load balancer nodes, endpoints, and scaling events.
- Forgetting reserved addresses. A
/28holds 11 usable addresses, not 16. - Using the default VPC for real work. Its
172.31.0.0/16block is the same in every account. - Allocating every address now. Leave whole
/20blocks unassigned so future tiers fit without renumbering.
Exercise
Your company uses 10.50.0.0/16 on-premises. You plan three VPCs that must connect to it through a VPN and to each other through a Transit Gateway. Propose a CIDR for each VPC, and compute how many usable addresses a /26 subnet in one of them has.
Show answer
Any three distinct blocks that do not overlap 10.50.0.0/16 or each other work, for example 10.0.0.0/16, 10.1.0.0/16, and 10.2.0.0/16. A /26 has 64 addresses and AWS reserves 5, so it has 59 usable addresses.
Exercise
A VPC with primary CIDR 10.0.0.0/16 is running out of addresses. Can you replace the primary block with 10.0.0.0/15? What can you do instead?
Show answer
No, the primary CIDR block cannot be changed. You can associate a secondary CIDR block, such as 10.1.0.0/16, with aws ec2 associate-vpc-cidr-block, as long as it does not overlap any existing block or any network you connect to. Then create new subnets from it and update routes.