Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Commit b427f65

Browse files
psibiketzacoatl
authored andcommitted
Extend nat-gateways modules to enable support for EIP switching
This is a backward compatible change with no updates needed to existing modules. The additional variables can be used for scenarions where migrating a EIP from one NAT to another is required.
1 parent fc2ddc2 commit b427f65

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

modules/nat-gateways/main.tf

+21-10
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,32 @@
99
*
1010
*/
1111

12+
locals {
13+
total_nat_count = var.enable_nat_creation ? var.nat_count : 0
14+
total_new_nat = var.enable_nat_creation ? (length(var.nat_eip) == 0 ? local.total_nat_count : 0) : 0
15+
nat_ids = var.enable_nat_creation ? (length(var.nat_eip) == 0 ? aws_eip.nat.*.id : values(data.aws_eip.nat)[*].id) : []
16+
}
17+
1218
# AWS Managed NAT Gateways
1319
resource "aws_eip" "nat" {
14-
count = var.nat_count
20+
count = local.total_new_nat
1521
vpc = true
1622
}
1723

24+
data "aws_eip" "nat" {
25+
for_each = length(var.nat_eip) != 0 ? toset(var.nat_eip) : toset([])
26+
public_ip = each.value
27+
}
28+
1829
data "aws_subnet" "public" {
1930
count = length(var.public_subnet_ids)
2031
id = element(var.public_subnet_ids, count.index)
2132
}
2233

2334
resource "aws_nat_gateway" "nat" {
24-
count = var.nat_count
35+
count = local.total_nat_count
2536
subnet_id = element(data.aws_subnet.public.*.id, count.index)
26-
allocation_id = element(aws_eip.nat.*.id, count.index)
37+
allocation_id = element(local.nat_ids, count.index)
2738

2839
tags = merge(
2940
{
@@ -35,7 +46,7 @@ resource "aws_nat_gateway" "nat" {
3546

3647
# Route tables. One per NAT gateway.
3748
resource "aws_route_table" "private" {
38-
count = var.nat_count
49+
count = local.total_nat_count
3950
vpc_id = var.vpc_id
4051

4152
tags = merge(
@@ -47,15 +58,15 @@ resource "aws_route_table" "private" {
4758
}
4859

4960
resource "aws_route" "private_nat_gateway" {
50-
count = var.nat_count
51-
route_table_id = aws_route_table.private[count.index].id
52-
destination_cidr_block = "0.0.0.0/0"
53-
nat_gateway_id = element(aws_nat_gateway.nat.*.id, count.index)
61+
count = local.total_nat_count
62+
route_table_id = aws_route_table.private[count.index].id
63+
destination_cidr_block = "0.0.0.0/0"
64+
nat_gateway_id = element(aws_nat_gateway.nat.*.id, count.index)
5465
}
5566

67+
# https://github.com/terraform-providers/terraform-provider-aws/pull/6999
5668
resource "aws_route_table_association" "private-rta" {
57-
count = length(var.private_subnet_ids)
69+
count = var.enable_nat_creation ? length(var.private_subnet_ids) : 0
5870
subnet_id = element(var.private_subnet_ids, count.index)
5971
route_table_id = element(aws_route_table.private.*.id, count.index)
6072
}
61-

modules/nat-gateways/variables.tf

+11
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,14 @@ variable "extra_tags" {
2929
type = map(string)
3030
}
3131

32+
variable "enable_nat_creation" {
33+
default = true
34+
description = "boolean, enable/disable NAT creation"
35+
type = string
36+
}
37+
38+
variable "nat_eip" {
39+
description = "The public IP of the specific EIP to retrieve. If non empty, this list should have same number of EIP as the number of var.public_subnet_ids."
40+
type = list(string)
41+
default = []
42+
}

0 commit comments

Comments
 (0)