Custom CIDR Blocks in an AWS VPC: Taking Control of Subnet Addressing

The vpc-app module computes subnet CIDRs automatically — convenient until you need deterministic addressing for peering, firewall rules, or IPAM planning. This example shows the override knobs: exact CIDR maps, subnet bit-width, and spacing.

The configuration

module "vpc_app_example" {
  source = "../../modules/vpc-app"

  vpc_name               = var.vpc_name
  cidr_block             = "10.0.0.0/20"   # deliberately small VPC
  num_nat_gateways       = 2
  num_availability_zones = 3

  public_subnet_cidr_blocks = {
    "AZ-0" = "10.0.0.0/25"
  }
  private_app_subnet_cidr_blocks = {
    "AZ-0" = "10.0.8.0/25"
  }

  persistence_subnet_bits = 4
  private_subnet_bits     = 4
  subnet_spacing          = 5
}

What each knob does

  • cidr_block = "10.0.0.0/20" — a deliberately small VPC (4,096 addresses). The module docs recommend /16 but allow down to /27; smaller ranges are how you keep address space available for future VPCs in a growing org.
  • public_subnet_cidr_blocks / private_app_subnet_cidr_blocks — explicit AZ-n -> CIDR maps. When a map is non-empty the module uses your addresses for those AZs instead of computing them. Keys must be AZ-0, AZ-1, … matching the zone index.
  • persistence_subnet_bits / private_subnet_bits / subnet_spacing — the computed path’s parameters: bit-width of each generated subnet (/24-ish here) and spacing between them. Useful when you want auto-allocation but with a different granularity than the defaults.

Why it matters

Deterministic CIDRs are the difference between “VPCs that can’t peer because ranges overlap” and an address plan. This pattern — small VPC, explicit maps where you care, computed bits where you don’t — is the pragmatic middle ground between full IPAM tooling and chaos.

Verified: the reconstructed example passes terraform validate against the real vpc-app module.


<
Previous Post
Nat Gateway Failure Domain
>
Next Post
Vpc No Nat Gateway