Commit 323a995a authored by Loren Gordon's avatar Loren Gordon Committed by Anton Babenko

Creates a single private route table when single_nat_gateway is true (#83)

Fixes #82
parent b226dd21
...@@ -4,6 +4,7 @@ terraform { ...@@ -4,6 +4,7 @@ terraform {
locals { locals {
max_subnet_length = "${max(length(var.private_subnets), length(var.elasticache_subnets), length(var.database_subnets), length(var.redshift_subnets))}" max_subnet_length = "${max(length(var.private_subnets), length(var.elasticache_subnets), length(var.database_subnets), length(var.redshift_subnets))}"
nat_gateway_count = "${var.single_nat_gateway ? 1 : local.max_subnet_length}"
} }
###### ######
...@@ -80,11 +81,11 @@ resource "aws_route" "public_internet_gateway" { ...@@ -80,11 +81,11 @@ resource "aws_route" "public_internet_gateway" {
# There are so many routing tables as the largest amount of subnets of each type (really?) # There are so many routing tables as the largest amount of subnets of each type (really?)
################# #################
resource "aws_route_table" "private" { resource "aws_route_table" "private" {
count = "${var.create_vpc && local.max_subnet_length > 0 ? local.max_subnet_length : 0}" count = "${var.create_vpc && local.max_subnet_length > 0 ? local.nat_gateway_count : 0}"
vpc_id = "${aws_vpc.this.id}" vpc_id = "${aws_vpc.this.id}"
tags = "${merge(var.tags, var.private_route_table_tags, map("Name", format("%s-private-%s", var.name, element(var.azs, count.index))))}" tags = "${merge(var.tags, var.private_route_table_tags, map("Name", (var.single_nat_gateway ? "${var.name}-private" : format("%s-private-%s", var.name, element(var.azs, count.index)))))}"
lifecycle { lifecycle {
# When attaching VPN gateways it is common to define aws_vpn_gateway_route_propagation # When attaching VPN gateways it is common to define aws_vpn_gateway_route_propagation
...@@ -203,7 +204,7 @@ locals { ...@@ -203,7 +204,7 @@ locals {
} }
resource "aws_eip" "nat" { resource "aws_eip" "nat" {
count = "${var.create_vpc && (var.enable_nat_gateway && !var.reuse_nat_ips) ? (var.single_nat_gateway ? 1 : length(var.azs)) : 0}" count = "${var.create_vpc && (var.enable_nat_gateway && !var.reuse_nat_ips) ? local.nat_gateway_count : 0}"
vpc = true vpc = true
...@@ -211,7 +212,7 @@ resource "aws_eip" "nat" { ...@@ -211,7 +212,7 @@ resource "aws_eip" "nat" {
} }
resource "aws_nat_gateway" "this" { resource "aws_nat_gateway" "this" {
count = "${var.create_vpc && var.enable_nat_gateway ? (var.single_nat_gateway ? 1 : length(var.azs)) : 0}" count = "${var.create_vpc && var.enable_nat_gateway ? local.nat_gateway_count : 0}"
allocation_id = "${element(local.nat_gateway_ips, (var.single_nat_gateway ? 0 : count.index))}" allocation_id = "${element(local.nat_gateway_ips, (var.single_nat_gateway ? 0 : count.index))}"
subnet_id = "${element(aws_subnet.public.*.id, (var.single_nat_gateway ? 0 : count.index))}" subnet_id = "${element(aws_subnet.public.*.id, (var.single_nat_gateway ? 0 : count.index))}"
...@@ -222,7 +223,7 @@ resource "aws_nat_gateway" "this" { ...@@ -222,7 +223,7 @@ resource "aws_nat_gateway" "this" {
} }
resource "aws_route" "private_nat_gateway" { resource "aws_route" "private_nat_gateway" {
count = "${var.create_vpc && var.enable_nat_gateway ? length(var.private_subnets) : 0}" count = "${var.create_vpc && var.enable_nat_gateway ? local.nat_gateway_count : 0}"
route_table_id = "${element(aws_route_table.private.*.id, count.index)}" route_table_id = "${element(aws_route_table.private.*.id, count.index)}"
destination_cidr_block = "0.0.0.0/0" destination_cidr_block = "0.0.0.0/0"
...@@ -246,7 +247,7 @@ resource "aws_vpc_endpoint" "s3" { ...@@ -246,7 +247,7 @@ resource "aws_vpc_endpoint" "s3" {
} }
resource "aws_vpc_endpoint_route_table_association" "private_s3" { resource "aws_vpc_endpoint_route_table_association" "private_s3" {
count = "${var.create_vpc && var.enable_s3_endpoint ? length(var.private_subnets) : 0}" count = "${var.create_vpc && var.enable_s3_endpoint ? local.nat_gateway_count : 0}"
vpc_endpoint_id = "${aws_vpc_endpoint.s3.id}" vpc_endpoint_id = "${aws_vpc_endpoint.s3.id}"
route_table_id = "${element(aws_route_table.private.*.id, count.index)}" route_table_id = "${element(aws_route_table.private.*.id, count.index)}"
...@@ -276,7 +277,7 @@ resource "aws_vpc_endpoint" "dynamodb" { ...@@ -276,7 +277,7 @@ resource "aws_vpc_endpoint" "dynamodb" {
} }
resource "aws_vpc_endpoint_route_table_association" "private_dynamodb" { resource "aws_vpc_endpoint_route_table_association" "private_dynamodb" {
count = "${var.create_vpc && var.enable_dynamodb_endpoint ? length(var.private_subnets) : 0}" count = "${var.create_vpc && var.enable_dynamodb_endpoint ? local.nat_gateway_count : 0}"
vpc_endpoint_id = "${aws_vpc_endpoint.dynamodb.id}" vpc_endpoint_id = "${aws_vpc_endpoint.dynamodb.id}"
route_table_id = "${element(aws_route_table.private.*.id, count.index)}" route_table_id = "${element(aws_route_table.private.*.id, count.index)}"
...@@ -296,28 +297,28 @@ resource "aws_route_table_association" "private" { ...@@ -296,28 +297,28 @@ resource "aws_route_table_association" "private" {
count = "${var.create_vpc && length(var.private_subnets) > 0 ? length(var.private_subnets) : 0}" count = "${var.create_vpc && length(var.private_subnets) > 0 ? length(var.private_subnets) : 0}"
subnet_id = "${element(aws_subnet.private.*.id, count.index)}" subnet_id = "${element(aws_subnet.private.*.id, count.index)}"
route_table_id = "${element(aws_route_table.private.*.id, count.index)}" route_table_id = "${element(aws_route_table.private.*.id, (var.single_nat_gateway ? 0 : count.index))}"
} }
resource "aws_route_table_association" "database" { resource "aws_route_table_association" "database" {
count = "${var.create_vpc && length(var.database_subnets) > 0 ? length(var.database_subnets) : 0}" count = "${var.create_vpc && length(var.database_subnets) > 0 ? length(var.database_subnets) : 0}"
subnet_id = "${element(aws_subnet.database.*.id, count.index)}" subnet_id = "${element(aws_subnet.database.*.id, count.index)}"
route_table_id = "${element(aws_route_table.private.*.id, count.index)}" route_table_id = "${element(aws_route_table.private.*.id, (var.single_nat_gateway ? 0 : count.index))}"
} }
resource "aws_route_table_association" "redshift" { resource "aws_route_table_association" "redshift" {
count = "${var.create_vpc && length(var.redshift_subnets) > 0 ? length(var.redshift_subnets) : 0}" count = "${var.create_vpc && length(var.redshift_subnets) > 0 ? length(var.redshift_subnets) : 0}"
subnet_id = "${element(aws_subnet.redshift.*.id, count.index)}" subnet_id = "${element(aws_subnet.redshift.*.id, count.index)}"
route_table_id = "${element(aws_route_table.private.*.id, count.index)}" route_table_id = "${element(aws_route_table.private.*.id, (var.single_nat_gateway ? 0 : count.index))}"
} }
resource "aws_route_table_association" "elasticache" { resource "aws_route_table_association" "elasticache" {
count = "${var.create_vpc && length(var.elasticache_subnets) > 0 ? length(var.elasticache_subnets) : 0}" count = "${var.create_vpc && length(var.elasticache_subnets) > 0 ? length(var.elasticache_subnets) : 0}"
subnet_id = "${element(aws_subnet.elasticache.*.id, count.index)}" subnet_id = "${element(aws_subnet.elasticache.*.id, count.index)}"
route_table_id = "${element(aws_route_table.private.*.id, count.index)}" route_table_id = "${element(aws_route_table.private.*.id, (var.single_nat_gateway ? 0 : count.index))}"
} }
resource "aws_route_table_association" "public" { resource "aws_route_table_association" "public" {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment