Skip to content

Commit

Permalink
Re-support Default Routing Table #44 (#62)
Browse files Browse the repository at this point in the history
* revert PR #44

* Auto Format

* Apply suggestions from code review

Co-authored-by: Andriy Knysh <[email protected]>

* revert accidental dleetion

* pr comments

* Auto Format

* more pr comments

* Auto Format

* ids not id

* Auto Format

* working map

* Auto Format

* remove extra tests for now

* revert more tests

* Apply suggestions from code review

Co-authored-by: Andriy Knysh <[email protected]>

* Auto Format

* PR Comments

* Apply suggestions from code review

Co-authored-by: Nuru <[email protected]>

Co-authored-by: cloudpossebot <[email protected]>
Co-authored-by: Andriy Knysh <[email protected]>
Co-authored-by: Nuru <[email protected]>
  • Loading branch information
4 people committed Jun 28, 2022
1 parent 67f1be2 commit 33a24a3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 18 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,11 @@ Available targets:
| [aws_caller_identity.requester](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
| [aws_region.accepter](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
| [aws_region.requester](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
| [aws_route_table.accepter](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route_table) | data source |
| [aws_route_table.requester](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route_table) | data source |
| [aws_subnet_ids.accepter](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnet_ids) | 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 |
| [aws_vpc.requester](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
52 changes: 38 additions & 14 deletions accepter.tf
Original file line number Diff line number Diff line change
Expand Up @@ -48,46 +48,65 @@ data "aws_vpc" "accepter" {
}

# Lookup accepter subnets
data "aws_subnet_ids" "accepter" {
data "aws_subnets" "accepter" {
count = local.accepter_count
provider = aws.accepter
vpc_id = local.accepter_vpc_id
tags = var.accepter_subnet_tags
filter {
name = "vpc-id"
values = [local.accepter_vpc_id]
}
tags = var.accepter_subnet_tags
}

locals {
accepter_subnet_ids = try(distinct(sort(flatten(data.aws_subnet_ids.accepter.*.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_table" "accepter" {
count = local.accepter_enabled ? local.accepter_subnet_ids_count : 0
provider = aws.accepter
subnet_id = element(local.accepter_subnet_ids, count.index)
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.
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_table.accepter.*.route_table_id)), [])
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 = try(flatten(data.aws_vpc.accepter.*.cidr_block_associations), [])
accepter_cidr_block_associations = flatten(data.aws_vpc.accepter.*.cidr_block_associations)
accepter_cidr_block_associations_count = length(local.accepter_cidr_block_associations)
}

# Create routes from accepter to requester
resource "aws_route" "accepter" {
count = local.accepter_enabled ? local.accepter_aws_route_table_ids_count * local.requester_cidr_block_associations_count : 0
count = local.enabled ? local.accepter_aws_route_table_ids_count * local.requester_cidr_block_associations_count : 0
provider = aws.accepter
route_table_id = local.accepter_aws_route_table_ids[floor(count.index / local.requester_cidr_block_associations_count)]
destination_cidr_block = local.requester_cidr_block_associations[count.index % local.requester_cidr_block_associations_count]["cidr_block"]
vpc_peering_connection_id = join("", aws_vpc_peering_connection.requester.*.id)
depends_on = [
data.aws_route_table.accepter,
data.aws_route_tables.accepter,
aws_vpc_peering_connection_accepter.accepter,
aws_vpc_peering_connection.requester
aws_vpc_peering_connection.requester,
]

timeouts {
Expand Down Expand Up @@ -124,3 +143,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"
}
6 changes: 4 additions & 2 deletions docs/terraform.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@
| [aws_caller_identity.requester](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
| [aws_region.accepter](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
| [aws_region.requester](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
| [aws_route_table.accepter](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route_table) | data source |
| [aws_route_table.requester](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route_table) | data source |
| [aws_subnet_ids.accepter](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnet_ids) | 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 |
| [aws_vpc.requester](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 -->

0 comments on commit 33a24a3

Please sign in to comment.