Commit 3681299
committed
Fix NAT routing when max_nats limits NATs to fewer AZs
**Problem:**
When max_nats < number of AZs, NAT Gateways are only created in the
first max_nats AZs, but the route table mapping formulas assumed NATs
exist in all AZs, causing "Invalid index" errors.
**Example scenario that failed:**
- 2 AZs, max_nats=1 → Only 1 NAT created (in AZ0)
- 2 private route tables (1 per AZ)
- Mapping produced [0, 1] but only NAT[0] exists
- Error: aws_nat_gateway.default[1] - invalid index
**Root cause:**
The private_route_table_to_nat_map and public_route_table_to_nat_map
formulas calculated: az_index * nats_per_az + subnet_within_az
This works when NATs exist in all AZs, but fails when max_nats limits
NATs to fewer AZs. The formula could generate indices >= nat_count.
**Fix:**
Added modulo operation to clamp results to available NAT indices:
(floor(i / subnets_per_az_count) * nats_per_az +
(i % subnets_per_az_count) % nats_per_az) % nat_count
Now route tables in AZs without NATs will wrap around to use NATs
from other AZs (typically AZ0).
**Why wasn't this caught by tests?**
None of the module's examples use the max_nats feature. All examples
either omit max_nats (defaulting to unlimited) or use max_nats >=
number of AZs, so the bug was never triggered.
**Testing:**
This bug was discovered by the aws-vpc component test suite when
using:
- 2 AZs with max_nats: 1
- public_subnets_enabled: true
- NAT Gateway enabled
Fixes: #2261 parent fec1189 commit 3681299
1 file changed
+16
-8
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
240 | 240 | | |
241 | 241 | | |
242 | 242 | | |
243 | | - | |
| 243 | + | |
244 | 244 | | |
245 | 245 | | |
246 | 246 | | |
| |||
255 | 255 | | |
256 | 256 | | |
257 | 257 | | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
258 | 262 | | |
259 | 263 | | |
260 | 264 | | |
261 | | - | |
262 | | - | |
263 | | - | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
264 | 270 | | |
265 | 271 | | |
266 | 272 | | |
267 | | - | |
| 273 | + | |
268 | 274 | | |
269 | 275 | | |
270 | 276 | | |
271 | | - | |
272 | | - | |
273 | | - | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
274 | 282 | | |
275 | 283 | | |
276 | 284 | | |
| |||
0 commit comments