-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathvpc.tf
318 lines (213 loc) · 7.54 KB
/
vpc.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# VPC
resource "aws_vpc" "vault" {
cidr_block = var.vpc_cidr
instance_tenancy = var.vpc_instance_tenancy
enable_dns_support = true
enable_dns_hostnames = true # required for VPC peering.
assign_generated_ipv6_cidr_block = true
tags = merge(
{ "Name" = "${var.main_project_tag}-vpc" },
{ "Project" = var.main_project_tag },
var.vpc_tags
)
}
# Gateways
## Internet Gateway
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.vault.id
tags = merge(
{ "Name" = "${var.main_project_tag}-igw"},
{ "Project" = var.main_project_tag },
var.vpc_tags
)
}
## Egress Only Gateway (IPv6)
resource "aws_egress_only_internet_gateway" "eigw" {
vpc_id = aws_vpc.vault.id
}
## NAT Gateway
#### The NAT Elastic IP
resource "aws_eip" "nat" {
count = var.operator_mode ? 1 : 0
vpc = true
tags = merge(
{ "Name" = "${var.main_project_tag}-nat-eip"},
{ "Project" = var.main_project_tag },
var.vpc_tags
)
depends_on = [aws_internet_gateway.igw]
}
#### The NAT Gateway
resource "aws_nat_gateway" "nat" {
count = var.operator_mode ? 1 : 0
allocation_id = aws_eip.nat[0].id // same as aws_eip.nat.0.id
subnet_id = aws_subnet.public.0.id
tags = merge(
{ "Name" = "${var.main_project_tag}-nat"},
{ "Project" = var.main_project_tag },
var.vpc_tags
)
depends_on = [
aws_internet_gateway.igw,
aws_eip.nat
]
}
# Route Tables
// NOTE: Routing to the VPC's CIDR is allowed by default, so no route is needed
## Public Route Table
resource "aws_route_table" "public" {
vpc_id = aws_vpc.vault.id
tags = merge(
{ "Name" = "${var.main_project_tag}-public-rtb"},
{ "Project" = var.main_project_tag },
var.vpc_tags
)
}
#### Public routes
resource "aws_route" "public_internet_access" {
route_table_id = aws_route_table.public.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
## Private Route Table
resource "aws_route_table" "private" {
vpc_id = aws_vpc.vault.id
tags = merge(
{ "Name" = "${var.main_project_tag}-private-rtb"},
{ "Project" = var.main_project_tag },
var.vpc_tags
)
}
#### Private Routes
resource "aws_route" "private_internet_access" {
count = var.operator_mode ? 1 : 0
route_table_id = aws_route_table.private.id
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.nat[0].id
}
resource "aws_route" "private_internet_access_ipv6" {
count = var.operator_mode ? 1 : 0
route_table_id = aws_route_table.private.id
destination_ipv6_cidr_block = "::/0"
egress_only_gateway_id = aws_egress_only_internet_gateway.eigw.id
}
# Subnets
## Public Subnets
resource "aws_subnet" "public" {
count = var.vpc_public_subnet_count
vpc_id = aws_vpc.vault.id
cidr_block = cidrsubnet(aws_vpc.vault.cidr_block, 4, count.index)
availability_zone = data.aws_availability_zones.available.names[count.index]
map_public_ip_on_launch = true
ipv6_cidr_block = cidrsubnet(aws_vpc.vault.ipv6_cidr_block, 8, count.index)
assign_ipv6_address_on_creation = true
tags = merge(
{ "Name" = "${var.main_project_tag}-public-${data.aws_availability_zones.available.names[count.index]}"},
{ "Project" = var.main_project_tag },
var.vpc_tags
)
}
## Private Subnets
resource "aws_subnet" "private" {
count = var.vpc_private_subnet_count
vpc_id = aws_vpc.vault.id
// Increment the netnum by the number of public subnets to avoid overlap
cidr_block = cidrsubnet(aws_vpc.vault.cidr_block, 4, count.index + var.vpc_public_subnet_count)
availability_zone = data.aws_availability_zones.available.names[count.index]
tags = merge(
{ "Name" = "${var.main_project_tag}-private-${data.aws_availability_zones.available.names[count.index]}"},
{ "Project" = var.main_project_tag },
var.vpc_tags
)
}
# Route Table Associations
## Public Subnet Route Associations
resource "aws_route_table_association" "public" {
count = var.vpc_public_subnet_count
subnet_id = element(aws_subnet.public.*.id, count.index)
route_table_id = aws_route_table.public.id
}
## Private Subnet Route Associations
resource "aws_route_table_association" "private" {
count = var.vpc_private_subnet_count
subnet_id = element(aws_subnet.private.*.id, count.index)
route_table_id = aws_route_table.private.id
}
# VPC Endpoints
// Make safe calls to KMS and DynamoDB without leaving the VPC. Because #awsthings. C'mon. This should be default without these things.
## KMS Endpoint
data "aws_vpc_endpoint_service" "kms" {
service = "kms"
}
#### To get the required data, if you're confused, just output the above KMS data source. It has all the details.
resource "aws_vpc_endpoint" "kms" {
service_name = data.aws_vpc_endpoint_service.kms.service_name
vpc_id = aws_vpc.vault.id
private_dns_enabled = true
// Can also be done with "aws_vpc_endpoint_subnet_association"
subnet_ids = aws_subnet.private.*.id
security_group_ids = [aws_security_group.kms_endpoint.id]
tags = merge(
{ "Name" = "${var.main_project_tag}-kms-endpoint"},
{ "Project" = var.main_project_tag },
var.vpc_tags
)
vpc_endpoint_type = "Interface" // KMS is indeed an interface type
}
## DynamoDB Endpoint
data "aws_vpc_endpoint_service" "dynamodb" {
service = "dynamodb"
}
resource "aws_vpc_endpoint" "dynamodb" {
service_name = data.aws_vpc_endpoint_service.dynamodb.service_name
vpc_id = aws_vpc.vault.id
// Can also be done with "aws_vpc_endpoint_route_table_association"
route_table_ids = [aws_route_table.private.id]
tags = merge(
{ "Name" = "${var.main_project_tag}-dynamodb-endpoint"},
{ "Project" = var.main_project_tag },
var.vpc_tags
)
vpc_endpoint_type = "Gateway"
}
# VPC Peering
## Enabled in Private Mode only. Allows other VPCs in the same account and region to access your Vault VPC.
## Data from Peered VPC (AKA the external VPC we're letting in)
data "aws_vpc" "peered_vpc" {
count = var.private_mode && length(var.peered_vpc_ids) > 0 ? length(var.peered_vpc_ids) : 0
id = var.peered_vpc_ids[count.index]
}
## Peering Connections
resource "aws_vpc_peering_connection" "vault" {
count = var.private_mode && length(var.peered_vpc_ids) > 0 ? length(var.peered_vpc_ids) : 0
peer_vpc_id = var.peered_vpc_ids[count.index]
vpc_id = aws_vpc.vault.id
auto_accept = true
accepter {
allow_remote_vpc_dns_resolution = true
}
requester {
allow_remote_vpc_dns_resolution = true
}
tags = merge(
{ "Name" = "${var.main_project_tag}-vpc-peering-connection-${count.index + 1}"},
{ "Project" = var.main_project_tag },
var.vpc_tags
)
}
## Peering Connection Routes for the VAULT Route Table
resource "aws_route" "requester_peering_route" {
count = var.private_mode && length(var.peered_vpc_ids) > 0 ? length(var.peered_vpc_ids) : 0
route_table_id = aws_route_table.public.id
destination_cidr_block = data.aws_vpc.peered_vpc[count.index].cidr_block
vpc_peering_connection_id = aws_vpc_peering_connection.vault[count.index].id
}
## Peering Connection Routes for the External VPC Route Tables to allow Vault Traffic
## Note: this associates it to the external VPC's MAIN ROUTE TABLE.
## If you want it associated to a different route table, you'll have to do so manually or set the table you want as the main route table.
resource "aws_route" "accepter_peering_route" {
count = var.private_mode && length(var.peered_vpc_ids) > 0 ? length(var.peered_vpc_ids) : 0
route_table_id = data.aws_vpc.peered_vpc[count.index].main_route_table_id
destination_cidr_block = aws_vpc.vault.cidr_block
vpc_peering_connection_id = aws_vpc_peering_connection.vault[count.index].id
}