-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.tf
190 lines (172 loc) · 8.31 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
## Managed By : CloudDrove
## Description : This Script is used to manage a VPC peering connection.
## Copyright @ CloudDrove. All Right Reserved.
data "aws_region" "default" {}
data "aws_caller_identity" "current" {}
##-----------------------------------------------------------------------------
## Locals declration to determine count of accept_region.
##-----------------------------------------------------------------------------
locals {
accept_region = var.auto_accept == false ? var.accept_region : data.aws_region.default.id
}
##-----------------------------------------------------------------------------
## Labels module called that will be used for naming and tags.
##-----------------------------------------------------------------------------
module "labels" {
source = "clouddrove/labels/aws"
version = "1.3.0"
name = var.name
environment = var.environment
attributes = var.attributes
repository = var.repository
managedby = var.managedby
label_order = var.label_order
}
##-----------------------------------------------------------------------------
## Provides a resource to manage a VPC peering connection.
##-----------------------------------------------------------------------------
resource "aws_vpc_peering_connection" "default" {
count = var.enable_peering && var.auto_accept ? 1 : 0
vpc_id = var.requestor_vpc_id
peer_vpc_id = var.acceptor_vpc_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 = merge(
module.labels.tags,
{
"Name" = format("%s-peering", module.labels.id)
}
)
}
##-----------------------------------------------------------------------------
##provides details about a requestor VPC.
##-----------------------------------------------------------------------------
data "aws_vpc" "requestor" {
count = var.enable_peering ? 1 : 0
id = var.requestor_vpc_id
}
##-----------------------------------------------------------------------------
## provides details about a acceptor VPC.
##-----------------------------------------------------------------------------
data "aws_vpc" "acceptor" {
provider = aws.peer
count = var.enable_peering ? 1 : 0
id = var.acceptor_vpc_id
}
##-----------------------------------------------------------------------------
## This resource can be useful for getting back a list of acceptor route table ids to be referenced elsewhere.
##-----------------------------------------------------------------------------
data "aws_route_tables" "acceptor" {
provider = aws.peer
count = var.enable_peering ? 1 : 0
vpc_id = join("", data.aws_vpc.acceptor[*].id)
}
##-----------------------------------------------------------------------------
## This resource can be useful for getting back a list of requestor route table ids to be referenced elsewhere.
##-----------------------------------------------------------------------------
data "aws_route_tables" "requestor" {
count = var.enable_peering ? 1 : 0
vpc_id = join("", data.aws_vpc.requestor[*].id)
}
##-----------------------------------------------------------------------------
## Provides a resource to manage the accepter's side of a VPC Peering Connection.
##-----------------------------------------------------------------------------
resource "aws_vpc_peering_connection_accepter" "peer" {
count = var.enable_peering && var.auto_accept == false ? 1 : 0
provider = aws.peer
vpc_peering_connection_id = aws_vpc_peering_connection.region[0].id
auto_accept = true
tags = merge(
module.labels.tags,
{
"Name" = format("%s-peering", module.labels.environment)
}
)
}
##-----------------------------------------------------------------------------
## provides details about a requestor Route.
##-----------------------------------------------------------------------------
resource "aws_route" "requestor" {
count = var.enable_peering && var.auto_accept ? length(distinct(sort(data.aws_route_tables.requestor[0].ids))) * length(data.aws_vpc.acceptor[0].cidr_block_associations) : 0
route_table_id = element(distinct(sort(data.aws_route_tables.requestor[0].ids)), ceil(count.index / length(data.aws_vpc.acceptor[0].cidr_block_associations)))
destination_cidr_block = data.aws_vpc.acceptor[0].cidr_block_associations[count.index % length(data.aws_vpc.acceptor[0].cidr_block_associations)]["cidr_block"]
vpc_peering_connection_id = join("", aws_vpc_peering_connection.default[*].id)
depends_on = [data.aws_route_tables.requestor, aws_vpc_peering_connection.default]
}
##-----------------------------------------------------------------------------
## provides details about a requestor-region Route.
##-----------------------------------------------------------------------------
resource "aws_route" "requestor-region" {
count = var.enable_peering && var.auto_accept == false ? length(
distinct(sort(data.aws_route_tables.requestor[*].ids[0])),
) * length(data.aws_vpc.acceptor[0].cidr_block_associations) : 0
route_table_id = element(
distinct(sort(data.aws_route_tables.requestor[*].ids[0])),
ceil(
count.index / length(data.aws_vpc.acceptor[0].cidr_block_associations),
),
)
destination_cidr_block = data.aws_vpc.acceptor[0].cidr_block_associations[count.index % length(data.aws_vpc.acceptor[0].cidr_block_associations)]["cidr_block"]
vpc_peering_connection_id = aws_vpc_peering_connection.region[0].id
depends_on = [
data.aws_route_tables.requestor,
aws_vpc_peering_connection.region,
]
}
##-----------------------------------------------------------------------------
## provides details about a acceptor Route.
##-----------------------------------------------------------------------------
resource "aws_route" "acceptor" {
count = var.enable_peering && var.auto_accept ? length(distinct(sort(data.aws_route_tables.acceptor[0].ids))) * length(data.aws_vpc.requestor[0].cidr_block_associations) : 0
route_table_id = element(distinct(sort(data.aws_route_tables.acceptor[0].ids)), ceil(count.index / length(data.aws_vpc.requestor[0].cidr_block_associations)))
destination_cidr_block = data.aws_vpc.requestor[0].cidr_block_associations[count.index % length(data.aws_vpc.requestor[0].cidr_block_associations)]["cidr_block"]
vpc_peering_connection_id = join("", aws_vpc_peering_connection.default[*].id)
depends_on = [data.aws_route_tables.acceptor, aws_vpc_peering_connection.default]
}
##-----------------------------------------------------------------------------
## provides details about a acceptor-region Route.
##-----------------------------------------------------------------------------
resource "aws_route" "acceptor-region" {
count = var.enable_peering && var.auto_accept == false ? length(
distinct(sort(data.aws_route_tables.acceptor[*].ids[0])),
) * length(data.aws_vpc.requestor[0].cidr_block_associations) : 0
route_table_id = element(
distinct(sort(data.aws_route_tables.acceptor[*].ids[0])),
ceil(
count.index / length(data.aws_vpc.requestor[0].cidr_block_associations),
),
)
provider = aws.peer
destination_cidr_block = data.aws_vpc.requestor[0].cidr_block_associations[count.index % length(data.aws_vpc.requestor[0].cidr_block_associations)]["cidr_block"]
vpc_peering_connection_id = aws_vpc_peering_connection.region[0].id
depends_on = [
data.aws_route_tables.acceptor,
aws_vpc_peering_connection.region,
]
}
provider "aws" {
alias = "peer"
region = local.accept_region
}
##-----------------------------------------------------------------------------
## Provides a resource to manage a VPC peering connection.
##-----------------------------------------------------------------------------
resource "aws_vpc_peering_connection" "region" {
count = var.enable_peering && var.auto_accept == false ? 1 : 0
vpc_id = var.requestor_vpc_id
peer_vpc_id = var.acceptor_vpc_id
auto_accept = var.auto_accept
peer_region = local.accept_region
peer_owner_id = data.aws_caller_identity.current.account_id
tags = merge(
module.labels.tags,
{
"Name" = format("%s-peering", module.labels.environment)
}
)
}