Skip to content

Commit d3b1bdc

Browse files
authored
Feat: add routes to default route table of vpc. Fix: update deprecated data source aws_subnets. (#35)
1 parent bfb36c2 commit d3b1bdc

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

main.tf

+21-16
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ module "labels" {
2222
provider "aws" {
2323
alias = "accepter"
2424
region = var.accepter_region
25-
version = ">= 3.1.15"
2625
profile = var.profile_name
2726

2827
assume_role {
@@ -31,11 +30,11 @@ provider "aws" {
3130
}
3231

3332
data "aws_caller_identity" "peer" {
34-
provider = "aws.accepter"
33+
provider = aws.accepter
3534
}
3635

3736
data "aws_region" "peer" {
38-
provider = "aws.accepter"
37+
provider = aws.accepter
3938
}
4039

4140

@@ -60,7 +59,7 @@ resource "aws_vpc_peering_connection" "default" {
6059
#Description : Provides a resource to manage the accepter's side of a VPC Peering Connection.
6160
resource "aws_vpc_peering_connection_accepter" "peer" {
6261
count = var.enable_peering == true ? 1 : 0
63-
provider = "aws.accepter"
62+
provider = aws.accepter
6463
vpc_peering_connection_id = aws_vpc_peering_connection.default[0].id
6564
auto_accept = true
6665
tags = module.labels.tags
@@ -89,33 +88,39 @@ data "aws_route_table" "requestor" {
8988
data "aws_subnets" "requestor" {
9089
count = var.enable_peering == true ? 1 : 0
9190

91+
filter {
92+
name = "vpc-id"
93+
values = [data.aws_vpc.requestor[0].id]
94+
}
9295
}
9396

9497
#Module : VPC ACCEPTOR
9598
#Description : Lookup acceptor VPC so that we can reference the CIDR.
9699
data "aws_vpc" "acceptor" {
97-
provider = "aws.accepter"
100+
provider = aws.accepter
98101
count = var.enable_peering == true ? 1 : 0
99102
id = var.acceptor_vpc_id
100103
}
101104

102105
#Module : SUBNET ID's ACCEPTOR
103106
#Description : Lookup acceptor subnets.
104107
data "aws_subnets" "acceptor" {
105-
provider = "aws.accepter"
108+
provider = aws.accepter
106109
count = var.enable_peering == true ? 1 : 0
110+
111+
filter {
112+
name = "vpc-id"
113+
values = [data.aws_vpc.acceptor[0].id]
114+
}
107115
}
108116

109117
#Module : ROUTE TABLE
110118
#Description : Lookup acceptor route tables.
111-
data "aws_route_table" "acceptor" {
112-
provider = "aws.accepter"
119+
data "aws_route_tables" "acceptor" {
120+
provider = aws.accepter
113121
count = var.enable_peering == true ? length(distinct(sort(data.aws_subnets.acceptor[0].ids))) : 0
114122

115-
subnet_id = element(
116-
distinct(sort(data.aws_subnets.acceptor[0].ids)),
117-
count.index
118-
)
123+
vpc_id = data.aws_vpc.acceptor[0].id
119124
}
120125

121126
#Module : ROUTE REQUESTOR
@@ -144,14 +149,14 @@ resource "aws_route" "requestor" {
144149
#Module : ROUTE ACCEPTOR
145150
#Description : Create routes from acceptor to requestor.
146151
resource "aws_route" "acceptor" {
147-
provider = "aws.accepter"
152+
provider = aws.accepter
148153

149154
count = var.enable_peering == true ? length(
150-
distinct(sort(data.aws_route_table.acceptor.*.route_table_id))
155+
distinct(sort(data.aws_route_tables.acceptor[0].ids))
151156
) * length(data.aws_vpc.requestor[0].cidr_block_associations) : 0
152157

153158
route_table_id = element(
154-
distinct(sort(data.aws_route_table.acceptor.*.route_table_id)),
159+
distinct(sort(data.aws_route_tables.acceptor[0].ids)),
155160
ceil(
156161
count.index / length(data.aws_vpc.requestor[0].cidr_block_associations)
157162
)
@@ -160,7 +165,7 @@ resource "aws_route" "acceptor" {
160165
destination_cidr_block = data.aws_vpc.requestor.0.cidr_block_associations[count.index % length(data.aws_vpc.requestor[0].cidr_block_associations)]["cidr_block"]
161166
vpc_peering_connection_id = aws_vpc_peering_connection.default[0].id
162167
depends_on = [
163-
data.aws_route_table.acceptor,
168+
data.aws_route_tables.acceptor,
164169
aws_vpc_peering_connection.default,
165170
]
166171
}

0 commit comments

Comments
 (0)