Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Default Route Table #62

Merged
merged 18 commits into from
Jun 28, 2022
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ Available targets:
| [aws_region.requester](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
| [aws_route_table.requester](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route_table) | data source |
| [aws_route_tables.accepter](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route_tables) | data source |
| [aws_route_tables.default_rts](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route_tables) | data source |
| [aws_subnet_ids.requester](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnet_ids) | data source |
| [aws_subnets.accepter](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnets) | data source |
| [aws_vpc.accepter](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/vpc) | data source |
Expand Down Expand Up @@ -419,6 +420,7 @@ Available targets:
|------|-------------|
| <a name="output_accepter_accept_status"></a> [accepter\_accept\_status](#output\_accepter\_accept\_status) | Accepter VPC peering connection request status |
| <a name="output_accepter_connection_id"></a> [accepter\_connection\_id](#output\_accepter\_connection\_id) | Accepter VPC peering connection ID |
| <a name="output_accepter_subnet_route_table_map"></a> [accepter\_subnet\_route\_table\_map](#output\_accepter\_subnet\_route\_table\_map) | Map of accepter VPC subnet IDs to route table IDs |
| <a name="output_requester_accept_status"></a> [requester\_accept\_status](#output\_requester\_accept\_status) | Requester VPC peering connection request status |
| <a name="output_requester_connection_id"></a> [requester\_connection\_id](#output\_requester\_connection\_id) | Requester VPC peering connection ID |
<!-- markdownlint-restore -->
Expand Down
34 changes: 31 additions & 3 deletions accepter.tf
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,48 @@ data "aws_subnets" "accepter" {
name = "vpc-id"
values = [local.accepter_vpc_id]
}
Benbentwo marked this conversation as resolved.
Show resolved Hide resolved
dynamic "filter" {
for_each = var.accepter_subnet_tags
content {
name = "tag:${filter.key}"
values = [filter.value]
}
}
Benbentwo marked this conversation as resolved.
Show resolved Hide resolved
}

locals {
accepter_subnet_ids = try(local.accepter_enabled ? data.aws_subnets.accepter[0].ids : [], [])
accepter_subnet_ids = local.accepter_enabled ? data.aws_subnets.accepter[0].ids : []
accepter_subnet_ids_count = length(local.accepter_subnet_ids)
accepter_vpc_id = join("", data.aws_vpc.accepter.*.id)
accepter_account_id = join("", data.aws_caller_identity.accepter.*.account_id)
accepter_region = join("", data.aws_region.accepter.*.name)
}

# Lookup accepter route tables
data "aws_route_tables" "accepter" {
for_each = toset(local.accepter_subnet_ids)
provider = aws.accepter
vpc_id = local.accepter_vpc_id
filter {
name = "association.subnet-id"
values = [each.key]
}
}

# If we had more subnets than routetables, we should update the default.
Benbentwo marked this conversation as resolved.
Show resolved Hide resolved
data "aws_route_tables" "default_rts" {
count = local.count
provider = aws.accepter
vpc_id = local.accepter_vpc_id
filter {
name = "association.main"
values = ["true"]
}
}

locals {
accepter_aws_route_table_ids = try(distinct(sort(data.aws_route_tables.accepter[0].ids)), [])
accepter_aws_default_rt_id = join("", flatten(data.aws_route_tables.default_rts.*.ids))
accepter_aws_rt_map = { for s in local.accepter_subnet_ids : s => try(data.aws_route_tables.accepter[s].ids[0], local.accepter_aws_default_rt_id) }
accepter_aws_route_table_ids = distinct(sort(values(local.accepter_aws_rt_map)))
accepter_aws_route_table_ids_count = length(local.accepter_aws_route_table_ids)
accepter_cidr_block_associations = flatten(data.aws_vpc.accepter.*.cidr_block_associations)
accepter_cidr_block_associations_count = length(local.accepter_cidr_block_associations)
Expand Down Expand Up @@ -126,3 +149,8 @@ output "accepter_accept_status" {
value = join("", aws_vpc_peering_connection_accepter.accepter.*.accept_status)
description = "Accepter VPC peering connection request status"
}

output "accepter_subnet_route_table_map" {
value = local.accepter_aws_rt_map
description = "Map of accepter VPC subnet IDs to route table IDs"
}
Benbentwo marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions docs/terraform.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
| [aws_region.requester](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
| [aws_route_table.requester](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route_table) | data source |
| [aws_route_tables.accepter](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route_tables) | data source |
| [aws_route_tables.default_rts](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route_tables) | data source |
| [aws_subnet_ids.requester](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnet_ids) | data source |
| [aws_subnets.accepter](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnets) | data source |
| [aws_vpc.accepter](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/vpc) | data source |
Expand Down Expand Up @@ -97,6 +98,7 @@
|------|-------------|
| <a name="output_accepter_accept_status"></a> [accepter\_accept\_status](#output\_accepter\_accept\_status) | Accepter VPC peering connection request status |
| <a name="output_accepter_connection_id"></a> [accepter\_connection\_id](#output\_accepter\_connection\_id) | Accepter VPC peering connection ID |
| <a name="output_accepter_subnet_route_table_map"></a> [accepter\_subnet\_route\_table\_map](#output\_accepter\_subnet\_route\_table\_map) | Map of accepter VPC subnet IDs to route table IDs |
| <a name="output_requester_accept_status"></a> [requester\_accept\_status](#output\_requester\_accept\_status) | Requester VPC peering connection request status |
| <a name="output_requester_connection_id"></a> [requester\_connection\_id](#output\_requester\_connection\_id) | Requester VPC peering connection ID |
<!-- markdownlint-restore -->