Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor conditional creation of NAT gateway for EKS #189

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions aws/eks/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ module "eks-vpc" {
module "eks-vpc-nat-gateway" {
source = "../nat_gateway"

uses_nat_gateway = var.uses_nat_gateway
count = var.uses_nat_gateway ? 1 : 0
exclude_availability_zones = var.subnet_module.exclude_names
internet_gateway_id = module.eks-vpc.internet_gateway_id
name = var.name
vpc_id = module.eks-vpc.vpc_id
subnet_cidr_netnum_offset = 100 # So that it doesn't vary based on capacity
vpc_id = module.eks-vpc.vpc_id

tags = merge(
local.tags,
Expand All @@ -38,7 +38,7 @@ module "eks-subnets" {
netnum_offset = var.subnet_module.netnum_offset

internet_gateway_id = module.eks-vpc.internet_gateway_id
nat_gateway_id = var.uses_nat_gateway ? module.eks-vpc-nat-gateway.nat_gateway_id : 0
nat_gateway_id = var.uses_nat_gateway ? module.eks-vpc-nat-gateway[0].nat_gateway_id : 0

tags = merge(
local.tags,
Expand Down
14 changes: 5 additions & 9 deletions aws/nat_gateway/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ data "aws_vpc" "current" {
}

resource "aws_subnet" "mod" {
count = var.uses_nat_gateway ? length(data.aws_availability_zones.available.names) : 0
count = length(data.aws_availability_zones.available.names)
availability_zone = element(data.aws_availability_zones.available.names, count.index)
cidr_block = cidrsubnet(
data.aws_vpc.current.cidr_block,
Expand All @@ -24,15 +24,13 @@ resource "aws_subnet" "mod" {

# ElasticIP address for use with the NAT Gateway
resource "aws_eip" "nat-gw-eip" {
count = var.uses_nat_gateway ? 1 : 0
vpc = true
tags = var.tags
}

# NAT Gateway in the first subnet
resource "aws_nat_gateway" "gw" {
count = var.uses_nat_gateway ? 1 : 0
allocation_id = aws_eip.nat-gw-eip[0].id
allocation_id = aws_eip.nat-gw-eip.id
subnet_id = aws_subnet.mod[0].id

tags = merge(
Expand All @@ -45,20 +43,18 @@ resource "aws_nat_gateway" "gw" {
}

resource "aws_route_table" "mod" {
count = var.uses_nat_gateway ? 1 : 0
tags = var.tags
vpc_id = var.vpc_id
}

resource "aws_route" "mod" {
count = var.uses_nat_gateway ? 1 : 0
destination_cidr_block = "0.0.0.0/0"
gateway_id = var.internet_gateway_id
route_table_id = aws_route_table.mod[0].id
route_table_id = aws_route_table.mod.id
}

resource "aws_route_table_association" "mod" {
count = var.uses_nat_gateway ? length(data.aws_availability_zones.available.names) : 0
route_table_id = aws_route_table.mod[0].id
count = length(data.aws_availability_zones.available.names)
route_table_id = aws_route_table.mod.id
subnet_id = element(aws_subnet.mod[*].id, count.index)
}
2 changes: 1 addition & 1 deletion aws/nat_gateway/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
output "nat_gateway_id" {
value = var.uses_nat_gateway ? aws_nat_gateway.gw[0].id : -1
value = aws_nat_gateway.gw.id
}
6 changes: 0 additions & 6 deletions aws/nat_gateway/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ variable "internet_gateway_id" {
description = "Internet Gateway router for internet traffic"
}

variable "uses_nat_gateway" {
description = "Enable creation of this NAT Gateway and associated subnet/routes"
default = false
type = bool
}

variable "exclude_availability_zones" {
description = "Which AZ(s) should NOT be used (all other zones will have a subnet created)"
type = list(string)
Expand Down