Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ data "aws_route_table" "this" {

locals {
rtb_ids = concat(data.aws_route_table.this[*].id, var.route_table_ids)
map_of_tables_and_routes = merge(tolist(flatten([for id in local.rtb_ids : {for r in var.routes : "${id}_${r}"=> { rtb_id = id, route = r }}]))...)
map_of_tables_and_prefixes = merge(tolist(flatten([for id in local.rtb_ids : {for pl in var.prefix_list_ids : "${id}_${pl}" => { rtb_id = id, prefix_list_id = pl }}]))...)
map_of_tables_and_routes = merge(tolist(flatten([for id in local.rtb_ids : { for r in var.routes : "${id}_${r}" => { rtb_id = id, route = r } }]))...)
map_of_tables_and_prefixes = merge(tolist(flatten([for id in local.rtb_ids : { for pl in var.prefix_list_ids : "${id}_${pl}" => { rtb_id = id, prefix_list_id = pl } }]))...)
merged_routes = merge(local.map_of_tables_and_routes, local.map_of_tables_and_prefixes)
}

resource "aws_route" "to_tgw" {
for_each = local.merged_routes
route_table_id = lookup(each.value, "rtb_id")
transit_gateway_id = var.transit_gateway_id
for_each = local.merged_routes
route_table_id = lookup(each.value, "rtb_id")
# only one of transit_gateway_id or vpc_peering_connection_id may be set. So if the vpc_peer_conn_id is set this will be null.
transit_gateway_id = var.vpc_peering_connection_id != "" ? null : var.transit_gateway_id
destination_cidr_block = lookup(each.value, "route", null)
destination_prefix_list_id = lookup(each.value, "prefix_list_id", null)
vpc_peering_connection_id = var.vpc_peering_connection_id != "" ? var.vpc_peering_connection_id : null
}
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ variable "prefix_list_ids" {
default = []
type = list(string)
}

variable "vpc_peering_connection_id" {
description = "Optional ID of VPC peering connection - useful if you're creating routes for a peered VPC"
default = ""
type = string
}