Skip to content

Commit

Permalink
feat: Make TGW routing creation optional (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalis committed Dec 11, 2023
1 parent e522e72 commit 1661dfa
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ No modules.
| <a name="input_tgw_vpc_attachment_tags"></a> [tgw\_vpc\_attachment\_tags](#input\_tgw\_vpc\_attachment\_tags) | Additional tags for VPC attachments | `map(string)` | `{}` | no |
| <a name="input_timeouts"></a> [timeouts](#input\_timeouts) | Create, update, and delete timeout configurations for the transit gateway | `map(string)` | `{}` | no |
| <a name="input_transit_gateway_cidr_blocks"></a> [transit\_gateway\_cidr\_blocks](#input\_transit\_gateway\_cidr\_blocks) | One or more IPv4 or IPv6 CIDR blocks for the transit gateway. Must be a size /24 CIDR block or larger for IPv4, or a size /64 CIDR block or larger for IPv6 | `list(string)` | `[]` | no |
| <a name="input_create_tgw_routes"></a> [create\_tgw\_routes](#input\_create\_tgw\_routes) | Controls if TGW Route Table / Routes should be created | `bool` | `true` | no |
| <a name="input_transit_gateway_route_table_id"></a> [transit\_gateway\_route\_table\_id](#input\_transit\_gateway\_route\_table\_id) | Identifier of EC2 Transit Gateway Route Table to use with the Target Gateway when reusing it between multiple TGWs | `string` | `null` | no |
| <a name="input_vpc_attachments"></a> [vpc\_attachments](#input\_vpc\_attachments) | Maps of maps of VPC details to attach to TGW. Type 'any' to disable type validation by Terraform. | `any` | `{}` | no |

Expand Down
8 changes: 4 additions & 4 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ resource "aws_ec2_transit_gateway_vpc_attachment" "this" {
################################################################################

resource "aws_ec2_transit_gateway_route_table" "this" {
count = var.create_tgw ? 1 : 0
count = var.create_tgw && var.create_tgw_routes ? 1 : 0

transit_gateway_id = aws_ec2_transit_gateway.this[0].id

Expand All @@ -100,7 +100,7 @@ resource "aws_ec2_transit_gateway_route_table" "this" {
}

resource "aws_ec2_transit_gateway_route" "this" {
count = length(local.vpc_attachments_with_routes)
count = var.create_tgw_routes ? length(local.vpc_attachments_with_routes) : 0

destination_cidr_block = local.vpc_attachments_with_routes[count.index][1].destination_cidr_block
blackhole = try(local.vpc_attachments_with_routes[count.index][1].blackhole, null)
Expand All @@ -119,7 +119,7 @@ resource "aws_route" "this" {

resource "aws_ec2_transit_gateway_route_table_association" "this" {
for_each = {
for k, v in var.vpc_attachments : k => v if var.create_tgw && try(v.transit_gateway_default_route_table_association, true) != true
for k, v in var.vpc_attachments : k => v if var.create_tgw && var.create_tgw_routes && try(v.transit_gateway_default_route_table_association, true) != true
}

# Create association if it was not set already by aws_ec2_transit_gateway_vpc_attachment resource
Expand All @@ -129,7 +129,7 @@ resource "aws_ec2_transit_gateway_route_table_association" "this" {

resource "aws_ec2_transit_gateway_route_table_propagation" "this" {
for_each = {
for k, v in var.vpc_attachments : k => v if var.create_tgw && try(v.transit_gateway_default_route_table_propagation, true) != true
for k, v in var.vpc_attachments : k => v if var.create_tgw && var.create_tgw_routes && try(v.transit_gateway_default_route_table_propagation, true) != true
}

# Create association if it was not set already by aws_ec2_transit_gateway_vpc_attachment resource
Expand Down
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ variable "tgw_vpc_attachment_tags" {
# Route Table / Routes
################################################################################

variable "create_tgw_routes" {
description = "Controls if TGW Route Table / Routes should be created"
type = bool
default = true
}

variable "transit_gateway_route_table_id" {
description = "Identifier of EC2 Transit Gateway Route Table to use with the Target Gateway when reusing it between multiple TGWs"
type = string
Expand Down

0 comments on commit 1661dfa

Please sign in to comment.