Skip to content

Commit 94e508a

Browse files
fix(vpc): correct NAT gateway AZ mapping logic (#1257)
1 parent 4d37b2b commit 94e508a

File tree

4 files changed

+15
-23
lines changed

4 files changed

+15
-23
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ No modules.
336336
| [aws_vpn_gateway_route_propagation.intra](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpn_gateway_route_propagation) | resource |
337337
| [aws_vpn_gateway_route_propagation.private](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpn_gateway_route_propagation) | resource |
338338
| [aws_vpn_gateway_route_propagation.public](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpn_gateway_route_propagation) | resource |
339+
| [aws_availability_zones.available](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zones) | data source |
339340
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
340341
| [aws_iam_policy_document.flow_log_cloudwatch_assume_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
341342
| [aws_iam_policy_document.vpc_flow_log_cloudwatch](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
@@ -488,6 +489,7 @@ No modules.
488489
| <a name="input_name"></a> [name](#input\_name) | Name to be used on all the resources as identifier | `string` | `""` | no |
489490
| <a name="input_nat_eip_tags"></a> [nat\_eip\_tags](#input\_nat\_eip\_tags) | Additional tags for the NAT EIP | `map(string)` | `{}` | no |
490491
| <a name="input_nat_gateway_destination_cidr_block"></a> [nat\_gateway\_destination\_cidr\_block](#input\_nat\_gateway\_destination\_cidr\_block) | Used to pass a custom destination route for private NAT Gateway. If not specified, the default 0.0.0.0/0 is used as a destination route | `string` | `"0.0.0.0/0"` | no |
492+
| <a name="input_nat_gateway_subnet_ids"></a> [nat\_gateway\_subnet\_ids](#input\_nat\_gateway\_subnet\_ids) | List of subnet IDs to use for NAT Gateways. If provided, these subnet IDs will be used (in order). If empty, the module will automatically select the public subnet that matches each Availability Zone (AZ). | `list(string)` | `[]` | no |
491493
| <a name="input_nat_gateway_tags"></a> [nat\_gateway\_tags](#input\_nat\_gateway\_tags) | Additional tags for the NAT gateways | `map(string)` | `{}` | no |
492494
| <a name="input_one_nat_gateway_per_az"></a> [one\_nat\_gateway\_per\_az](#input\_one\_nat\_gateway\_per\_az) | Should be true if you want only one NAT Gateway per availability zone. Requires `var.azs` to be set, and the number of `public_subnets` created to be greater than or equal to the number of availability zones specified in `var.azs` | `bool` | `false` | no |
493495
| <a name="input_outpost_acl_tags"></a> [outpost\_acl\_tags](#input\_outpost\_acl\_tags) | Additional tags for the outpost subnets network ACL | `map(string)` | `{}` | no |

examples/complete/main.tf

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ locals {
99
region = "eu-west-1"
1010

1111
vpc_cidr = "10.0.0.0/16"
12-
azs = slice(data.aws_availability_zones.available.names, 0, 3)
13-
12+
azs = slice(sort(data.aws_availability_zones.available.names), 0, 3)
13+
#azs = slice(sort(data.aws_availability_zones.available.names), 0, 2) #Tested with 2 AZs
1414
tags = {
1515
Example = local.name
1616
GithubRepo = "terraform-aws-vpc"
@@ -28,9 +28,11 @@ module "vpc" {
2828
name = local.name
2929
cidr = local.vpc_cidr
3030

31-
azs = local.azs
32-
private_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k)]
33-
public_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k + 4)]
31+
azs = local.azs
32+
private_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k)]
33+
#4 Subnets created for 3 AZs to test the NAT gateways are created in correct availability zone
34+
public_subnets = [for i in range(4) : cidrsubnet(local.vpc_cidr, 8, i + 4)]
35+
#public_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k + 4)]
3436
database_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k + 8)]
3537
elasticache_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k + 12)]
3638
redshift_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k + 16)]
@@ -41,19 +43,20 @@ module "vpc" {
4143
database_subnet_names = ["DB Subnet One"]
4244
elasticache_subnet_names = ["Elasticache Subnet One", "Elasticache Subnet Two"]
4345
redshift_subnet_names = ["Redshift Subnet One", "Redshift Subnet Two", "Redshift Subnet Three"]
44-
intra_subnet_names = []
4546

4647
create_database_subnet_group = false
4748
manage_default_network_acl = false
4849
manage_default_route_table = false
4950
manage_default_security_group = false
5051

52+
# NAT Gateway Configuration
53+
enable_nat_gateway = true
54+
one_nat_gateway_per_az = true # Module will automatically map NAT Gateways to first public subnet in each AZ
55+
single_nat_gateway = false
56+
5157
enable_dns_hostnames = true
5258
enable_dns_support = true
5359

54-
enable_nat_gateway = true
55-
single_nat_gateway = true
56-
5760
customer_gateways = {
5861
IP1 = {
5962
bgp_asn = 65112

variables.tf

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,20 +1245,6 @@ variable "one_nat_gateway_per_az" {
12451245
default = false
12461246
}
12471247

1248-
variable "nat_gateway_subnet_ids" {
1249-
description = <<EOT
1250-
Optional list of subnet IDs to use for NAT Gateways. If provided, these
1251-
subnet IDs will be used (in order). If empty, the module will automatically
1252-
select the public subnet that matches each Availability Zone (AZ).
1253-
EOT
1254-
validation {
1255-
condition = alltrue([for id in var.nat_gateway_subnet_ids : id != ""])
1256-
error_message = "nat_gateway_subnet_ids must not contain empty strings."
1257-
}
1258-
type = list(string)
1259-
default = []
1260-
}
1261-
12621248
variable "reuse_nat_ips" {
12631249
description = "Should be true if you don't want EIPs to be created for your NAT Gateways and will instead pass them in via the 'external_nat_ip_ids' variable"
12641250
type = bool

wrappers/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ module "wrapper" {
233233
name = try(each.value.name, var.defaults.name, "")
234234
nat_eip_tags = try(each.value.nat_eip_tags, var.defaults.nat_eip_tags, {})
235235
nat_gateway_destination_cidr_block = try(each.value.nat_gateway_destination_cidr_block, var.defaults.nat_gateway_destination_cidr_block, "0.0.0.0/0")
236+
nat_gateway_subnet_ids = try(each.value.nat_gateway_subnet_ids, var.defaults.nat_gateway_subnet_ids, [])
236237
nat_gateway_tags = try(each.value.nat_gateway_tags, var.defaults.nat_gateway_tags, {})
237238
one_nat_gateway_per_az = try(each.value.one_nat_gateway_per_az, var.defaults.one_nat_gateway_per_az, false)
238239
outpost_acl_tags = try(each.value.outpost_acl_tags, var.defaults.outpost_acl_tags, {})

0 commit comments

Comments
 (0)