Two VPCs, one 10.0.0.0/16: plan CIDR ranges before you ever need peering
Our production and test VPCs both came out of the VPC wizard as 10.0.0.0/16 with the same subnets, so they can never be peered without renumbering one. A written CIDR table, a /20 per subnet, room for a third AZ, and what to do if you are already stuck.
When I wrote the network part of our architecture docs, I put the subnet tables of the production VPC and the test VPC next to each other. They were the same table. Both VPCs are 10.0.0.0/16, and every subnet matches too:
| Subnet | CIDR | Present in |
|---|---|---|
| public, AZ a | 10.0.0.0/20 |
both |
| public, AZ b | 10.0.16.0/20 |
both |
| private, AZ a | 10.0.128.0/20 |
both |
| private, AZ b | 10.0.144.0/20 |
both |
That's not a coincidence. It is exactly what the VPC console's "VPC and more" wizard gives you if you accept the defaults. Somebody accepted them three times, actually; a third VPC with the same range existed too and has been deleted since. Deleting it removed one third of the collision and none of the problem.
Why it matters
VPC peering needs non-overlapping CIDRs. The AWS peering docs say it plainly, and you cannot create the connection. Transit Gateway will let you attach both, but a route table can't send 10.0.5.10 to two places, so traffic between them doesn't work either. Same story for a site-to-site VPN from an office that also uses 10.0.0.0/16.
Today nothing connects our two VPCs. No peering, no Transit Gateway, no VPN. Anything one side reaches on the other side goes over public addresses. That is fine as long as nobody needs, say, a production tool to read a test database privately. The day somebody does, the only clean fix is renumbering one VPC.
And renumbering is not a setting. A subnet's CIDR can't be changed. You create new subnets, which means new ENIs for every task, a new DB subnet group (and RDS moving into it), new route tables, new endpoints, new security group rules that mention CIDRs. And the VPC's primary CIDR can never be removed, so the old range stays attached to that VPC for good. It's a migration, basically, with a weekend attached.
So the cheapest moment to pick ranges is before first terraform apply. Ten minutes and a table.
The plan I'd use now
One /16 per environment, from a table that is written down and committed. Never reuse a number, even after an environment is deleted.
| Environment | CIDR |
|---|---|
| shared (tools, CI runners) | 10.10.0.0/16 |
| prod | 10.20.0.0/16 |
| staging | 10.30.0.0/16 |
| dev | 10.40.0.0/16 |
Gaps of ten leave room to slot something in later. I skip 10.0.0.0/16 completely, since every tutorial and every wizard uses it, and the next company you acquire or the next vendor VPN you connect will be sitting on it. I'd keep away from 192.168.0.0/16 too (home routers, so anyone on a client VPN from home can clash) and from 172.17.0.0/16, which is Docker's default bridge on every laptop running Docker.
Inside each /16, one /20 per subnet. A /20 is 4096 addresses and AWS keeps 5 of them in every subnet, so 4091 usable. That's plenty for Fargate tasks, which each take one IP from the subnet.
The part people forget is the third AZ. Mumbai (ap-south-1) has three, a, b and c. Even if you start with two, give every tier a fixed block of four /20s so AZ c, or a fourth one later, slots in without moving anything.
Doing it with cidrsubnet
Terraform's cidrsubnet(prefix, newbits, netnum) does the maths. Adding 4 bits to a /16 gives a /20, and netnum picks which one:
locals {
env_cidrs = {
shared = "10.10.0.0/16"
prod = "10.20.0.0/16"
staging = "10.30.0.0/16"
dev = "10.40.0.0/16"
}
vpc_cidr = local.env_cidrs[var.environment]
azs = ["ap-south-1a", "ap-south-1b", "ap-south-1c"]
# Tiers start at fixed offsets, so a fourth AZ fits without renumbering.
tier_offset = { public = 0, private = 4, data = 8 }
subnets = {
for pair in setproduct(keys(local.tier_offset), range(length(local.azs))) :
"${pair[0]}-${local.azs[pair[1]]}" => cidrsubnet(local.vpc_cidr, 4, local.tier_offset[pair[0]] + pair[1])
}
}
I ran it through terraform console (Terraform 1.17.1) with environment = "prod":
{
"data-ap-south-1a" = "10.20.128.0/20"
"data-ap-south-1b" = "10.20.144.0/20"
"data-ap-south-1c" = "10.20.160.0/20"
"private-ap-south-1a" = "10.20.64.0/20"
"private-ap-south-1b" = "10.20.80.0/20"
"private-ap-south-1c" = "10.20.96.0/20"
"public-ap-south-1a" = "10.20.0.0/20"
"public-ap-south-1b" = "10.20.16.0/20"
"public-ap-south-1c" = "10.20.32.0/20"
}
10.20.48.0/20, 10.20.112.0/20 and 10.20.176.0/20 stay free for a fourth AZ, and everything from 10.20.192.0/18 is untouched for a tier I haven't thought of. A separate data tier for RDS is optional. I like it because the DB subnet group then has its own route table with no internet route at all, and a security group mistake is a bit harder to make. Small thing, but it reads well in a diagram.
If you build VPCs by hand rather than Terraform, the same numbers work. Building a VPC by hand walks through the console side; just type the table's values instead of the wizard's.
A check that runs in CI
A table helps only if people read it. So I wrote a small check that fails when any two ranges overlap. It reads name cidr pairs on stdin:
#!/usr/bin/env python3
import ipaddress
import itertools
import sys
nets = []
for line in sys.stdin:
line = line.split("#", 1)[0].strip()
if not line:
continue
name, cidr = line.split()
nets.append((name, ipaddress.ip_network(cidr)))
bad = 0
for (n1, c1), (n2, c2) in itertools.combinations(nets, 2):
if c1.overlaps(c2):
print(f"OVERLAP {n1} {c1} <-> {n2} {c2}")
bad += 1
print(f"{len(nets)} ranges checked, {bad} overlap(s)")
sys.exit(1 if bad else 0)
Fed with what our account looks like today plus an office VPN range on 192.168.0.0/16, it does what it should:
OVERLAP prod 10.0.0.0/16 <-> staging 10.0.0.0/16
3 ranges checked, 1 overlap(s)
The live list comes from aws ec2 describe-vpcs --query 'Vpcs[].[VpcId,CidrBlock]' --output text, which prints one VPC per line, id then CIDR. Secondary CIDRs are under CidrBlockAssociationSet if you've added any. Add the office VPN and any partner ranges by hand at the bottom of the file, since those are the ones nobody remembers.
If you're already stuck
This is our situation, so I have thought about it more than I wanted to.
First option: don't connect them. If the need is one service talking to one service, PrivateLink works across overlapping ranges. You put an NLB in front of the service, make it an endpoint service, and create an interface endpoint in the other VPC. The client sees an IP from its own range. It costs per endpoint per hour and per GB, so it suits one or two services, not "everything to everything".
Second, a new environment goes into a fresh range from the table, and only the old ones stay overlapped. That stops the problem from growing, which is honestly the most useful thing you can do on a Monday.
Third, a secondary CIDR. You can add a non-overlapping range such as 10.20.0.0/16 to the existing VPC, create new subnets in it and move ECS services across one by one (they only need new subnets in their network configuration and a redeploy). Through a Transit Gateway or a VPN you then route only the new range, which works. Peering still won't, because the primary 10.0.0.0/16 stays on the VPC and still overlaps. RDS is the slow step in this plan; read the current RDS docs on moving an instance to other subnets before you promise anyone a date. For real peering you are looking at a new VPC.
A private NAT gateway can also hide an overlapping range behind a routable one. It works, but it's the option I'd reach for last; the new regional NAT gateway mode doesn't support private NAT, so it means zonal gateways, and debugging translated addresses is no fun.
What I'd do if starting again is only the table and the cidrsubnet locals. Ten minutes, before the first apply.