Skip to content

Commit 66b54e7

Browse files
committed
feat: Allow adding custom routes to private route tables (#1130)
1 parent 9ffd9c6 commit 66b54e7

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

main.tf

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,22 @@ resource "aws_network_acl_rule" "public_outbound" {
229229

230230
locals {
231231
create_private_subnets = local.create_vpc && local.len_private_subnets > 0
232+
233+
private_route_table_routes_map = {
234+
for rt_id, routes in var.private_route_table_routes :
235+
rt_id => [
236+
for idx, route in routes : {
237+
key = "${rt_id}-${idx}"
238+
route_table_id = rt_id
239+
route = route
240+
}
241+
]
242+
}
243+
244+
private_route_table_routes_flat = {
245+
for route_item in flatten(values(local.private_route_table_routes_map)) :
246+
route_item.key => route_item
247+
}
232248
}
233249

234250
resource "aws_subnet" "private" {
@@ -287,6 +303,24 @@ resource "aws_route_table_association" "private" {
287303
)
288304
}
289305

306+
resource "aws_route" "private_route_table_routes" {
307+
for_each = local.private_route_table_routes_flat
308+
309+
route_table_id = each.value.route_table_id
310+
311+
# Route attributes
312+
destination_cidr_block = lookup(each.value.route, "destination_cidr_block", null)
313+
destination_ipv6_cidr_block = lookup(each.value.route, "destination_ipv6_cidr_block", null)
314+
egress_only_gateway_id = lookup(each.value.route, "egress_only_gateway_id", null)
315+
gateway_id = lookup(each.value.route, "gateway_id", null)
316+
nat_gateway_id = lookup(each.value.route, "nat_gateway_id", null)
317+
transit_gateway_id = lookup(each.value.route, "transit_gateway_id", null)
318+
vpc_peering_connection_id = lookup(each.value.route, "vpc_peering_connection_id", null)
319+
local_gateway_id = lookup(each.value.route, "local_gateway_id", null)
320+
carrier_gateway_id = lookup(each.value.route, "carrier_gateway_id", null)
321+
destination_prefix_list_id = lookup(each.value.route, "destination_prefix_list_id", null)
322+
}
323+
290324
################################################################################
291325
# Private Network ACLs
292326
################################################################################

variables.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,23 @@ variable "private_route_table_tags" {
396396
default = {}
397397
}
398398

399+
variable "private_route_table_routes" {
400+
description = "A map of private route table IDs to a list of route objects."
401+
type = map(list(object({
402+
destination_cidr_block = optional(string)
403+
destination_ipv6_cidr_block = optional(string)
404+
egress_only_gateway_id = optional(string)
405+
gateway_id = optional(string)
406+
nat_gateway_id = optional(string)
407+
transit_gateway_id = optional(string)
408+
vpc_peering_connection_id = optional(string)
409+
local_gateway_id = optional(string)
410+
carrier_gateway_id = optional(string)
411+
destination_prefix_list_id = optional(string)
412+
})))
413+
default = {}
414+
}
415+
399416
################################################################################
400417
# Private Network ACLs
401418
################################################################################

0 commit comments

Comments
 (0)