Skip to content

Commit

Permalink
feat: Add support for multiple route tables to public and intra subne…
Browse files Browse the repository at this point in the history
…ts (#1051)

Co-authored-by: Kevin Snyder <kevinsnyder@Kevins-Raft-MacBook-Pro.local>
Co-authored-by: Bryant Biggs <bryantbiggs@gmail.com>
  • Loading branch information
3 people committed Apr 25, 2024
1 parent 15423e7 commit da05f24
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/antonbabenko/pre-commit-terraform
rev: v1.88.0
rev: v1.89.0
hooks:
- id: terraform_fmt
- id: terraform_docs
Expand All @@ -24,7 +24,7 @@ repos:
- '--args=--only=terraform_unused_required_providers'
- id: terraform_validate
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: v4.6.0
hooks:
- id: check-merge-conflict
- id: end-of-file-fixer
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,8 @@ No modules.
| <a name="input_create_flow_log_cloudwatch_iam_role"></a> [create\_flow\_log\_cloudwatch\_iam\_role](#input\_create\_flow\_log\_cloudwatch\_iam\_role) | Whether to create IAM role for VPC Flow Logs | `bool` | `false` | no |
| <a name="input_create_flow_log_cloudwatch_log_group"></a> [create\_flow\_log\_cloudwatch\_log\_group](#input\_create\_flow\_log\_cloudwatch\_log\_group) | Whether to create CloudWatch log group for VPC Flow Logs | `bool` | `false` | no |
| <a name="input_create_igw"></a> [create\_igw](#input\_create\_igw) | Controls if an Internet Gateway is created for public subnets and the related routes that connect them | `bool` | `true` | no |
| <a name="input_create_multiple_intra_route_tables"></a> [create\_multiple\_intra\_route\_tables](#input\_create\_multiple\_intra\_route\_tables) | Indicates whether to create a separate route table for each intra subnet. Default: `false` | `bool` | `false` | no |
| <a name="input_create_multiple_public_route_tables"></a> [create\_multiple\_public\_route\_tables](#input\_create\_multiple\_public\_route\_tables) | Indicates whether to create a separate route table for each public subnet. Default: `false` | `bool` | `false` | no |
| <a name="input_create_redshift_subnet_group"></a> [create\_redshift\_subnet\_group](#input\_create\_redshift\_subnet\_group) | Controls if redshift subnet group should be created | `bool` | `true` | no |
| <a name="input_create_redshift_subnet_route_table"></a> [create\_redshift\_subnet\_route\_table](#input\_create\_redshift\_subnet\_route\_table) | Controls if separate route table for redshift should be created | `bool` | `false` | no |
| <a name="input_create_vpc"></a> [create\_vpc](#input\_create\_vpc) | Controls if VPC should be created (it affects almost all resources) | `bool` | `true` | no |
Expand Down
30 changes: 24 additions & 6 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,22 @@ resource "aws_subnet" "public" {
)
}

locals {
num_public_route_tables = var.create_multiple_public_route_tables ? local.len_public_subnets : 1
}

resource "aws_route_table" "public" {
count = local.create_public_subnets ? 1 : 0
count = local.create_public_subnets ? local.num_public_route_tables : 0

vpc_id = local.vpc_id

tags = merge(
{ "Name" = "${var.name}-${var.public_subnet_suffix}" },
{
"Name" = var.create_multiple_public_route_tables ? format(
"${var.name}-${var.public_subnet_suffix}-%s",
element(var.azs, count.index),
) : "${var.name}-${var.public_subnet_suffix}"
},
var.tags,
var.public_route_table_tags,
)
Expand All @@ -139,7 +148,7 @@ resource "aws_route_table_association" "public" {
count = local.create_public_subnets ? local.len_public_subnets : 0

subnet_id = element(aws_subnet.public[*].id, count.index)
route_table_id = aws_route_table.public[0].id
route_table_id = element(aws_route_table.public[*].id, var.create_multiple_public_route_tables ? count.index : 0)
}

resource "aws_route" "public_internet_gateway" {
Expand Down Expand Up @@ -816,13 +825,22 @@ resource "aws_subnet" "intra" {
)
}

locals {
num_intra_route_tables = var.create_multiple_intra_route_tables ? local.len_intra_subnets : 1
}

resource "aws_route_table" "intra" {
count = local.create_intra_subnets ? 1 : 0
count = local.create_intra_subnets ? local.num_intra_route_tables : 0

vpc_id = local.vpc_id

tags = merge(
{ "Name" = "${var.name}-${var.intra_subnet_suffix}" },
{
"Name" = var.create_multiple_intra_route_tables ? format(
"${var.name}-${var.intra_subnet_suffix}-%s",
element(var.azs, count.index),
) : "${var.name}-${var.intra_subnet_suffix}"
},
var.tags,
var.intra_route_table_tags,
)
Expand All @@ -832,7 +850,7 @@ resource "aws_route_table_association" "intra" {
count = local.create_intra_subnets ? local.len_intra_subnets : 0

subnet_id = element(aws_subnet.intra[*].id, count.index)
route_table_id = element(aws_route_table.intra[*].id, 0)
route_table_id = element(aws_route_table.intra[*].id, var.create_multiple_intra_route_tables ? count.index : 0)
}

################################################################################
Expand Down
12 changes: 12 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ variable "public_subnet_enable_resource_name_dns_a_record_on_launch" {
default = false
}

variable "create_multiple_public_route_tables" {
description = "Indicates whether to create a separate route table for each public subnet. Default: `false`"
type = bool
default = false
}

variable "public_subnet_ipv6_prefixes" {
description = "Assigns IPv6 public subnet id based on the Amazon provided /56 prefix base 10 integer (0-256). Must be of equal length to the corresponding IPv4 subnet list"
type = list(string)
Expand Down Expand Up @@ -914,6 +920,12 @@ variable "intra_subnet_enable_resource_name_dns_a_record_on_launch" {
default = false
}

variable "create_multiple_intra_route_tables" {
description = "Indicates whether to create a separate route table for each intra subnet. Default: `false`"
type = bool
default = false
}

variable "intra_subnet_ipv6_prefixes" {
description = "Assigns IPv6 intra subnet id based on the Amazon provided /56 prefix base 10 integer (0-256). Must be of equal length to the corresponding IPv4 subnet list"
type = list(string)
Expand Down

0 comments on commit da05f24

Please sign in to comment.