forked from cloudposse/terraform-aws-vpc-peering
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
84 lines (71 loc) · 3.62 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
module "label" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.3.3"
enabled = "${var.enabled}"
namespace = "${var.namespace}"
name = "${var.name}"
stage = "${var.stage}"
delimiter = "${var.delimiter}"
attributes = "${var.attributes}"
tags = "${var.tags}"
}
resource "aws_vpc_peering_connection" "default" {
count = "${var.enabled == "true" ? 1 : 0}"
vpc_id = "${join("", data.aws_vpc.requestor.*.id)}"
peer_vpc_id = "${join("", data.aws_vpc.acceptor.*.id)}"
auto_accept = "${var.auto_accept}"
accepter {
allow_remote_vpc_dns_resolution = "${var.acceptor_allow_remote_vpc_dns_resolution}"
}
requester {
allow_remote_vpc_dns_resolution = "${var.requestor_allow_remote_vpc_dns_resolution}"
}
tags = "${module.label.tags}"
}
# Lookup requestor VPC so that we can reference the CIDR
data "aws_vpc" "requestor" {
count = "${var.enabled == "true" ? 1 : 0}"
id = "${var.requestor_vpc_id}"
tags = "${var.requestor_vpc_tags}"
}
# 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[0].id}"
}
# Lookup acceptor VPC so that we can reference the CIDR
data "aws_vpc" "acceptor" {
count = "${var.enabled == "true" ? 1 : 0}"
id = "${var.acceptor_vpc_id}"
tags = "${var.acceptor_vpc_tags}"
}
# Lookup acceptor subnets
data "aws_subnet_ids" "acceptor" {
count = "${var.enabled == "true" ? 1 : 0}"
vpc_id = "${data.aws_vpc.acceptor[0].id}"
}
# Lookup acceptor route tables
data "aws_route_table" "acceptor" {
count = "${var.enabled == "true" ? length(distinct(sort(data.aws_subnet_ids.acceptor.ids))) : 0}"
subnet_id = "${element(distinct(sort(data.aws_subnet_ids.acceptor.ids)), count.index)}"
}
# 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))) * length(data.aws_vpc.acceptor.cidr_block_associations) : 0}"
route_table_id = "${element(distinct(sort(data.aws_route_table.requestor.*.route_table_id)), (ceil(count.index / (length(data.aws_vpc.acceptor.cidr_block_associations)))))}"
destination_cidr_block = "${lookup(data.aws_vpc.acceptor.cidr_block_associations[count.index % (length(data.aws_vpc.acceptor.cidr_block_associations))], "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 routes from acceptor to requestor
resource "aws_route" "acceptor" {
count = "${var.enabled == "true" ? length(distinct(sort(data.aws_route_table.acceptor.*.route_table_id))) * length(data.aws_vpc.requestor.cidr_block_associations) : 0}"
route_table_id = "${element(distinct(sort(data.aws_route_table.acceptor.*.route_table_id)), ceil(count.index / (length(data.aws_vpc.requestor.cidr_block_associations))))}"
destination_cidr_block = "${lookup(data.aws_vpc.requestor.cidr_block_associations[count.index % (length(data.aws_vpc.requestor.cidr_block_associations))], "cidr_block")}"
vpc_peering_connection_id = "${aws_vpc_peering_connection.default.id}"
depends_on = ["data.aws_route_table.acceptor", "aws_vpc_peering_connection.default"]
}