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

Commit

Permalink
Extend nat-gateways modules to enable support for EIP switching
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
psibi authored and ketzacoatl committed Apr 15, 2020
1 parent fc2ddc2 commit b427f65
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
31 changes: 21 additions & 10 deletions modules/nat-gateways/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,32 @@
*
*/

locals {
total_nat_count = var.enable_nat_creation ? var.nat_count : 0
total_new_nat = var.enable_nat_creation ? (length(var.nat_eip) == 0 ? local.total_nat_count : 0) : 0
nat_ids = var.enable_nat_creation ? (length(var.nat_eip) == 0 ? aws_eip.nat.*.id : values(data.aws_eip.nat)[*].id) : []
}

# AWS Managed NAT Gateways
resource "aws_eip" "nat" {
count = var.nat_count
count = local.total_new_nat
vpc = true
}

data "aws_eip" "nat" {
for_each = length(var.nat_eip) != 0 ? toset(var.nat_eip) : toset([])
public_ip = each.value
}

data "aws_subnet" "public" {
count = length(var.public_subnet_ids)
id = element(var.public_subnet_ids, count.index)
}

resource "aws_nat_gateway" "nat" {
count = var.nat_count
count = local.total_nat_count
subnet_id = element(data.aws_subnet.public.*.id, count.index)
allocation_id = element(aws_eip.nat.*.id, count.index)
allocation_id = element(local.nat_ids, count.index)

tags = merge(
{
Expand All @@ -35,7 +46,7 @@ resource "aws_nat_gateway" "nat" {

# Route tables. One per NAT gateway.
resource "aws_route_table" "private" {
count = var.nat_count
count = local.total_nat_count
vpc_id = var.vpc_id

tags = merge(
Expand All @@ -47,15 +58,15 @@ resource "aws_route_table" "private" {
}

resource "aws_route" "private_nat_gateway" {
count = var.nat_count
route_table_id = aws_route_table.private[count.index].id
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = element(aws_nat_gateway.nat.*.id, count.index)
count = local.total_nat_count
route_table_id = aws_route_table.private[count.index].id
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = element(aws_nat_gateway.nat.*.id, count.index)
}

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

11 changes: 11 additions & 0 deletions modules/nat-gateways/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,14 @@ variable "extra_tags" {
type = map(string)
}

variable "enable_nat_creation" {
default = true
description = "boolean, enable/disable NAT creation"
type = string
}

variable "nat_eip" {
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."
type = list(string)
default = []
}

0 comments on commit b427f65

Please sign in to comment.