diff --git a/README.md b/README.md
index 97d959771..b5e336ad2 100644
--- a/README.md
+++ b/README.md
@@ -579,6 +579,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 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/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
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 095cc8bdf..c7bc7427a 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