Skip to content

Commit

Permalink
Merge pull request #1 from AlexisColes/nat-gateway-secondary-ips
Browse files Browse the repository at this point in the history
feat: Nat gateway secondary ips
  • Loading branch information
AlexisColes authored Aug 2, 2024
2 parents 2e417ad + 04d8ce6 commit b92bce8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
19 changes: 19 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,7 @@ resource "aws_route" "private_ipv6_egress" {
locals {
nat_gateway_count = var.single_nat_gateway ? 1 : var.one_nat_gateway_per_az ? length(var.azs) : local.max_subnet_length
nat_gateway_ips = var.reuse_nat_ips ? var.external_nat_ip_ids : aws_eip.nat[*].id
seips_suffixs = [for num in range(0, var.number_of_secondary_eips_per_gateway) : "s${num + 1}"]
}

resource "aws_eip" "nat" {
Expand All @@ -1074,6 +1075,22 @@ resource "aws_eip" "nat" {
depends_on = [aws_internet_gateway.this]
}

resource "aws_eip" "secondary" {
for_each = toset(flatten([for nat in aws_eip.nat : [for suffix in local.seips_suffixs : "${nat.tags.Name}-${suffix}"]]))

domain = "vpc"

tags = merge(
{
"Name" = each.key,
},
var.tags,
var.nat_eip_tags,
)

depends_on = [aws_internet_gateway.this]
}

resource "aws_nat_gateway" "this" {
count = local.create_vpc && var.enable_nat_gateway ? local.nat_gateway_count : 0

Expand All @@ -1086,6 +1103,8 @@ resource "aws_nat_gateway" "this" {
var.single_nat_gateway ? 0 : count.index,
)

secondary_allocation_ids = [for suffix in local.seips_suffixs : aws_eip.secondary["${aws_eip.nat[count.index].tags.Name}-${suffix}"].allocation_id]

tags = merge(
{
"Name" = format(
Expand Down
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,12 @@ variable "external_nat_ips" {
default = []
}

variable "number_of_secondary_eips_per_gateway" {
description = "how many secondary eips per natgateway"
type = number
default = 0
}

variable "nat_gateway_tags" {
description = "Additional tags for the NAT gateways"
type = map(string)
Expand Down

0 comments on commit b92bce8

Please sign in to comment.