Skip to content

Commit 4b52da0

Browse files
authored
feat: add default route table resource to manage default route table, its tags, routes, etc. (#599)
1 parent ebe45b8 commit 4b52da0

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ No Modules.
249249
| [aws_customer_gateway](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/customer_gateway) |
250250
| [aws_db_subnet_group](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/db_subnet_group) |
251251
| [aws_default_network_acl](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/default_network_acl) |
252+
| [aws_default_route_table](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/default_route_table) |
252253
| [aws_default_security_group](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/default_security_group) |
253254
| [aws_default_vpc](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/default_vpc) |
254255
| [aws_egress_only_internet_gateway](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/egress_only_internet_gateway) |
@@ -385,6 +386,9 @@ No Modules.
385386
| default\_network\_acl\_ingress | List of maps of ingress rules to set on the Default Network ACL | `list(map(string))` | <pre>[<br> {<br> "action": "allow",<br> "cidr_block": "0.0.0.0/0",<br> "from_port": 0,<br> "protocol": "-1",<br> "rule_no": 100,<br> "to_port": 0<br> },<br> {<br> "action": "allow",<br> "from_port": 0,<br> "ipv6_cidr_block": "::/0",<br> "protocol": "-1",<br> "rule_no": 101,<br> "to_port": 0<br> }<br>]</pre> | no |
386387
| default\_network\_acl\_name | Name to be used on the Default Network ACL | `string` | `""` | no |
387388
| default\_network\_acl\_tags | Additional tags for the Default Network ACL | `map(string)` | `{}` | no |
389+
| default\_route\_table\_propagating\_vgws | List of virtual gateways for propagation | `list(string)` | `[]` | no |
390+
| default\_route\_table\_routes | Configuration block of routes. See https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/default_route_table#route | `list(map(string))` | `[]` | no |
391+
| default\_route\_table\_tags | Additional tags for the default route table | `map(string)` | `{}` | no |
388392
| default\_security\_group\_egress | List of maps of egress rules to set on the default security group | `list(map(string))` | `null` | no |
389393
| default\_security\_group\_ingress | List of maps of ingress rules to set on the default security group | `list(map(string))` | `null` | no |
390394
| default\_security\_group\_name | Name to be used on the default security group | `string` | `"default"` | no |
@@ -603,6 +607,7 @@ No Modules.
603607
| logs\_endpoint\_security\_group\_ids | The ID of one or more security groups to associate with the network interface for CloudWatch Logs endpoint | `list(string)` | `[]` | no |
604608
| logs\_endpoint\_subnet\_ids | The ID of one or more subnets in which to create a network interface for CloudWatch Logs endpoint. Only a single subnet within an AZ is supported. If omitted, private subnets will be used. | `list(string)` | `[]` | no |
605609
| manage\_default\_network\_acl | Should be true to adopt and manage Default Network ACL | `bool` | `false` | no |
610+
| manage\_default\_route\_table | Should be true to manage default route table | `bool` | `false` | no |
606611
| manage\_default\_security\_group | Should be true to adopt and manage default security group | `bool` | `false` | no |
607612
| manage\_default\_vpc | Should be true to adopt and manage Default VPC | `bool` | `false` | no |
608613
| map\_public\_ip\_on\_launch | Should be false if you do not want to auto-assign public IP on launch | `bool` | `true` | no |

examples/complete-vpc/main.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ module "vpc" {
2424

2525
create_database_subnet_group = false
2626

27+
manage_default_route_table = true
28+
default_route_table_tags = { DefaultRouteTable = true }
29+
2730
enable_dns_hostnames = true
2831
enable_dns_support = true
2932

main.tf

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,43 @@ resource "aws_egress_only_internet_gateway" "this" {
160160
)
161161
}
162162

163+
###############
164+
# Default route
165+
###############
166+
167+
resource "aws_default_route_table" "default" {
168+
count = var.create_vpc && var.manage_default_route_table ? 1 : 0
169+
170+
default_route_table_id = aws_vpc.this[0].default_route_table_id
171+
propagating_vgws = var.default_route_table_propagating_vgws
172+
173+
dynamic "route" {
174+
for_each = var.default_route_table_routes
175+
content {
176+
# One of the following destinations must be provided
177+
cidr_block = route.value.cidr_block
178+
ipv6_cidr_block = lookup(route.value, "ipv6_cidr_block", null)
179+
180+
# One of the following targets must be provided
181+
egress_only_gateway_id = lookup(route.value, "egress_only_gateway_id", null)
182+
gateway_id = lookup(route.value, "gateway_id", null)
183+
instance_id = lookup(route.value, "instance_id", null)
184+
nat_gateway_id = lookup(route.value, "nat_gateway_id", null)
185+
network_interface_id = lookup(route.value, "network_interface_id", null)
186+
transit_gateway_id = lookup(route.value, "transit_gateway_id", null)
187+
# `vpc_endpoint_id` was recently added in v3.15.0
188+
# vpc_endpoint_id = lookup(route.value, "vpc_endpoint_id", null)
189+
vpc_peering_connection_id = lookup(route.value, "vpc_peering_connection_id", null)
190+
}
191+
}
192+
193+
tags = merge(
194+
{ "Name" = var.name },
195+
var.tags,
196+
var.default_route_table_tags,
197+
)
198+
}
199+
163200
################
164201
# Publiс routes
165202
################

variables.tf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2213,6 +2213,30 @@ variable "propagate_public_route_tables_vgw" {
22132213
default = false
22142214
}
22152215

2216+
variable "manage_default_route_table" {
2217+
description = "Should be true to manage default route table"
2218+
type = bool
2219+
default = false
2220+
}
2221+
2222+
variable "default_route_table_propagating_vgws" {
2223+
description = "List of virtual gateways for propagation"
2224+
type = list(string)
2225+
default = []
2226+
}
2227+
2228+
variable "default_route_table_routes" {
2229+
description = "Configuration block of routes. See https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/default_route_table#route"
2230+
type = list(map(string))
2231+
default = []
2232+
}
2233+
2234+
variable "default_route_table_tags" {
2235+
description = "Additional tags for the default route table"
2236+
type = map(string)
2237+
default = {}
2238+
}
2239+
22162240
variable "tags" {
22172241
description = "A map of tags to add to all resources"
22182242
type = map(string)

0 commit comments

Comments
 (0)