Skip to content

Commit 466ee64

Browse files
committed
feat: Allow specifying additional TGW routes in attached VPCs
Adds parameter `tgw_additional_vpc_cidrs` to the `vpc_attachments` map that enables adding additional `aws_route` resources that send traffic across the TGW peering connection. Changes the name of the existing `aws_route` resource from `this` since there are now more than one in the state file.
1 parent 01789bd commit 466ee64

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ module "vpc" {
6868

6969
| Name | Version |
7070
|------|---------|
71-
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.1 |
71+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.1 |
7272
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.4 |
7373

7474
## Providers
@@ -96,7 +96,8 @@ No modules.
9696
| [aws_ram_resource_association.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ram_resource_association) | resource |
9797
| [aws_ram_resource_share.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ram_resource_share) | resource |
9898
| [aws_ram_resource_share_accepter.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ram_resource_share_accepter) | resource |
99-
| [aws_route.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route) | resource |
99+
| [aws_route.additional_cidrs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route) | resource |
100+
| [aws_route.destination_cidr](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route) | resource |
100101

101102
## Inputs
102103

examples/multi-account/main.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ module "tgw_peer" {
105105
transit_gateway_default_route_table_propagation = false
106106

107107
vpc_route_table_ids = module.vpc1.private_route_table_ids
108-
tgw_destination_cidr = "0.0.0.0/0"
108+
tgw_destination_cidr = "10.0.0.0/8"
109+
tgw_additional_cidrs = ["172.0.0/12"]
109110

110111
tgw_routes = [
111112
{

main.tf

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ locals {
1919
}
2020
]
2121
])
22+
23+
vpc_route_table_additional_cidrs = flatten([
24+
for k, v in var.vpc_attachments : [
25+
for rtb_id in try(v.vpc_route_table_ids, []) : [
26+
for cidr in try(v.tgw_additional_cidrs, []) : {
27+
rtb_id = rtb_id
28+
cidr = cidr
29+
tgw_id = var.create_tgw ? aws_ec2_transit_gateway.this[0].id : v.tgw_id
30+
}
31+
]
32+
]
33+
])
2234
}
2335

2436
################################################################################
@@ -112,7 +124,7 @@ resource "aws_ec2_transit_gateway_route" "this" {
112124
transit_gateway_attachment_id = tobool(try(local.vpc_attachments_with_routes[count.index][1].blackhole, false)) == false ? aws_ec2_transit_gateway_vpc_attachment.this[local.vpc_attachments_with_routes[count.index][0].key].id : null
113125
}
114126

115-
resource "aws_route" "this" {
127+
resource "aws_route" "destination_cidr" {
116128
for_each = { for x in local.vpc_route_table_destination_cidr : x.rtb_id => {
117129
cidr = x.cidr,
118130
tgw_id = x.tgw_id
@@ -124,6 +136,24 @@ resource "aws_route" "this" {
124136
transit_gateway_id = each.value["tgw_id"]
125137
}
126138

139+
moved {
140+
from = aws_route.this
141+
to = aws_route.destination_cidr
142+
}
143+
144+
resource "aws_route" "additional_cidrs" {
145+
for_each = { for x in local.vpc_route_table_additional_cidrs : "${x.rtb_id}_${x.cidr}" => {
146+
cidr = x.cidr
147+
rtb_id = x.rtb_id
148+
tgw_id = x.tgw_id
149+
} }
150+
151+
route_table_id = each.value["rtb_id"]
152+
destination_cidr_block = try(each.value.ipv6_support, false) ? null : each.value["cidr"]
153+
destination_ipv6_cidr_block = try(each.value.ipv6_support, false) ? each.value["cidr"] : null
154+
transit_gateway_id = each.value["tgw_id"]
155+
}
156+
127157
resource "aws_ec2_transit_gateway_route_table_association" "this" {
128158
for_each = {
129159
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

versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
terraform {
2-
required_version = ">= 0.13.1"
2+
required_version = ">= 1.1"
33

44
required_providers {
55
aws = {

0 commit comments

Comments
 (0)