generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.tf
445 lines (369 loc) · 11.8 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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# Get AZs for account
data "aws_availability_zones" "available" {
state = "available"
}
data "aws_region" "current" {}
locals {
availability_zones = sort(data.aws_availability_zones.available.names)
# Protected subnets
# get protected subnet cidr from spare /23 in first defined subnet-set for the vpc
protected_cidr = {
for index, item in var.subnet_sets :
index => cidrsubnet(item, 2, 3)
if index == "general"
}
expanded_protected_subnets = [
for index, cidr in cidrsubnets(local.protected_cidr["general"], 2, 2, 2) : {
key = "protected"
cidr = cidr
az = local.availability_zones[index]
type = "protected"
}
]
expanded_protected_subnets_with_keys = {
for subnet in local.expanded_protected_subnets :
"${subnet.key}-${subnet.az}" => subnet
}
# Transit Gateway subnets
transit_gateway_cidr = cidrsubnet(local.protected_cidr["general"], 2, 3)
expanded_tgw_subnets = [
for index, cidr in cidrsubnets(local.transit_gateway_cidr, 3, 3, 3) : {
key = "transit-gateway"
cidr = cidr
az = local.availability_zones[index]
type = "transit-gateway"
}
]
expanded_tgw_subnets_with_keys = {
for subnet in local.expanded_tgw_subnets :
"${subnet.key}-${subnet.az}" => subnet
}
# Worker subnets
expanded_worker_subnets = {
for key, subnet_set in var.subnet_sets :
key => chunklist(cidrsubnets(subnet_set, 3, 3, 3, 4, 4, 4, 4, 4, 4), 3)
}
expanded_worker_subnets_assocation = flatten([
for key, subnet_set in local.expanded_worker_subnets : [
for set_index, set in subnet_set : [
for cidr_index, cidr in set : {
key = key
cidr = cidr
az = local.availability_zones[cidr_index]
type = set_index == 0 ? "private" : (set_index == 1 ? "public" : "data")
group = key
}
]
]
])
expanded_worker_subnets_with_keys = {
for subnet in local.expanded_worker_subnets_assocation :
"${subnet.key}-${subnet.type}-${subnet.az}" => subnet
}
# All subnets (TGW and worker subnets)
all_subnets_with_keys = merge(
local.expanded_tgw_subnets_with_keys,
local.expanded_worker_subnets_with_keys,
# local.expanded_protected_subnets_with_keys
)
all_distinct_route_tables_with_keys = {
for rt in local.all_distinct_route_tables :
rt => rt
}
# All distinct route tables
all_distinct_route_tables = distinct([
for subnet in local.all_subnets_with_keys :
"${subnet.key}-${subnet.type}"
])
# All distinct route table associations
all_distinct_route_table_associations = {
for key, subnet in local.all_subnets_with_keys :
key => "${subnet.key}-${subnet.type}"
}
# SSM Endpoints (Systems Session Manager)
ssm_endpoints = [
"com.amazonaws.${data.aws_region.current.name}.ec2",
"com.amazonaws.${data.aws_region.current.name}.ec2messages",
"com.amazonaws.${data.aws_region.current.name}.ssm",
"com.amazonaws.${data.aws_region.current.name}.ssmmessages",
]
# Merge SSM endpoints with VPC requested endpoints
merged_endpoint_list = concat(
local.ssm_endpoints,
var.additional_endpoints
)
# Custom VPC flow log statement
custom_flow_log_format = "$${version} $${account-id} $${interface-id} $${srcaddr} $${dstaddr} $${srcport} $${dstport} $${protocol} $${packets} $${bytes} $${start} $${end} $${action} $${log-status} $${vpc-id} $${subnet-id} $${instance-id} $${tcp-flags} $${type} $${pkt-srcaddr} $${pkt-dstaddr} $${region} $${az-id} $${sublocation-type} $${sublocation-id} $${pkt-src-aws-service} $${pkt-dst-aws-service} $${flow-direction} $${traffic-path}"
}
# VPC
resource "aws_vpc" "vpc" {
cidr_block = var.subnet_sets["general"]
enable_dns_support = true
enable_dns_hostnames = true
tags = merge(
var.tags_common,
{
Name = var.tags_prefix
},
)
}
# Bring management of the default security group in the member vpc under terraform
resource "aws_default_security_group" "default" {
vpc_id = aws_vpc.vpc.id
# Block all inbound and outbound access to through this default security group
ingress = []
egress = []
tags = merge(
var.tags_common,
{
Name = "${var.tags_prefix}-default"
}
)
# For reference, the following inline ingress and egress rules are the 'default' rules which we are effectively removing
# Uncomment these rules to restore an uncustomised, default security group back to what it was originally
# See https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/default-custom-security-groups.html#default-security-group for more info
# ingress = [
# {
# protocol = -1
# self = true
# from_port = 0
# to_port = 0
# }
# ]
# egress = [
# {
# from_port = 0
# to_port = 0
# protocol = "-1"
# cidr_blocks = ["0.0.0.0/0"]
# }
# ]
}
# VPC Flow Logs
# TF sec exclusions
# - Ignore warnings regarding log groups not encrypted using customer-managed KMS keys - following cost/benefit discussion and longer term plans for logging solution
#tfsec:ignore:AWS089
resource "random_id" "flow_logs" {
keepers = {
# Generate a new id each time we regenerate a vpc
vpc_id = aws_vpc.vpc.cidr_block
}
byte_length = 4
}
#tfsec:ignore:aws-cloudwatch-log-group-customer-key
resource "aws_cloudwatch_log_group" "default" {
#checkov:skip=CKV_AWS_158:"Temporarily skip KMS encryption check while logging solution is being updated"
name = "${var.tags_prefix}-vpc-flow-logs-${random_id.flow_logs.hex}"
retention_in_days = 731 # 0 = never expire
}
resource "aws_flow_log" "cloudwatch" {
iam_role_arn = var.vpc_flow_log_iam_role
log_destination = aws_cloudwatch_log_group.default.arn
log_destination_type = "cloud-watch-logs"
log_format = local.custom_flow_log_format
max_aggregation_interval = "60"
traffic_type = "ALL"
vpc_id = aws_vpc.vpc.id
tags = merge(
var.tags_common,
{
Name = "${var.tags_prefix}-vpc-flow-logs-${random_id.flow_logs.hex}"
}
)
}
resource "aws_flow_log" "s3" {
for_each = var.flow_log_s3_destination_arn != "" ? toset([var.flow_log_s3_destination_arn]) : toset([])
log_destination = each.key
log_destination_type = "s3"
log_format = local.custom_flow_log_format
max_aggregation_interval = "60"
traffic_type = "ALL"
vpc_id = aws_vpc.vpc.id
tags = merge(
var.tags_common,
{
Name = "${var.tags_prefix}-vpc-flow-logs-s3-${random_id.flow_logs.hex}"
}
)
}
resource "aws_vpc_ipv4_cidr_block_association" "subnet_sets" {
for_each = {
for k, v in tomap(var.subnet_sets) :
k => v
if k != "general"
}
vpc_id = aws_vpc.vpc.id
cidr_block = each.value
}
# VPC: Subnet per type, per availability zone
resource "aws_subnet" "subnets" {
depends_on = [aws_vpc_ipv4_cidr_block_association.subnet_sets]
for_each = tomap(local.all_subnets_with_keys)
vpc_id = aws_vpc.vpc.id
cidr_block = each.value.cidr
availability_zone = each.value.az
tags = merge(
var.tags_common,
{
Name = "${var.tags_prefix}-${each.key}"
}
)
}
# Protected Subnets
resource "aws_subnet" "protected" {
for_each = tomap(local.expanded_protected_subnets_with_keys)
vpc_id = aws_vpc.vpc.id
cidr_block = each.value.cidr
availability_zone = each.value.az
tags = merge(
var.tags_common,
{
Name = "${var.tags_prefix}-${each.key}"
}
)
}
# VPC: Internet Gateway
resource "aws_internet_gateway" "default" {
vpc_id = aws_vpc.vpc.id
tags = merge(
var.tags_common,
{
Name = "${var.tags_prefix}-internet-gateway"
},
)
}
resource "aws_route_table" "route_tables" {
for_each = tomap(local.all_distinct_route_tables_with_keys)
vpc_id = aws_vpc.vpc.id
tags = merge(
var.tags_common,
{
Name = "${var.tags_prefix}-${each.value}"
}
)
}
resource "aws_route_table_association" "route_table_associations" {
for_each = tomap(local.all_distinct_route_table_associations)
subnet_id = aws_subnet.subnets[each.key].id
route_table_id = aws_route_table.route_tables[each.value].id
}
resource "aws_route" "public_internet_gateway" {
for_each = {
for key, route_table in aws_route_table.route_tables :
key => route_table
if substr(key, length(key) - 6, length(key)) == "public"
}
route_table_id = aws_route_table.route_tables[each.key].id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.default.id
}
resource "aws_route" "transit_gateway" {
for_each = {
for key, route_table in aws_route_table.route_tables :
key => route_table
if substr(key, length(key) - 6, length(key)) != "public"
}
route_table_id = aws_route_table.route_tables[each.key].id
destination_cidr_block = "0.0.0.0/0"
transit_gateway_id = var.transit_gateway_id
}
resource "aws_route_table" "protected" {
vpc_id = aws_vpc.vpc.id
tags = merge(
var.tags_common,
{
Name = "${var.tags_prefix}-protected"
}
)
}
resource "aws_route_table_association" "protected" {
for_each = aws_subnet.protected
subnet_id = each.value.id
route_table_id = aws_route_table.protected.id
}
# SSM Security Groups
resource "aws_security_group" "endpoints" {
name = "${var.tags_prefix}-int-endpoint"
description = "Control interface traffic"
vpc_id = aws_vpc.vpc.id
tags = merge(
var.tags_common,
{
Name = "${var.tags_prefix}-int-endpoint"
}
)
}
resource "aws_security_group_rule" "endpoints_ingress_1" {
for_each = var.subnet_sets
description = "Allow inbound HTTPS"
type = "ingress"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = [each.value]
security_group_id = aws_security_group.endpoints.id
}
resource "aws_security_group_rule" "endpoints_ingress_2" {
for_each = var.subnet_sets
description = "Allow inbound SMTP"
type = "ingress"
from_port = 25
to_port = 25
protocol = "tcp"
cidr_blocks = [each.value]
security_group_id = aws_security_group.endpoints.id
}
resource "aws_security_group_rule" "endpoints_ingress_3" {
for_each = var.subnet_sets
description = "Allow inbound SMTP-TLS"
type = "ingress"
from_port = 587
to_port = 587
protocol = "tcp"
cidr_blocks = [each.value]
security_group_id = aws_security_group.endpoints.id
}
resource "aws_security_group_rule" "endpoints_ingress_4" {
for_each = var.subnet_sets
description = "Allow inbound Redshift"
type = "ingress"
from_port = 5439
to_port = 5439
protocol = "tcp"
cidr_blocks = [each.value]
security_group_id = aws_security_group.endpoints.id
}
# SSM Endpoints
resource "aws_vpc_endpoint" "ssm_interfaces" {
for_each = toset(local.merged_endpoint_list)
vpc_id = aws_vpc.vpc.id
service_name = each.value
vpc_endpoint_type = "Interface"
subnet_ids = [
for az in local.availability_zones :
aws_subnet.protected["protected-${az}"].id
]
security_group_ids = [aws_security_group.endpoints.id]
private_dns_enabled = true
tags = merge(
var.tags_common,
{
Name = "${var.tags_prefix}-${each.key}"
}
)
}
resource "aws_vpc_endpoint" "ssm_s3" {
vpc_id = aws_vpc.vpc.id
service_name = "com.amazonaws.${data.aws_region.current.name}.s3"
vpc_endpoint_type = "Gateway"
route_table_ids = [
for value in local.all_distinct_route_table_associations :
aws_route_table.route_tables[value].id
]
tags = merge(
var.tags_common,
{
Name = "${var.tags_prefix}-com.amazonaws.${data.aws_region.current.name}.s3"
}
)
}