Skip to content

Commit

Permalink
Change NAT gateway route for the database route table to use the data…
Browse files Browse the repository at this point in the history
…base route tables instead of the private route tables
  • Loading branch information
bmickunas committed Jul 30, 2020
1 parent 75a1888 commit d90e3cd
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
3 changes: 3 additions & 0 deletions examples/database_route_table/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Example: Database Subnet Route Table

Example script for provisioning dedicated database subnets, route table and NAT gateways alongside public and private subnets. It was originally created to resolve an issue where the database route table failed to provision a route for the NAT gateway.
9 changes: 9 additions & 0 deletions examples/database_route_table/env/Dev.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
region = "us-east-1"

# applicaion config
environment = "Dev"
vpc_cidr_prefix = "10.120"
application_name = "DBRouteTableTest"

region_prefix = "VA"

64 changes: 64 additions & 0 deletions examples/database_route_table/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
provider "aws" {
version = "~> 2.0"
region = var.region
}

locals {
availability_zones = ["${var.region}a", "${var.region}b", "${var.region}c"]
private_subnets = ["${var.vpc_cidr_prefix}.0.0/20", "${var.vpc_cidr_prefix}.16.0/20", "${var.vpc_cidr_prefix}.32.0/20"]
database_subnets = ["${var.vpc_cidr_prefix}.48.0/24", "${var.vpc_cidr_prefix}.50.0/24"]
public_subnets = ["${var.vpc_cidr_prefix}.128.0/20", "${var.vpc_cidr_prefix}.144.0/20", "${var.vpc_cidr_prefix}.160.0/20"]
}

module "vpc" {
# source = "terraform-aws-modules/vpc/aws"
# version = "2.0"
source = "../.."

name = "${var.region_prefix}-${var.environment}-${var.application_name}-VPC"
cidr = "${var.vpc_cidr_prefix}.0.0/16"

azs = local.availability_zones
private_subnets = local.private_subnets
private_subnet_tags = {
Name = "${var.region_prefix}-${var.environment}-${var.application_name}-VPC-PrivateSubnet"
Application_Role = "Private Subnet"
}
public_subnets = local.public_subnets
public_subnet_tags = {
Name = "${var.region_prefix}-${var.environment}-${var.application_name}-VPC-PublicSubnet"
Application_Role = "Public Subnet"
}

database_subnet_suffix = "Database"
database_subnets = local.database_subnets
database_subnet_tags = {
Name = "${var.region_prefix}-${var.environment}-${var.application_name}-VPC-DatabaseSubnet"
Application_Role = "Database Subnet"
}
create_database_subnet_route_table = true
create_database_subnet_group = false
create_database_nat_gateway_route = true
database_route_table_tags = {
Name = "${var.region_prefix}-${var.environment}-${var.application_name}-RT-Database"
Application_Role = "Database Route Table"
}

enable_nat_gateway = true
# this means we use 1 NAT gateway which is cheaper but not HA if an AZ goes down
# we need to determine if multiple NAT gateways are needed (especially in a dev environment)
single_nat_gateway = true

enable_dns_hostnames = true
enable_dns_support = true
enable_dhcp_options = true

enable_s3_endpoint = false
enable_dynamodb_endpoint = false

tags = {
Name = "${var.region_prefix}-${var.environment}-${var.application_name}-VPC"
Application_Role = "Network"
Terraform = "true"
}
}
19 changes: 19 additions & 0 deletions examples/database_route_table/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
variable "region" {
description = "The AWS region to create the VPC"
}

variable "environment" {
description = "The name of the environment (ex. Dev)"
}

variable "vpc_cidr_prefix" {
description = "The first two parts of the ipv4 cidr block (ex. 10.120)"
}

variable "region_prefix" {
description = "The abbreviation of the region. i.e. VA, OH, etc."
}

variable "application_name" {
description = "The name of the application to be used for naming resources"
}
2 changes: 1 addition & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ resource "aws_route" "database_internet_gateway" {
resource "aws_route" "database_nat_gateway" {
count = var.create_vpc && var.create_database_subnet_route_table && length(var.database_subnets) > 0 && false == var.create_database_internet_gateway_route && var.create_database_nat_gateway_route && 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.database.*.id, count.index)
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = element(aws_nat_gateway.this.*.id, count.index)

Expand Down

0 comments on commit d90e3cd

Please sign in to comment.