Skip to content

Commit 33a24a3

Browse files
BenbentwocloudpossebotaknyshNuru
authored
Re-support Default Routing Table #44 (#62)
* revert PR #44 * Auto Format * Apply suggestions from code review Co-authored-by: Andriy Knysh <aknysh@users.noreply.github.com> * 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 <aknysh@users.noreply.github.com> * Auto Format * PR Comments * Apply suggestions from code review Co-authored-by: Nuru <Nuru@users.noreply.github.com> Co-authored-by: cloudpossebot <11232728+cloudpossebot@users.noreply.github.com> Co-authored-by: Andriy Knysh <aknysh@users.noreply.github.com> Co-authored-by: Nuru <Nuru@users.noreply.github.com>
1 parent 67f1be2 commit 33a24a3

File tree

3 files changed

+46
-18
lines changed

3 files changed

+46
-18
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,11 @@ Available targets:
357357
| [aws_caller_identity.requester](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
358358
| [aws_region.accepter](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
359359
| [aws_region.requester](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
360-
| [aws_route_table.accepter](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route_table) | data source |
361360
| [aws_route_table.requester](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route_table) | data source |
362-
| [aws_subnet_ids.accepter](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnet_ids) | data source |
361+
| [aws_route_tables.accepter](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route_tables) | data source |
362+
| [aws_route_tables.default_rts](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route_tables) | data source |
363363
| [aws_subnet_ids.requester](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnet_ids) | data source |
364+
| [aws_subnets.accepter](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnets) | data source |
364365
| [aws_vpc.accepter](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/vpc) | data source |
365366
| [aws_vpc.requester](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/vpc) | data source |
366367

@@ -419,6 +420,7 @@ Available targets:
419420
|------|-------------|
420421
| <a name="output_accepter_accept_status"></a> [accepter\_accept\_status](#output\_accepter\_accept\_status) | Accepter VPC peering connection request status |
421422
| <a name="output_accepter_connection_id"></a> [accepter\_connection\_id](#output\_accepter\_connection\_id) | Accepter VPC peering connection ID |
423+
| <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 |
422424
| <a name="output_requester_accept_status"></a> [requester\_accept\_status](#output\_requester\_accept\_status) | Requester VPC peering connection request status |
423425
| <a name="output_requester_connection_id"></a> [requester\_connection\_id](#output\_requester\_connection\_id) | Requester VPC peering connection ID |
424426
<!-- markdownlint-restore -->

accepter.tf

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,46 +48,65 @@ data "aws_vpc" "accepter" {
4848
}
4949

5050
# Lookup accepter subnets
51-
data "aws_subnet_ids" "accepter" {
51+
data "aws_subnets" "accepter" {
5252
count = local.accepter_count
5353
provider = aws.accepter
54-
vpc_id = local.accepter_vpc_id
55-
tags = var.accepter_subnet_tags
54+
filter {
55+
name = "vpc-id"
56+
values = [local.accepter_vpc_id]
57+
}
58+
tags = var.accepter_subnet_tags
5659
}
5760

5861
locals {
59-
accepter_subnet_ids = try(distinct(sort(flatten(data.aws_subnet_ids.accepter.*.ids))), [])
62+
accepter_subnet_ids = local.accepter_enabled ? data.aws_subnets.accepter[0].ids : []
6063
accepter_subnet_ids_count = length(local.accepter_subnet_ids)
6164
accepter_vpc_id = join("", data.aws_vpc.accepter.*.id)
6265
accepter_account_id = join("", data.aws_caller_identity.accepter.*.account_id)
6366
accepter_region = join("", data.aws_region.accepter.*.name)
6467
}
6568

66-
# Lookup accepter route tables
67-
data "aws_route_table" "accepter" {
68-
count = local.accepter_enabled ? local.accepter_subnet_ids_count : 0
69-
provider = aws.accepter
70-
subnet_id = element(local.accepter_subnet_ids, count.index)
69+
data "aws_route_tables" "accepter" {
70+
for_each = toset(local.accepter_subnet_ids)
71+
provider = aws.accepter
72+
vpc_id = local.accepter_vpc_id
73+
filter {
74+
name = "association.subnet-id"
75+
values = [each.key]
76+
}
77+
}
78+
79+
# If we had more subnets than routetables, we should update the default.
80+
data "aws_route_tables" "default_rts" {
81+
count = local.count
82+
provider = aws.accepter
83+
vpc_id = local.accepter_vpc_id
84+
filter {
85+
name = "association.main"
86+
values = ["true"]
87+
}
7188
}
7289

7390
locals {
74-
accepter_aws_route_table_ids = try(distinct(sort(data.aws_route_table.accepter.*.route_table_id)), [])
91+
accepter_aws_default_rt_id = join("", flatten(data.aws_route_tables.default_rts.*.ids))
92+
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) }
93+
accepter_aws_route_table_ids = distinct(sort(values(local.accepter_aws_rt_map)))
7594
accepter_aws_route_table_ids_count = length(local.accepter_aws_route_table_ids)
76-
accepter_cidr_block_associations = try(flatten(data.aws_vpc.accepter.*.cidr_block_associations), [])
95+
accepter_cidr_block_associations = flatten(data.aws_vpc.accepter.*.cidr_block_associations)
7796
accepter_cidr_block_associations_count = length(local.accepter_cidr_block_associations)
7897
}
7998

8099
# Create routes from accepter to requester
81100
resource "aws_route" "accepter" {
82-
count = local.accepter_enabled ? local.accepter_aws_route_table_ids_count * local.requester_cidr_block_associations_count : 0
101+
count = local.enabled ? local.accepter_aws_route_table_ids_count * local.requester_cidr_block_associations_count : 0
83102
provider = aws.accepter
84103
route_table_id = local.accepter_aws_route_table_ids[floor(count.index / local.requester_cidr_block_associations_count)]
85104
destination_cidr_block = local.requester_cidr_block_associations[count.index % local.requester_cidr_block_associations_count]["cidr_block"]
86105
vpc_peering_connection_id = join("", aws_vpc_peering_connection.requester.*.id)
87106
depends_on = [
88-
data.aws_route_table.accepter,
107+
data.aws_route_tables.accepter,
89108
aws_vpc_peering_connection_accepter.accepter,
90-
aws_vpc_peering_connection.requester
109+
aws_vpc_peering_connection.requester,
91110
]
92111

93112
timeouts {
@@ -124,3 +143,8 @@ output "accepter_accept_status" {
124143
value = join("", aws_vpc_peering_connection_accepter.accepter.*.accept_status)
125144
description = "Accepter VPC peering connection request status"
126145
}
146+
147+
output "accepter_subnet_route_table_map" {
148+
value = local.accepter_aws_rt_map
149+
description = "Map of accepter VPC subnet IDs to route table IDs"
150+
}

docs/terraform.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@
3535
| [aws_caller_identity.requester](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
3636
| [aws_region.accepter](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
3737
| [aws_region.requester](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
38-
| [aws_route_table.accepter](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route_table) | data source |
3938
| [aws_route_table.requester](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route_table) | data source |
40-
| [aws_subnet_ids.accepter](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnet_ids) | data source |
39+
| [aws_route_tables.accepter](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route_tables) | data source |
40+
| [aws_route_tables.default_rts](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route_tables) | data source |
4141
| [aws_subnet_ids.requester](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnet_ids) | data source |
42+
| [aws_subnets.accepter](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnets) | data source |
4243
| [aws_vpc.accepter](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/vpc) | data source |
4344
| [aws_vpc.requester](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/vpc) | data source |
4445

@@ -97,6 +98,7 @@
9798
|------|-------------|
9899
| <a name="output_accepter_accept_status"></a> [accepter\_accept\_status](#output\_accepter\_accept\_status) | Accepter VPC peering connection request status |
99100
| <a name="output_accepter_connection_id"></a> [accepter\_connection\_id](#output\_accepter\_connection\_id) | Accepter VPC peering connection ID |
101+
| <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 |
100102
| <a name="output_requester_accept_status"></a> [requester\_accept\_status](#output\_requester\_accept\_status) | Requester VPC peering connection request status |
101103
| <a name="output_requester_connection_id"></a> [requester\_connection\_id](#output\_requester\_connection\_id) | Requester VPC peering connection ID |
102104
<!-- markdownlint-restore -->

0 commit comments

Comments
 (0)