From 85195a0f497901271051370c47b703e30c2e1510 Mon Sep 17 00:00:00 2001 From: Cosimo Meli Date: Fri, 8 Nov 2024 15:57:40 +0100 Subject: [PATCH 1/3] Add single_nat_gateway_subnet_index --- README.md | 1 + main.tf | 6 +++--- variables.tf | 6 ++++++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ac08bbbe7..352ce308a 100644 --- a/README.md +++ b/README.md @@ -578,6 +578,7 @@ No modules. | [reuse\_nat\_ips](#input\_reuse\_nat\_ips) | 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 | `bool` | `false` | no | | [secondary\_cidr\_blocks](#input\_secondary\_cidr\_blocks) | List of secondary CIDR blocks to associate with the VPC to extend the IP Address pool | `list(string)` | `[]` | no | | [single\_nat\_gateway](#input\_single\_nat\_gateway) | Should be true if you want to provision a single shared NAT Gateway across all of your private networks | `bool` | `false` | no | +| [single\_nat\_gateway\_subnet\_index](#input\_single\_nat\_gateway\_subnet\_index) | The index of the public subnet which should be used for the NAT Gateway. Only used when `single_nat_gateway` is true | `number` | `0` | no | | [tags](#input\_tags) | A map of tags to add to all resources | `map(string)` | `{}` | no | | [use\_ipam\_pool](#input\_use\_ipam\_pool) | Determines whether IPAM pool is used for CIDR allocation | `bool` | `false` | no | | [vpc\_flow\_log\_iam\_policy\_name](#input\_vpc\_flow\_log\_iam\_policy\_name) | Name of the IAM policy | `string` | `"vpc-flow-log-to-cloudwatch"` | no | diff --git a/main.tf b/main.tf index 77cba6715..2512c7b45 100644 --- a/main.tf +++ b/main.tf @@ -1084,14 +1084,14 @@ resource "aws_nat_gateway" "this" { ) subnet_id = element( aws_subnet.public[*].id, - var.single_nat_gateway ? 0 : count.index, + var.single_nat_gateway ? var.single_nat_gateway_subnet_index : count.index, ) tags = merge( { - "Name" = format( + "Name" = var.single_nat_gateway ? var.name : format( "${var.name}-%s", - element(var.azs, var.single_nat_gateway ? 0 : count.index), + element(var.azs, count.index), ) }, var.tags, diff --git a/variables.tf b/variables.tf index 39521ae15..5e7805e06 100644 --- a/variables.tf +++ b/variables.tf @@ -1210,6 +1210,12 @@ variable "single_nat_gateway" { default = false } +variable "single_nat_gateway_subnet_index" { + description = "The index of the public subnet used for the NAT Gateway. Only used when `single_nat_gateway` is true" + type = number + default = 0 +} + variable "one_nat_gateway_per_az" { description = "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`" type = bool From 1c1d04de31beaba1f9d154f1db712b2237ddba1f Mon Sep 17 00:00:00 2001 From: Cosimo Meli Date: Fri, 8 Nov 2024 16:16:41 +0100 Subject: [PATCH 2/3] Update example --- examples/complete/main.tf | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/complete/main.tf b/examples/complete/main.tf index 579a47395..8ff822061 100644 --- a/examples/complete/main.tf +++ b/examples/complete/main.tf @@ -54,6 +54,8 @@ module "vpc" { enable_nat_gateway = true single_nat_gateway = true + single_nat_gateway_subnet_index = 1 + customer_gateways = { IP1 = { bgp_asn = 65112 From 43cf1620c76c781d68ea22b00bcc69a230332955 Mon Sep 17 00:00:00 2001 From: Cosimo Meli Date: Fri, 8 Nov 2024 16:29:47 +0100 Subject: [PATCH 3/3] Fix docs --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 352ce308a..6cb32ab98 100644 --- a/README.md +++ b/README.md @@ -578,7 +578,7 @@ No modules. | [reuse\_nat\_ips](#input\_reuse\_nat\_ips) | 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 | `bool` | `false` | no | | [secondary\_cidr\_blocks](#input\_secondary\_cidr\_blocks) | List of secondary CIDR blocks to associate with the VPC to extend the IP Address pool | `list(string)` | `[]` | no | | [single\_nat\_gateway](#input\_single\_nat\_gateway) | Should be true if you want to provision a single shared NAT Gateway across all of your private networks | `bool` | `false` | no | -| [single\_nat\_gateway\_subnet\_index](#input\_single\_nat\_gateway\_subnet\_index) | The index of the public subnet which should be used for the NAT Gateway. Only used when `single_nat_gateway` is true | `number` | `0` | no | +| [single\_nat\_gateway\_subnet\_index](#input\_single\_nat\_gateway\_subnet\_index) | The index of the public subnet used for the NAT Gateway. Only used when `single_nat_gateway` is true | `number` | `0` | no | | [tags](#input\_tags) | A map of tags to add to all resources | `map(string)` | `{}` | no | | [use\_ipam\_pool](#input\_use\_ipam\_pool) | Determines whether IPAM pool is used for CIDR allocation | `bool` | `false` | no | | [vpc\_flow\_log\_iam\_policy\_name](#input\_vpc\_flow\_log\_iam\_policy\_name) | Name of the IAM policy | `string` | `"vpc-flow-log-to-cloudwatch"` | no |