Skip to content

Commit edfa117

Browse files
committed
feat: added the ability to provision route table entries for the subnets
1 parent 303e5f0 commit edfa117

File tree

5 files changed

+131
-2
lines changed

5 files changed

+131
-2
lines changed

modules/shared/locals.tf

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ locals {
66
subnets = merge([
77
for k, v in var.subnets : {
88
for i, cidr in v.cidrs : format("%s-%s", k, cidr) => {
9-
name = format("%s-%s-%s", var.name, k, data.aws_availability_zones.current.names[i])
109
availability_zone = data.aws_availability_zones.current.names[i]
1110
cidr_block = cidr
11+
name = format("%s-%s-%s", var.name, k, data.aws_availability_zones.current.names[i])
12+
subnet_prefix = format("%s-%s-", var.name, k)
13+
subnet_name = k
1214
}
1315
}
1416
]...)
@@ -35,4 +37,12 @@ locals {
3537

3638
## A collection of tags to apply to the subnets
3739
tags = merge(var.tags, {})
40+
41+
## A collection of subnets ids by the name
42+
subnets_ids_by_name = {
43+
for k, v in var.subnets : k => [for s in aws_subnet.subnets : s.id if startswith(s.tags["Name"], format("%s-%s-", var.name, k))]
44+
}
45+
46+
## A list of all the subnet ids
47+
subnets_ids = flatten([for k, v in local.subnets_ids_by_name : local.subnets_ids_by_name[k]])
3848
}

modules/shared/outputs.tf

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,28 @@ output "outbound_network_acls" {
88
description = "The outbound network ACLs provisioned"
99
value = local.network_outbound_acls
1010
}
11+
12+
output "subnets_map" {
13+
description = "A map of the subnets"
14+
value = local.subnets
15+
}
16+
17+
output "subnet_ids_by_name" {
18+
description = "A map of the subnets ids by the name"
19+
value = local.subnets_ids_by_name
20+
}
21+
22+
output "subnet_ids" {
23+
description = "A list of all the subnet ids"
24+
value = local.subnets_ids
25+
}
26+
27+
output "route_table" {
28+
description = "The route table provisioned"
29+
value = aws_route_table.current.id
30+
}
31+
32+
output "route_table_arn" {
33+
description = "The route table arn"
34+
value = aws_route_table.current.arn
35+
}

modules/shared/routes.tf

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## Related to the route tables
2+
3+
## Provision a route table for the subnets
4+
resource "aws_route_table" "current" {
5+
vpc_id = var.vpc_id
6+
tags = merge(local.tags, { Name = var.name })
7+
}
8+
9+
## Associate all the subnets with the routing table
10+
resource "aws_route_table_association" "current" {
11+
for_each = aws_subnet.subnets
12+
13+
subnet_id = each.value.id
14+
route_table_id = aws_route_table.current.id
15+
}
16+
17+
## Provision routing entries
18+
resource "aws_route" "current" {
19+
for_each = {
20+
for idx, v in var.routes : v.cidr => v
21+
}
22+
23+
carrier_gateway_id = each.value.carrier_gateway_id
24+
core_network_arn = each.value.core_network_arn
25+
destination_cidr_block = each.value.cidr
26+
egress_only_gateway_id = each.value.egress_only_gateway_id
27+
gateway_id = each.value.gateway_id
28+
local_gateway_id = each.value.local_gateway_id
29+
nat_gateway_id = each.value.nat_gateway_id
30+
network_interface_id = each.value.network_interface_id
31+
route_table_id = aws_route_table.current.id
32+
vpc_peering_connection_id = each.value.vpc_peering_connection_id
33+
}

modules/shared/variables.tf

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ variable "share" {
1717
organizational_units = optional(list(string), [])
1818
## A list of organizational units to share the subnets with
1919
})
20+
default = {}
2021
}
2122

2223
variable "permitted_subnets" {
@@ -35,11 +36,39 @@ variable "subnets" {
3536
description = "A collectionn of subnets to provision for the tenant"
3637
type = map(object({
3738
cidrs = list(string)
38-
## The cidr block to provision the subnets (optional)
3939
}))
4040
default = {}
4141
}
4242

43+
variable "routes" {
44+
description = "A collection of routes to add to the subnets"
45+
type = list(object({
46+
cidr = string
47+
## The cidr block to provision the subnets (optional)
48+
carrier_gateway_id = optional(string, null)
49+
## Identifier of a carrier gateway. This attribute can only be used when the VPC contains a subnet which is associated with a Wavelength Zone.
50+
core_network_arn = optional(string, null)
51+
## The Amazon Resource Name (ARN) of a core network.
52+
egress_only_gateway_id = optional(string, null)
53+
## Identifier of a VPC Egress Only Internet Gateway.
54+
gateway_id = optional(string, null)
55+
## Identifier of a VPC internet gateway or a virtual private gateway. Specify local when updating a previously imported local route.
56+
nat_gateway_id = optional(string, null)
57+
## Identifier of a VPC NAT gateway.
58+
local_gateway_id = optional(string, null)
59+
## Identifier of a Outpost local gateway.
60+
network_interface_id = optional(string, null)
61+
## Identifier of an EC2 network interface.
62+
transit_gateway_id = optional(string, null)
63+
## Identifier of an EC2 Transit Gateway.
64+
vpc_endpoint_id = optional(string, null)
65+
## Identifier of a VPC Endpoint.
66+
vpc_peering_connection_id = optional(string, null)
67+
## Identifier of a VPC peering connection.
68+
}))
69+
default = []
70+
}
71+
4372
variable "tags" {
4473
description = "A map of tags to apply to the NACL"
4574
type = map(string)

tests/subnets.tftest.hcl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ run "validation_subnets_module" {
4444
cidrs = ["10.90.10.0/24", "10.90.11.0/24"]
4545
}
4646
}
47+
48+
routes = [
49+
{
50+
cidr = "0.0.0.0/0"
51+
gateway_id = "tgw-12345678"
52+
}
53+
]
4754
}
4855

4956
assert {
@@ -191,4 +198,29 @@ run "validation_subnets_module" {
191198
condition = aws_ram_principal_association.accounts["123456789012"] != null
192199
error_message = "The expected the ram principal association for the accounts was not found"
193200
}
201+
202+
assert {
203+
condition = aws_route_table.current != null && aws_route_table.current.vpc_id == "vpc-12345678" && aws_route_table.current.tags["Name"] == "test"
204+
error_message = "The expected the route table was not found"
205+
}
206+
207+
assert {
208+
condition = aws_route_table_association.current["app-10.90.10.0/24"] != null
209+
error_message = "The expected the route table association for the app subnets was not found"
210+
}
211+
212+
assert {
213+
condition = aws_route_table_association.current["app-10.90.11.0/24"] != null && aws_route_table_association.current["app-10.90.10.0/24"] != null
214+
error_message = "The expected the route table association for the app subnets was not found"
215+
}
216+
217+
assert {
218+
condition = aws_route_table_association.current["web-10.90.0.0/24"] != null && aws_route_table_association.current["web-10.90.1.0/24"] != null
219+
error_message = "The expected the route table association for the web subnets was not found"
220+
}
221+
222+
assert {
223+
condition = aws_route.current["0.0.0.0/0"].gateway_id == "tgw-12345678"
224+
error_message = "The expected the route was not found"
225+
}
194226
}

0 commit comments

Comments
 (0)