diff --git a/main.tf b/main.tf index fa92f3d..af6fd85 100644 --- a/main.tf +++ b/main.tf @@ -33,17 +33,18 @@ data "aws_vpc" "requestor" { id = "${var.requestor_vpc_id}" } -# Lookup requestor subnets -data "aws_subnet_ids" "requestor" { - vpc_id = "${var.requestor_vpc_id}" -} - # Lookup requestor route tables data "aws_route_table" "requestor" { count = "${var.enabled == "true" ? length(distinct(sort(data.aws_subnet_ids.requestor.ids))) : 0}" subnet_id = "${element(distinct(sort(data.aws_subnet_ids.requestor.ids)), count.index)}" } +# Lookup requestor subnets +data "aws_subnet_ids" "requestor" { + count = "${var.enabled == "true" ? 1 : 0}" + vpc_id = "${data.aws_vpc.requestor.id}" +} + # Lookup acceptor VPC so that we can reference the CIDR data "aws_vpc" "acceptor" { count = "${var.enabled == "true" ? 1 : 0}" @@ -52,7 +53,8 @@ data "aws_vpc" "acceptor" { # Lookup acceptor subnets data "aws_subnet_ids" "acceptor" { - vpc_id = "${var.acceptor_vpc_id}" + count = "${var.enabled == "true" ? 1 : 0}" + vpc_id = "${data.aws_vpc.acceptor.id}" } # Lookup acceptor route tables @@ -61,18 +63,20 @@ data "aws_route_table" "acceptor" { subnet_id = "${element(distinct(sort(data.aws_subnet_ids.acceptor.ids)), count.index)}" } -# Create a route from requestor to acceptor +# Create routes from requestor to acceptor resource "aws_route" "requestor" { count = "${var.enabled == "true" ? length(distinct(sort(data.aws_route_table.requestor.*.route_table_id))) : 0}" route_table_id = "${element(distinct(sort(data.aws_route_table.requestor.*.route_table_id)), count.index)}" destination_cidr_block = "${data.aws_vpc.acceptor.cidr_block}" vpc_peering_connection_id = "${aws_vpc_peering_connection.default.id}" + depends_on = ["data.aws_route_table.requestor", "aws_vpc_peering_connection.default"] } -# Create a route from acceptor to requestor +# Create routes from acceptor to requestor resource "aws_route" "acceptor" { count = "${var.enabled == "true" ? length(distinct(sort(data.aws_route_table.acceptor.*.route_table_id))) : 0}" route_table_id = "${element(distinct(sort(data.aws_route_table.acceptor.*.route_table_id)), count.index)}" destination_cidr_block = "${data.aws_vpc.requestor.cidr_block}" vpc_peering_connection_id = "${aws_vpc_peering_connection.default.id}" + depends_on = ["data.aws_route_table.acceptor", "aws_vpc_peering_connection.default"] }