Skip to content
Closed
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
27 changes: 15 additions & 12 deletions modules/route-table/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,20 @@ module "transit_gateway_route_table" {
name = "example"
transit_gateway_id = module.transit_gateway.id

associations = {
vpc1 = {
associations = [
{
transit_gateway_attachment_id = module.transit_gateway.vpc_attachments["vpc1"].id
propagate_route_table = true
}
vpc2 = {
replace_existing_association = true
},
{
transit_gateway_attachment_id = module.transit_gateway.vpc_attachments["vpc2"].id
propagate_route_table = true
}
}
},
]

propagations = [ module.transit_gateway.vpc_attachments["vpc1"].id, module.transit_gateway.vpc_attachments["vpc2"].id ]


routes = {
static_routes = {
blackhole = {
blackhole = true
destination_cidr_block = "0.0.0.0/0"
Expand Down Expand Up @@ -93,12 +95,13 @@ No modules.

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_associations"></a> [associations](#input\_associations) | A map of transit gateway attachment IDs to associate with the Transit Gateway route table | <pre>map(object({<br/> transit_gateway_attachment_id = optional(string)<br/> replace_existing_association = optional(bool)<br/> propagate_route_table = optional(bool, false)<br/> }))</pre> | `{}` | no |
| <a name="input_associations"></a> [associations](#input\_associations) | List of Transit Gateway Attachments ids to associate to the route table | <pre>list(object({<br/> transit_gateway_attachment_id = string<br/> replace_existing_association = optional(bool)<br/> }))</pre> | `[]` | no |
| <a name="input_create"></a> [create](#input\_create) | Controls if resources should be created (it affects almost all resources) | `bool` | `true` | no |
| <a name="input_name"></a> [name](#input\_name) | Name to be used on all the resources as identifier | `string` | `""` | no |
| <a name="input_routes"></a> [routes](#input\_routes) | A map of Transit Gateway routes to create in the route table | <pre>map(object({<br/> destination_cidr_block = string<br/> blackhole = optional(bool, false)<br/> transit_gateway_attachment_id = optional(string)<br/> }))</pre> | `{}` | no |
| <a name="input_propagations"></a> [propagations](#input\_propagations) | List of Transit Gateway Attachments ids to propagate to the route table | `list(string)` | `[]` | no |
| <a name="input_static_routes"></a> [static\_routes](#input\_static\_routes) | A map of Transit Gateway routes to create in the route table | <pre>list(object({<br/> destination_cidr_block = string<br/> blackhole = optional(bool, false)<br/> transit_gateway_attachment_id = optional(string)<br/> }))</pre> | `[]` | no |
| <a name="input_tags"></a> [tags](#input\_tags) | A map of tags to add to all resources | `map(string)` | `{}` | no |
| <a name="input_transit_gateway_id"></a> [transit\_gateway\_id](#input\_transit\_gateway\_id) | The ID of the EC2 Transit Gateway | `string` | `""` | no |
| <a name="input_transit_gateway_id"></a> [transit\_gateway\_id](#input\_transit\_gateway\_id) | The ID of the EC2 Transit Gateway for the route table | `string` | n/a | yes |
| <a name="input_vpc_routes"></a> [vpc\_routes](#input\_vpc\_routes) | A map of VPC routes to create in the route table provided | <pre>map(object({<br/> route_table_id = string<br/> destination_cidr_block = optional(string)<br/> destination_ipv6_cidr_block = optional(string)<br/> }))</pre> | `{}` | no |

## Outputs
Expand Down
8 changes: 4 additions & 4 deletions modules/route-table/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ resource "aws_ec2_transit_gateway_route_table" "this" {
}

resource "aws_ec2_transit_gateway_route_table_association" "this" {
for_each = { for k, v in var.associations : k => v if var.create }
for_each = { for a in var.associations : a.transit_gateway_attachment_id => a if var.create }

transit_gateway_attachment_id = each.value.transit_gateway_attachment_id
transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.this[0].id
replace_existing_association = try(each.value.replace_existing_association, null)
}

resource "aws_ec2_transit_gateway_route_table_propagation" "this" {
for_each = { for k, v in var.associations : k => v if var.create && try(v.propagate_route_table, false) }
for_each = { for p in var.propagations : p => p if var.create }

transit_gateway_attachment_id = each.value.transit_gateway_attachment_id
transit_gateway_attachment_id = each.value
transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.this[0].id
}

Expand All @@ -33,7 +33,7 @@ resource "aws_ec2_transit_gateway_route_table_propagation" "this" {
################################################################################

resource "aws_ec2_transit_gateway_route" "this" {
for_each = { for k, v in var.routes : k => v if var.create }
for_each = { for route in var.static_routes : route.destination_cidr_block => route if var.create }
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still use for_each to prevent changes, it's just the parameter that it's a list instead of a map. We use the destination_cidr_block as the key (it's a good candidate because duplicated cidrs are not allowed in the routes).

I think this simplifies using the module but if that's not a good Terraform practice we can stick with maps


destination_cidr_block = each.value.destination_cidr_block
blackhole = each.value.blackhole
Expand Down
24 changes: 14 additions & 10 deletions modules/route-table/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,37 @@ variable "tags" {
################################################################################

variable "transit_gateway_id" {
description = "The ID of the EC2 Transit Gateway"
description = "The ID of the EC2 Transit Gateway for the route table"
type = string
default = ""
}

variable "associations" {
description = "A map of transit gateway attachment IDs to associate with the Transit Gateway route table"
type = map(object({
transit_gateway_attachment_id = optional(string)
description = "List of Transit Gateway Attachments ids to associate to the route table"
type = list(object({
transit_gateway_attachment_id = string
replace_existing_association = optional(bool)
propagate_route_table = optional(bool, false)
}))
default = {}
default = []
}

variable "propagations" {
description = "List of Transit Gateway Attachments ids to propagate to the route table"
type = list(string)
default = []
}

################################################################################
# Route(s)
################################################################################

variable "routes" {
variable "static_routes" {
description = "A map of Transit Gateway routes to create in the route table"
type = map(object({
type = list(object({
destination_cidr_block = string
blackhole = optional(bool, false)
transit_gateway_attachment_id = optional(string)
}))
default = {}
default = []
}

variable "vpc_routes" {
Expand Down