The NAT Gateway Count is a Failure-Domain Decision

Share
AWS VPC NAT Gateway Failure Domain Architecture

The NAT gateway count in a VPC module is usually treated as a cost setting: one for non-prod, one per AZ for prod, but it decides something more important: which Availability Zone each private subnet depends on for outbound traffic.

What a typical module does

The module creates one default route per AZ for its private app subnets, not one per NAT gateway. Each route picks its target with element(aws_nat_gateway.nat.*.id, count.index), and the NAT gateways are placed with the same element() pattern over the public subnets. element() wraps around when the index runs past the end of the list, so the two mappings only line up when the NAT count equals the AZ count.

Verifying it

I applied the module with the example's inputs against a local AWS API emulator and read the route tables back. With the module defaults (1 NAT, 2 AZs):

private-app-0  us-east-1a -> NAT in us-east-1a
private-app-1  us-east-1b -> NAT in us-east-1a  [cross-AZ]

With 2 NATs across 3 AZs, the third AZ routes through the first AZ's NAT. With 3 NATs across 2 AZs, one NAT gateway is referenced by no route at all, yet you still pay for it and its Elastic IP. With zero, the private subnets have no default route, only the local route.

Why it matters

If the shared NAT gateway or its supporting path becomes unavailable, workloads in an otherwise healthy AZ lose outbound access too. In normal operation, their traffic adds inter-AZ data charges. The plan output looks good; the only signal is two route tables sharing a nat_gateway_id.

Each option is a trade-off. Zonal NAT costs the most but contains a NAT failure to its own zone. A single NAT is fine for non-prod if everyone accepts the single-AZ dependency. Zero NAT gives the smallest egress surface, but it requires a full inventory of what the workloads call, plus interface endpoints for the AWS services they still need.

What I would change at scale

Replace the integer with an explicit egress_mode = "none" | "single" | "zonal" and derive the NAT count from it. Then turn the emulator check into a CI test that fails when a private route table targets a NAT gateway in another AZ, unless the mode is single.

Decide where each AZ's egress should go first, and derive the NAT count from that. Never work the other way round.