Skip to content

Commit f1ae03e

Browse files
committed
Configure additional routes for the subnets
1 parent 5abfd22 commit f1ae03e

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed

data.tf

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,37 @@ locals {
113113
# private subnets with cidrs per az if connect_to_public_eigw = true ... "privatetwo/us-east-1a"
114114
private_subnet_names_egress_routed = [for subnet in local.private_per_az : subnet if contains(local.private_subnets_egress_routed, split("/", subnet)[0])]
115115

116+
# Configure additional routes.
117+
# List of private subnets with additional routes.
118+
private_subnets_routes = [for type in local.private_subnet_names : type if length(lookup(var.subnets[type], "routes", [])) > 0]
119+
# List of routes for the private subnets.
120+
# The tricky part here is that we have to identify the subnet names where a route
121+
# should be added because the subnet name has suffix /az_name e.g. subnet is created for each zone.
122+
private_subnets_az_routes = flatten([
123+
for subnet_name in local.private_subnets_routes :
124+
[
125+
for subnet_name_az in local.private_per_az :
126+
[
127+
for route in var.subnets[subnet_name].routes :
128+
merge(route, { "route_table_name" : subnet_name_az })
129+
] if startswith(subnet_name_az, "${subnet_name}/")
130+
]
131+
])
132+
# List of routes for the public subnets.
133+
# There is just 1 public subnet and route table names are based on AZ names.
134+
public_subnet_az_routes = (length(try(var.subnets.public.routes, [])) > 0
135+
? flatten(
136+
[
137+
for az in local.azs :
138+
[
139+
for route in var.subnets.public.routes :
140+
merge(route, { "route_table_name" : az })
141+
]
142+
]
143+
)
144+
: []
145+
)
146+
116147
# VPC LATTICE ############################################################
117148
# If var.vpc_lattice is defined (default = {}), the VPC association is created.
118149
lattice_association = length(keys(var.vpc_lattice)) > 0

main.tf

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,28 @@ resource "aws_route" "public_to_cwan" {
210210
]
211211
}
212212

213+
# Configure routes provided in the input variable `subnets` for the public subnet(s).
214+
resource "aws_route" "public_routes" {
215+
count = length(local.public_subnet_az_routes)
216+
217+
destination_cidr_block = lookup(local.public_subnet_az_routes[count.index], "destination_cidr_block", null)
218+
destination_prefix_list_id = lookup(local.public_subnet_az_routes[count.index], "destination_prefix_list_id", null)
219+
destination_ipv6_cidr_block = lookup(local.public_subnet_az_routes[count.index], "destination_ipv6_cidr_block", null)
220+
221+
carrier_gateway_id = lookup(local.public_subnet_az_routes[count.index], "carrier_gateway_id", null)
222+
core_network_arn = lookup(local.public_subnet_az_routes[count.index], "core_network_arn", null)
223+
egress_only_gateway_id = lookup(local.public_subnet_az_routes[count.index], "egress_only_gateway_id", null)
224+
gateway_id = lookup(local.public_subnet_az_routes[count.index], "gateway_id", null)
225+
nat_gateway_id = lookup(local.public_subnet_az_routes[count.index], "nat_gateway_id", null)
226+
local_gateway_id = lookup(local.public_subnet_az_routes[count.index], "local_gateway_id", null)
227+
network_interface_id = lookup(local.public_subnet_az_routes[count.index], "network_interface_id", null)
228+
transit_gateway_id = lookup(local.public_subnet_az_routes[count.index], "transit_gateway_id", null)
229+
vpc_endpoint_id = lookup(local.public_subnet_az_routes[count.index], "vpc_endpoint_id", null)
230+
vpc_peering_connection_id = lookup(local.public_subnet_az_routes[count.index], "vpc_peering_connection_id", null)
231+
232+
route_table_id = aws_route_table.private[local.public_subnet_az_routes[count.index].route_table_name].id
233+
}
234+
213235
# Route: IPv6 routes from public subnets to AWS Cloud WAN's core network (if configured in var.core_network_routes)
214236
resource "aws_route" "ipv6_public_to_cwan" {
215237
for_each = (contains(local.subnet_keys, "public") && contains(local.ipv6_subnets_cwan_routed, "public") && local.create_cwan_routes) ? toset(local.azs) : toset([])
@@ -290,6 +312,28 @@ resource "aws_route" "private_to_egress_only" {
290312
egress_only_gateway_id = aws_egress_only_internet_gateway.eigw[0].id
291313
}
292314

315+
# Configure routes provided in the input variable `subnets` for the private subnet(s).
316+
resource "aws_route" "private_routes" {
317+
count = length(local.private_subnets_az_routes)
318+
319+
destination_cidr_block = lookup(local.private_subnets_az_routes[count.index], "destination_cidr_block", null)
320+
destination_prefix_list_id = lookup(local.private_subnets_az_routes[count.index], "destination_prefix_list_id", null)
321+
destination_ipv6_cidr_block = lookup(local.private_subnets_az_routes[count.index], "destination_ipv6_cidr_block", null)
322+
323+
carrier_gateway_id = lookup(local.private_subnets_az_routes[count.index], "carrier_gateway_id", null)
324+
core_network_arn = lookup(local.private_subnets_az_routes[count.index], "core_network_arn", null)
325+
egress_only_gateway_id = lookup(local.private_subnets_az_routes[count.index], "egress_only_gateway_id", null)
326+
gateway_id = lookup(local.private_subnets_az_routes[count.index], "gateway_id", null)
327+
nat_gateway_id = lookup(local.private_subnets_az_routes[count.index], "nat_gateway_id", null)
328+
local_gateway_id = lookup(local.private_subnets_az_routes[count.index], "local_gateway_id", null)
329+
network_interface_id = lookup(local.private_subnets_az_routes[count.index], "network_interface_id", null)
330+
transit_gateway_id = lookup(local.private_subnets_az_routes[count.index], "transit_gateway_id", null)
331+
vpc_endpoint_id = lookup(local.private_subnets_az_routes[count.index], "vpc_endpoint_id", null)
332+
vpc_peering_connection_id = lookup(local.private_subnets_az_routes[count.index], "vpc_peering_connection_id", null)
333+
334+
route_table_id = aws_route_table.private[local.private_subnets_az_routes[count.index].route_table_name].id
335+
}
336+
293337
# Route: IPv4 routes from private subnets to the Transit Gateway (if configured in var.transit_gateway_routes)
294338
resource "aws_route" "private_to_tgw" {
295339
for_each = toset(local.private_subnet_key_names_tgw_routed)
@@ -521,4 +565,4 @@ resource "aws_vpclattice_service_network_vpc_association" "vpc_lattice_service_n
521565
module.tags.tags_aws,
522566
module.vpc_lattice_tags.tags_aws
523567
)
524-
}
568+
}

variables.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ variable "subnets" {
112112
- `assign_ipv6_cidr` = (Optional|bool) **Cannot set if `ipv6_cidrs` is set.** If true, it will calculate a /64 block from the IPv6 VPC CIDR to set in the subnets.
113113
- `ipv6_cidrs` = (Optional|list(string)) **Cannot set if `assign_ipv6_cidr` is set.** List of IPv6 CIDRs to set to subnets. The subnet size must use a /64 prefix length. Count of CIDRs defined must match quantity of azs in `az_count`.
114114
- `name_prefix` = (Optional|String) A string prefix to use for the name of your subnet and associated resources. Subnet type key name is used if omitted (aka private, public, transit_gateway). Example `name_prefix = "private"` for `var.subnets.private` is redundant.
115+
- `routes` = (optional|list(map(string)) List of maps, where each map represents an `aws_route` resource. All `aws_route` attributes are supported for both the `destination` and `target` arguments.
115116
- `tags` = (Optional|map(string)) Tags to set on the subnet and associated resources.
116117
117118
**Any private subnet type options:**
@@ -161,6 +162,14 @@ variable "subnets" {
161162
assign_ipv6_cidr = true
162163
connect_to_eigw = true
163164
}
165+
# Additional routes
166+
private = {
167+
netmask = 24
168+
routes = [{
169+
destination_cidr_block = "0.0.0.0/0"
170+
transit_gateway_id = "tgw-01238768912345678"
171+
}]
172+
}
164173
# Transit gateway subnets (dual-stack)
165174
transit_gateway = {
166175
netmask = 24

0 commit comments

Comments
 (0)