fix(nat): Ensure NAT gateways are created in correct availability zone (#1257) #1258
+60
−13
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
✳️ Pull Request — Fix NAT Gateway AZ Mapping Logic (#1257)
📝 Description
This PR fixes and improves the subnet-to-AZ mapping logic in the VPC module, ensuring that NAT gateways are consistently mapped to their respective Availability Zones (AZs).
The update also improves behavior when the number of subnets differs from the number of AZs and updates example configurations accordingly.
🎯 Motivation and Context
Previously, subnet indexing caused misalignment between public/private subnets and NAT gateways when subnet counts did not match AZ counts.
This resulted in uneven NAT gateway distribution or mismatched subnet routing.
This PR ensures:
Consistent NAT gateway allocation per AZ
Flexible handling when the number of subnets > or < number of AZs
Accurate example coverage for both 2-AZ and 3-AZ configurations
✅ Fixes: #1257
⚙️ Breaking Changes
No breaking changes.
Existing configurations using standard subnet-to-AZ ratios remain fully compatible.
Only internal subnet indexing logic is refined to ensure correctness and consistency.
🧪 How Has This Been Tested?
Updated and validated
examples/completeto demonstrate proper behaviorTested with multiple configurations:
AZ = #Subnets → NAT gateways align correctly
2 AZs → validated subnet indexing logic
#Subnets > AZs → confirmed correct subnet distribution
Executed
pre-commit run -asuccessfully✅
terraform fmt,terraform_docs,tflint, andvalidatepassed✅ Wrapper modules generated successfully (
wrappers/)Verified
terraform planoutput across all scenarios📎 Test evidence and screenshots attached:
Test_Results.docx🧩 Files Updated
variables.tf— refined subnet/AZ indexing logicwrappers/main.tf— regenerated wrappersexamples/complete/main.tf— validated multi-AZ behaviorREADME.md— updated automatically byterraform_docs✅ Summary of Validation
🙌 Notes for Maintainers
The PR follows contributing guidelines
Semantic commit:
fix(vpc): correct NAT gateway AZ mapping logic (#1257)No manual changelog update required (maintainers handle this at merge)
📸 Test Artifacts
Attached:
Test_Results.docx— includes screenshots, plan outputs, and hook validation logs.