From 1661dfa3f538c8d5b4f612a7c0982e4afd20daca Mon Sep 17 00:00:00 2001
From: Vitaly Gorodetsky <36814+vitalis@users.noreply.github.com>
Date: Mon, 11 Dec 2023 15:30:00 +0200
Subject: [PATCH] feat: Make TGW routing creation optional (#119)
---
README.md | 1 +
main.tf | 8 ++++----
variables.tf | 6 ++++++
3 files changed, 11 insertions(+), 4 deletions(-)
diff --git a/README.md b/README.md
index b219ea4..a33b6f3 100644
--- a/README.md
+++ b/README.md
@@ -125,6 +125,7 @@ No modules.
| [tgw\_vpc\_attachment\_tags](#input\_tgw\_vpc\_attachment\_tags) | Additional tags for VPC attachments | `map(string)` | `{}` | no |
| [timeouts](#input\_timeouts) | Create, update, and delete timeout configurations for the transit gateway | `map(string)` | `{}` | no |
| [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 |
+| [create\_tgw\_routes](#input\_create\_tgw\_routes) | Controls if TGW Route Table / Routes should be created | `bool` | `true` | no |
| [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 |
| [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 |
diff --git a/main.tf b/main.tf
index 7a2fe44..e7b833b 100644
--- a/main.tf
+++ b/main.tf
@@ -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
@@ -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)
@@ -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
@@ -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
diff --git a/variables.tf b/variables.tf
index 0154bf6..029c701 100644
--- a/variables.tf
+++ b/variables.tf
@@ -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