Skip to content

Commit e270733

Browse files
authored
Merge pull request #188 from tablexi/rd-make_nat_gateway_also_create_public_subnets
Change NAT gateway module to create a separate subnet for each AZ.
2 parents cd99151 + 679dbdc commit e270733

File tree

5 files changed

+36
-25
lines changed

5 files changed

+36
-25
lines changed

aws/eks/main.tf

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ module "eks-vpc" {
1616
module "eks-vpc-nat-gateway" {
1717
source = "../nat_gateway"
1818

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
19+
uses_nat_gateway = var.uses_nat_gateway
20+
exclude_availability_zones = var.subnet_module.exclude_names
21+
internet_gateway_id = module.eks-vpc.internet_gateway_id
22+
name = var.name
23+
vpc_id = module.eks-vpc.vpc_id
24+
subnet_cidr_netnum_offset = 100 # So that it doesn't vary based on capacity
2425

2526
tags = merge(
2627
local.tags,
@@ -36,7 +37,9 @@ module "eks-subnets" {
3637
exclude_names = var.subnet_module.exclude_names
3738
netnum_offset = var.subnet_module.netnum_offset
3839

39-
gateway_id = var.uses_nat_gateway ? module.eks-vpc-nat-gateway.nat_gateway_id : module.eks-vpc.internet_gateway_id
40+
internet_gateway_id = module.eks-vpc.internet_gateway_id
41+
nat_gateway_id = var.uses_nat_gateway ? module.eks-vpc-nat-gateway.nat_gateway_id : 0
42+
4043
tags = merge(
4144
local.tags,
4245
{

aws/nat_gateway/main.tf

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,35 @@
1-
# Create a single subnet for the NAT Gateway to live in
2-
# routed to the outside world
1+
# Create subnets for use by the LoadBalancer for ingress
2+
# And use the first of these subnets for the NAT Gateway
3+
4+
data "aws_availability_zones" "available" {
5+
exclude_names = var.exclude_availability_zones
6+
}
37

48
data "aws_vpc" "current" {
59
id = var.vpc_id
610
}
711

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
1212
resource "aws_subnet" "mod" {
13-
count = var.uses_nat_gateway ? 1 : 0
14-
availability_zone = var.availability_zone
13+
count = var.uses_nat_gateway ? length(data.aws_availability_zones.available.names) : 0
14+
availability_zone = element(data.aws_availability_zones.available.names, count.index)
1515
cidr_block = cidrsubnet(
1616
data.aws_vpc.current.cidr_block,
1717
var.subnet_cidr_newbits,
18-
var.subnet_cidr_netnum_offset + 1,
18+
var.subnet_cidr_netnum_offset + count.index + 1,
1919
)
2020
map_public_ip_on_launch = true
2121
tags = var.tags
2222
vpc_id = var.vpc_id
2323
}
2424

25-
2625
# ElasticIP address for use with the NAT Gateway
2726
resource "aws_eip" "nat-gw-eip" {
2827
count = var.uses_nat_gateway ? 1 : 0
2928
vpc = true
3029
tags = var.tags
3130
}
3231

33-
# NAT Gateway in the first (only) subnet
32+
# NAT Gateway in the first subnet
3433
resource "aws_nat_gateway" "gw" {
3534
count = var.uses_nat_gateway ? 1 : 0
3635
allocation_id = aws_eip.nat-gw-eip[0].id
@@ -59,7 +58,7 @@ resource "aws_route" "mod" {
5958
}
6059

6160
resource "aws_route_table_association" "mod" {
62-
count = var.uses_nat_gateway ? 1 : 0
61+
count = var.uses_nat_gateway ? length(data.aws_availability_zones.available.names) : 0
6362
route_table_id = aws_route_table.mod[0].id
64-
subnet_id = aws_subnet.mod[0].id
63+
subnet_id = element(aws_subnet.mod[*].id, count.index)
6564
}

aws/nat_gateway/variables.tf

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ variable "uses_nat_gateway" {
2121
type = bool
2222
}
2323

24-
variable "availability_zone" {
25-
description = "Which AZ to create the NAT Gateway"
26-
default = "us-east-1a"
24+
variable "exclude_availability_zones" {
25+
description = "Which AZ(s) should NOT be used (all other zones will have a subnet created)"
26+
type = list(string)
27+
default = []
2728
}
2829

2930
variable "subnet_cidr_newbits" {

aws/vpc/subnets/main.tf

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ resource "aws_route_table" "mod" {
1212
vpc_id = var.vpc_id
1313
}
1414

15+
# no nat-gateway, create with internet_gateway
16+
# nat-gateway, create with nat_gateway, not internet_gateway
1517
resource "aws_route" "mod" {
1618
count = var.public ? 1 : 0
1719
destination_cidr_block = "0.0.0.0/0"
18-
gateway_id = var.gateway_id
20+
gateway_id = var.nat_gateway_id == 0 ? var.internet_gateway_id : null
21+
nat_gateway_id = var.nat_gateway_id == 0 ? null : var.nat_gateway_id
1922
route_table_id = aws_route_table.mod.id
2023
}
2124

aws/vpc/subnets/variables.tf

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

5-
variable "gateway_id" {
6-
description = "The unique ID of the Internet (or NAT) gateway."
5+
variable "internet_gateway_id" {
6+
description = "The unique ID of the Internet gateway."
7+
}
8+
9+
variable "nat_gateway_id" {
10+
description = "The ID of the NAT Gateway (if applicable)"
11+
default = 0
712
}
813

914
variable "public" {

0 commit comments

Comments
 (0)