generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
614 lines (566 loc) · 17.2 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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
data "aws_vpc" "shared" {
tags = {
"Name" = var.vpc_all
}
}
# Terraform module which creates S3 Bucket resources for Load Balancer Access Logs on AWS.
module "s3-bucket" {
count = var.existing_bucket_name == "" && var.access_logs ? 1 : 0
source = "github.com/ministryofjustice/modernisation-platform-terraform-s3-bucket?ref=568694e50e03630d99cb569eafa06a0b879a1239" # v7.1.0
providers = {
aws.bucket-replication = aws.bucket-replication
}
bucket_prefix = "${var.application_name}-lb-access-logs"
bucket_policy = [data.aws_iam_policy_document.bucket_policy[0].json]
replication_enabled = false
versioning_enabled = var.s3_versioning
force_destroy = var.force_destroy_bucket
sse_algorithm = var.load_balancer_type == "network" ? "AES256" : "aws:kms"
lifecycle_rule = var.access_logs_lifecycle_rule
tags = merge(
var.tags,
{
backup = false
},
)
}
data "aws_iam_policy_document" "bucket_policy" {
count = var.access_logs ? 1 : 0
statement {
effect = "Allow"
actions = [
"s3:PutObject"
]
resources = flatten([var.existing_bucket_name != ""
? [
"arn:aws:s3:::${var.existing_bucket_name}/${var.application_name}/AWSLogs/${var.account_number}/*",
"arn:aws:s3:::${var.existing_bucket_name}/AWSLogs/${var.account_number}/*"
]
: [
"${module.s3-bucket[0].bucket.arn}/${var.application_name}/AWSLogs/${var.account_number}/*",
"${module.s3-bucket[0].bucket.arn}/AWSLogs/${var.account_number}/*"
]
])
principals {
type = "AWS"
identifiers = [data.aws_elb_service_account.default.arn]
}
}
statement {
effect = "Allow"
sid = "AWSLogDeliveryWrite"
principals {
type = "Service"
identifiers = ["delivery.logs.amazonaws.com"]
}
actions = [
"s3:PutObject"
]
resources = flatten([var.existing_bucket_name != ""
? [
"arn:aws:s3:::${var.existing_bucket_name}/${var.application_name}/AWSLogs/${var.account_number}/*",
"arn:aws:s3:::${var.existing_bucket_name}/AWSLogs/${var.account_number}/*"
]
: [
"${module.s3-bucket[0].bucket.arn}/${var.application_name}/AWSLogs/${var.account_number}/*",
"${module.s3-bucket[0].bucket.arn}/AWSLogs/${var.account_number}/*"
]
])
condition {
test = "StringEquals"
variable = "s3:x-amz-acl"
values = [
"bucket-owner-full-control"
]
}
}
statement {
sid = "AWSLogDeliveryAclCheck"
effect = "Allow"
principals {
type = "Service"
identifiers = ["delivery.logs.amazonaws.com"]
}
actions = [
"s3:GetBucketAcl"
]
resources = [
var.existing_bucket_name != "" ? "arn:aws:s3:::${var.existing_bucket_name}" : module.s3-bucket[0].bucket.arn
]
}
}
data "aws_elb_service_account" "default" {}
#tfsec:ignore:aws-elb-alb-not-public
resource "aws_lb" "loadbalancer" {
#checkov:skip=CKV_AWS_150:preventing destroy can be controlled outside of the module
#checkov:skip=CKV2_AWS_28:WAF is configured outside of the module for more flexibility
name = "${var.application_name}-lb"
internal = var.internal_lb
load_balancer_type = var.load_balancer_type
security_groups = length(aws_security_group.lb) > 0 ? [aws_security_group.lb[0].id] : var.security_groups
subnets = concat(var.subnets, var.public_subnets)
enable_deletion_protection = var.enable_deletion_protection
idle_timeout = var.idle_timeout
drop_invalid_header_fields = var.drop_invalid_header_fields
enable_cross_zone_load_balancing = var.enable_cross_zone_load_balancing
dns_record_client_routing_policy = var.dns_record_client_routing_policy
dynamic "access_logs" {
for_each = var.access_logs ? [1] : []
content {
bucket = var.existing_bucket_name != "" ? var.existing_bucket_name : module.s3-bucket[0].bucket.id
prefix = var.application_name
enabled = var.access_logs
}
}
tags = merge(
var.tags,
{
Name = "${var.application_name}-lb"
},
)
depends_on = [module.s3-bucket]
}
resource "aws_security_group" "lb" {
count = var.security_groups == null ? 1 : 0
name = "${var.application_name}-lb-security-group"
description = "Controls access to the loadbalancer"
vpc_id = data.aws_vpc.shared.id
dynamic "ingress" {
for_each = var.loadbalancer_ingress_rules
content {
description = lookup(ingress.value, "description", null)
from_port = lookup(ingress.value, "from_port", null)
to_port = lookup(ingress.value, "to_port", null)
protocol = lookup(ingress.value, "protocol", null)
cidr_blocks = lookup(ingress.value, "cidr_blocks", null)
security_groups = lookup(ingress.value, "security_groups", null)
}
}
dynamic "egress" {
for_each = var.loadbalancer_egress_rules
content {
description = lookup(egress.value, "description", null)
from_port = lookup(egress.value, "from_port", null)
to_port = lookup(egress.value, "to_port", null)
protocol = lookup(egress.value, "protocol", null)
cidr_blocks = lookup(egress.value, "cidr_blocks", null)
security_groups = lookup(egress.value, "security_groups", null)
}
}
tags = merge(
var.tags,
{
Name = "${var.application_name}-lb-security-group"
},
)
}
resource "aws_athena_database" "lb-access-logs" {
count = var.access_logs ? 1 : 0
name = replace("${var.application_name}-lb-access-logs", "-", "_") # dashes not allowed in name
bucket = var.existing_bucket_name != "" ? var.existing_bucket_name : module.s3-bucket[0].bucket.id
encryption_configuration {
encryption_option = "SSE_S3"
}
}
resource "aws_athena_workgroup" "lb-access-logs" {
count = var.access_logs ? 1 : 0
name = "${var.application_name}-lb-access-logs"
configuration {
enforce_workgroup_configuration = true
publish_cloudwatch_metrics_enabled = true
engine_version {
selected_engine_version = "Athena engine version 3"
}
result_configuration {
output_location = var.existing_bucket_name != "" ? "s3://${var.existing_bucket_name}/output/" : "s3://${module.s3-bucket[0].bucket.id}/output/"
encryption_configuration {
encryption_option = "SSE_S3"
}
}
}
tags = merge(
var.tags,
{
Name = "${var.application_name}-lb-access-logs"
},
)
}
resource "aws_lb_target_group" "this" {
for_each = var.lb_target_groups
name = each.key
port = each.value.port
protocol = "TCP"
target_type = "alb"
deregistration_delay = each.value.deregistration_delay
vpc_id = data.aws_vpc.shared.id
dynamic "health_check" {
for_each = each.value.health_check != null ? [each.value.health_check] : []
content {
enabled = health_check.value.enabled
interval = health_check.value.interval
healthy_threshold = health_check.value.healthy_threshold
matcher = health_check.value.matcher
path = health_check.value.path
port = health_check.value.port
timeout = health_check.value.timeout
unhealthy_threshold = health_check.value.unhealthy_threshold
}
}
dynamic "stickiness" {
for_each = each.value.stickiness != null ? [each.value.stickiness] : []
content {
enabled = stickiness.value.enabled
type = stickiness.value.type
cookie_duration = stickiness.value.cookie_duration
cookie_name = stickiness.value.cookie_name
}
}
tags = merge(
var.tags,
{
Name = each.key
},
)
}
resource "aws_lb_target_group_attachment" "this" {
for_each = var.lb_target_groups
target_group_arn = aws_lb_target_group.this[each.key].arn
target_id = aws_lb.loadbalancer.arn
port = coalesce(each.value.attachment_port, each.value.port)
}
# Glue Permissions
resource "aws_iam_role" "glue" {
count = var.access_logs ? 1 : 0
name = "glue-${var.application_name}"
assume_role_policy = data.aws_iam_policy_document.glue_assume[count.index].json
}
data "aws_iam_policy_document" "glue_assume" {
count = var.access_logs ? 1 : 0
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["glue.amazonaws.com"]
}
}
}
data "aws_iam_policy_document" "glue_s3" {
count = var.access_logs ? 1 : 0
statement {
effect = "Allow"
actions = [
"s3:GetObject",
"s3:PutObject"
]
resources = flatten([var.existing_bucket_name != ""
? [
"arn:aws:s3:::${var.existing_bucket_name}/${var.application_name}/AWSLogs/${var.account_number}/*",
"arn:aws:s3:::${var.existing_bucket_name}/AWSLogs/${var.account_number}/*"
]
: [
"${module.s3-bucket[0].bucket.arn}/${var.application_name}/AWSLogs/${var.account_number}/*",
"${module.s3-bucket[0].bucket.arn}/AWSLogs/${var.account_number}/*"
]
])
}
}
resource "aws_iam_policy" "glue_s3" {
count = var.access_logs && length(data.aws_iam_policy_document.glue_s3) > 0 ? 1 : 0
name = "glue-s3-${var.application_name}"
policy = data.aws_iam_policy_document.glue_s3[count.index].json
}
resource "aws_iam_role_policy_attachment" "glue_s3" {
count = var.access_logs && length(data.aws_iam_policy_document.glue_s3) > 0 ? 1 : 0
role = aws_iam_role.glue[count.index].name
policy_arn = aws_iam_policy.glue_s3[count.index].arn
}
resource "aws_iam_role_policy_attachment" "glue_service" {
count = var.access_logs ? 1 : 0
role = aws_iam_role.glue[count.index].id
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSGlueServiceRole"
}
# Catalog Tables
resource "aws_glue_catalog_table" "application_lb_logs" {
count = var.access_logs && var.load_balancer_type == "application" ? 1 : 0
name = "${var.application_name}-application-lb-logs"
database_name = aws_athena_database.lb-access-logs[0].name
table_type = "EXTERNAL_TABLE"
partition_keys {
name = "day"
type = "string"
}
parameters = {
"projection.enabled" = "true"
"projection.day.format" = "yyyy/MM/dd"
"projection.day.interval" = "1"
"projection.day.interval.unit" = "DAYS"
"projection.day.type" = "date"
"projection.day.range" = "2023/01/01,NOW"
"storage.location.template" = var.existing_bucket_name != "" ? "s3://${var.existing_bucket_name}/${var.application_name}/AWSLogs/${var.account_number}/elasticloadbalancing/${var.region}/$${day}" : "s3://${module.s3-bucket[0].bucket.id}/${var.application_name}/AWSLogs/${var.account_number}/elasticloadbalancing/${var.region}/$${day}"
}
storage_descriptor {
location = var.existing_bucket_name != "" ? "s3://${var.existing_bucket_name}/${var.application_name}/AWSLogs/${var.account_number}/elasticloadbalancing/${var.region}/" : "s3://${module.s3-bucket[0].bucket.id}/${var.application_name}/AWSLogs/${var.account_number}/elasticloadbalancing/${var.region}/"
input_format = "org.apache.hadoop.mapred.TextInputFormat"
output_format = "org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat"
ser_de_info {
name = "application_lb_logs"
parameters = {
"serialization.format" = "1",
"input.regex" = "([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*):([0-9]*) ([^ ]*)[:-]([0-9]*) ([-.0-9]*) ([-.0-9]*) ([-.0-9]*) (|[-0-9]*) (-|[-0-9]*) ([-0-9]*) ([-0-9]*) \"([^ ]*) (.*) (- |[^ ]*)\" \"([^\"]*)\" ([A-Z0-9-_]+) ([A-Za-z0-9.-]*) ([^ ]*) \"([^\"]*)\" \"([^\"]*)\" \"([^\"]*)\" ([-.0-9]*) ([^ ]*) \"([^\"]*)\" \"([^\"]*)\" \"([^ ]*)\" \"([^s]+?)\" \"([^s]+)\" \"([^ ]*)\" \"([^ ]*)\" ([^ ]*)"
}
serialization_library = "org.apache.hadoop.hive.serde2.RegexSerDe"
}
columns {
name = "type"
type = "string"
}
columns {
name = "time"
type = "string"
}
columns {
name = "elb"
type = "string"
}
columns {
name = "client_ip"
type = "string"
}
columns {
name = "client_port"
type = "int"
}
columns {
name = "target_ip"
type = "string"
}
columns {
name = "target_port"
type = "int"
}
columns {
name = "request_processing_time"
type = "double"
}
columns {
name = "target_processing_time"
type = "double"
}
columns {
name = "response_processing_time"
type = "double"
}
columns {
name = "elb_status_code"
type = "int"
}
columns {
name = "target_status_code"
type = "string"
}
columns {
name = "received_bytes"
type = "bigint"
}
columns {
name = "sent_bytes"
type = "bigint"
}
columns {
name = "request_verb"
type = "string"
}
columns {
name = "request_url"
type = "string"
}
columns {
name = "request_proto"
type = "string"
}
columns {
name = "user_agent"
type = "string"
}
columns {
name = "ssl_cipher"
type = "string"
}
columns {
name = "ssl_protocol"
type = "string"
}
columns {
name = "target_group_arn"
type = "string"
}
columns {
name = "trace_id"
type = "string"
}
columns {
name = "domain_name"
type = "string"
}
columns {
name = "chosen_cert_arn"
type = "string"
}
columns {
name = "matched_rule_priority"
type = "string"
}
columns {
name = "request_creation_time"
type = "string"
}
columns {
name = "actions_executed"
type = "string"
}
columns {
name = "redirect_url"
type = "string"
}
columns {
name = "lambda_error_reason"
type = "string"
}
columns {
name = "target_port_list"
type = "string"
}
columns {
name = "target_status_code_list"
type = "string"
}
columns {
name = "classification"
type = "string"
}
columns {
name = "classification_reason"
type = "string"
}
columns {
name = "conn_trace_id"
type = "string"
}
}
}
resource "aws_glue_catalog_table" "network_lb_logs" {
count = var.access_logs && var.load_balancer_type == "network" ? 1 : 0
name = "${var.application_name}-network-lb-logs"
database_name = aws_athena_database.lb-access-logs[0].name
table_type = "EXTERNAL_TABLE"
storage_descriptor {
location = var.existing_bucket_name != "" ? "s3://${var.existing_bucket_name}/${var.application_name}/AWSLogs/${var.account_number}/elasticloadbalancing/${var.region}" : "s3://${module.s3-bucket[0].bucket.id}/${var.application_name}/AWSLogs/${var.account_number}/elasticloadbalancing/${var.region}"
input_format = "org.apache.hadoop.mapred.TextInputFormat"
output_format = "org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat"
ser_de_info {
name = "network_lb_logs"
parameters = {
"serialization.format" = "1",
"input.regex" = "([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*):([0-9]*) ([^ ]*):([0-9]*) ([-.0-9]*) ([-.0-9]*) ([-0-9]*) ([-0-9]*) ([-0-9]*) ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*)$"
}
serialization_library = "org.apache.hadoop.hive.serde2.RegexSerDe"
}
columns {
name = "type"
type = "string"
}
columns {
name = "version"
type = "string"
}
columns {
name = "time"
type = "string"
}
columns {
name = "elb"
type = "string"
}
columns {
name = "listener_id"
type = "string"
}
columns {
name = "client_ip"
type = "string"
}
columns {
name = "client_port"
type = "int"
}
columns {
name = "target_ip"
type = "string"
}
columns {
name = "target_port"
type = "int"
}
columns {
name = "tcp_connection_time"
type = "double"
}
columns {
name = "tls_handshake_time"
type = "double"
}
columns {
name = "received_bytes"
type = "bigint"
}
columns {
name = "sent_bytes"
type = "bigint"
}
columns {
name = "incoming_tls_alert"
type = "int"
}
columns {
name = "cert_arn"
type = "string"
}
columns {
name = "certificate_serial"
type = "string"
}
columns {
name = "tls_cipher_suite"
type = "string"
}
columns {
name = "tls_protocol_version"
type = "string"
}
columns {
name = "tls_named_group"
type = "string"
}
columns {
name = "domain_name"
type = "string"
}
columns {
name = "alpn_fe_protocol"
type = "string"
}
columns {
name = "alpn_be_protocol"
type = "string"
}
columns {
name = "alpn_client_preference_list"
type = "string"
}
columns {
name = "tls_connection_creation_time"
type = "string"
}
}
}