Skip to content

Commit 89ae336

Browse files
authored
Merge pull request #182 from tablexi/add_nat_gateway_to_eks
Create NAT Gateway for EKS traffic.
2 parents 4d1a9e7 + 5c9705d commit 89ae336

8 files changed

Lines changed: 133 additions & 6 deletions

File tree

aws/eks/main.tf

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,30 @@ module "eks-vpc" {
1313
)
1414
}
1515

16+
module "eks-vpc-nat-gateway" {
17+
source = "../nat_gateway"
18+
19+
uses_nat_gateway = var.uses_nat_gateway
20+
internet_gateway_id = module.eks-vpc.internet_gateway_id
21+
name = var.name
22+
vpc_id = module.eks-vpc.vpc_id
23+
subnet_cidr_netnum_offset = 100 # So that it doesn't vary based on capacity
24+
25+
tags = merge(
26+
local.tags,
27+
{
28+
"kubernetes.io/cluster/${var.name}" = "shared"
29+
},
30+
)
31+
}
32+
1633
module "eks-subnets" {
1734
source = "../vpc/subnets"
1835

1936
exclude_names = var.subnet_module.exclude_names
2037
netnum_offset = var.subnet_module.netnum_offset
2138

22-
internet_gateway_id = module.eks-vpc.internet_gateway_id
39+
gateway_id = var.uses_nat_gateway ? module.eks-vpc-nat-gateway.nat_gateway_id : module.eks-vpc.internet_gateway_id
2340
tags = merge(
2441
local.tags,
2542
{

aws/eks/outputs.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,3 @@ output "subnets" {
2525
output "vpc_id" {
2626
value = module.eks-vpc.vpc_id
2727
}
28-

aws/eks/variables.tf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ variable "capacity_max" {
1313
default = 6
1414
}
1515

16+
variable "uses_nat_gateway" {
17+
description = "Create a NAT Gateway for all outgoing internet traffic"
18+
default = false
19+
type = bool
20+
}
21+
1622
variable "ec2_ssh_key" {
1723
description = "(Optional) EC2 Key Pair name that provides access for SSH communication with the worker nodes in the EKS Node Group."
1824
type = string
@@ -31,7 +37,7 @@ variable "subnet_module" {
3137
exclude_names = list(string)
3238
netnum_offset = number
3339
})
34-
default = {
40+
default = {
3541
exclude_names = []
3642
netnum_offset = 0
3743
}

aws/nat_gateway/main.tf

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Create a single subnet for the NAT Gateway to live in
2+
# routed to the outside world
3+
4+
data "aws_vpc" "current" {
5+
id = var.vpc_id
6+
}
7+
8+
# Create a subnet in us-east-1a in the
9+
# CIDR block specified by the inputs
10+
# So that the CIDR block is different than
11+
# others in this VPC
12+
resource "aws_subnet" "mod" {
13+
count = var.uses_nat_gateway ? 1 : 0
14+
availability_zone = var.availability_zone
15+
cidr_block = cidrsubnet(
16+
data.aws_vpc.current.cidr_block,
17+
var.subnet_cidr_newbits,
18+
var.subnet_cidr_netnum_offset + 1,
19+
)
20+
map_public_ip_on_launch = true
21+
tags = var.tags
22+
vpc_id = var.vpc_id
23+
}
24+
25+
26+
# ElasticIP address for use with the NAT Gateway
27+
resource "aws_eip" "nat-gw-eip" {
28+
count = var.uses_nat_gateway ? 1 : 0
29+
vpc = true
30+
tags = var.tags
31+
}
32+
33+
# NAT Gateway in the first (only) subnet
34+
resource "aws_nat_gateway" "gw" {
35+
count = var.uses_nat_gateway ? 1 : 0
36+
allocation_id = aws_eip.nat-gw-eip[0].id
37+
subnet_id = aws_subnet.mod[0].id
38+
39+
tags = merge(
40+
var.tags,
41+
{
42+
Name = var.name
43+
},
44+
)
45+
depends_on = [var.internet_gateway_id]
46+
}
47+
48+
resource "aws_route_table" "mod" {
49+
count = var.uses_nat_gateway ? 1 : 0
50+
tags = var.tags
51+
vpc_id = var.vpc_id
52+
}
53+
54+
resource "aws_route" "mod" {
55+
count = var.uses_nat_gateway ? 1 : 0
56+
destination_cidr_block = "0.0.0.0/0"
57+
gateway_id = var.internet_gateway_id
58+
route_table_id = aws_route_table.mod[0].id
59+
}
60+
61+
resource "aws_route_table_association" "mod" {
62+
count = var.uses_nat_gateway ? 1 : 0
63+
route_table_id = aws_route_table.mod[0].id
64+
subnet_id = aws_subnet.mod[0].id
65+
}

aws/nat_gateway/outputs.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "nat_gateway_id" {
2+
value = var.uses_nat_gateway ? aws_nat_gateway.gw[0].id : -1
3+
}

aws/nat_gateway/variables.tf

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
variable "name" {
2+
description = "Name of the gateway"
3+
}
4+
5+
variable "vpc_id" {
6+
description = "The unique ID of the VPC."
7+
}
8+
9+
variable "tags" {
10+
description = "(Optional) A mapping of tags to assign to the resources"
11+
type = map(string)
12+
}
13+
14+
variable "internet_gateway_id" {
15+
description = "Internet Gateway router for internet traffic"
16+
}
17+
18+
variable "uses_nat_gateway" {
19+
description = "Enable creation of this NAT Gateway and associated subnet/routes"
20+
default = false
21+
type = bool
22+
}
23+
24+
variable "availability_zone" {
25+
description = "Which AZ to create the NAT Gateway"
26+
default = "us-east-1a"
27+
}
28+
29+
variable "subnet_cidr_newbits" {
30+
default = 8
31+
description = "The subnets modifier of a routing mask or decrease the scope of the cidr_block by the given bits."
32+
}
33+
34+
variable "subnet_cidr_netnum_offset" {
35+
default = 0
36+
description = "Offset the subnets netnum by a specific amount."
37+
}

aws/vpc/subnets/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ resource "aws_route_table" "mod" {
1515
resource "aws_route" "mod" {
1616
count = var.public ? 1 : 0
1717
destination_cidr_block = "0.0.0.0/0"
18-
gateway_id = var.internet_gateway_id
18+
gateway_id = var.gateway_id
1919
route_table_id = aws_route_table.mod.id
2020
}
2121

aws/vpc/subnets/variables.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ variable "vpc_id" {
22
description = "The unique ID of the VPC."
33
}
44

5-
variable "internet_gateway_id" {
6-
description = "The unique ID of the Internet gateway."
5+
variable "gateway_id" {
6+
description = "The unique ID of the Internet (or NAT) gateway."
77
}
88

99
variable "public" {

0 commit comments

Comments
 (0)