-
Notifications
You must be signed in to change notification settings - Fork 38
/
main.tf
496 lines (411 loc) · 14.7 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
locals {
lambda_function_source = var.local_lambda_file != null ? var.local_lambda_file : "${path.module}/files"
lambda_function_handler = var.local_lambda_file_handler != null ? var.local_lambda_file_handler : "kinesis-firehose-cloudwatch-logs-processor.handler"
cloudwatch_log_regions = var.region == null ? var.cloudwatch_log_regions : [var.region]
}
# Kenisis firehose stream
# Record Transformation Required, called "processing_configuration" in Terraform
resource "aws_kinesis_firehose_delivery_stream" "kinesis_firehose" {
name = var.firehose_name
destination = "splunk"
dynamic "server_side_encryption" {
for_each = var.firehose_server_side_encryption_enabled == true ? [1] : []
content {
enabled = var.firehose_server_side_encryption_enabled
key_type = var.firehose_server_side_encryption_key_type
key_arn = var.firehose_server_side_encryption_key_arn
}
}
splunk_configuration {
hec_endpoint = var.hec_url
hec_token = var.hec_token != null ? module.hec_token_kms_secret[0].hec_token_kms_secret : var.self_managed_hec_token
hec_acknowledgment_timeout = var.hec_acknowledgment_timeout
hec_endpoint_type = var.hec_endpoint_type
s3_backup_mode = var.s3_backup_mode
retry_duration = var.kinesis_firehose_retry_duration
s3_configuration {
role_arn = aws_iam_role.kinesis_firehose.arn
prefix = var.s3_prefix
bucket_arn = aws_s3_bucket.kinesis_firehose_s3_bucket.arn
buffering_size = var.kinesis_firehose_buffer
buffering_interval = var.kinesis_firehose_buffer_interval
compression_format = var.s3_compression_format
}
processing_configuration {
enabled = var.firehose_processing_enabled
processors {
type = "Lambda"
parameters {
parameter_name = "LambdaArn"
parameter_value = "${aws_lambda_function.firehose_lambda_transform.arn}:$LATEST"
}
parameters {
parameter_name = "RoleArn"
parameter_value = aws_iam_role.kinesis_firehose.arn
}
dynamic "parameters" {
for_each = var.lambda_processing_buffer_size_in_mb != null ? [1] : []
content {
parameter_name = "BufferSizeInMBs"
parameter_value = var.lambda_processing_buffer_size_in_mb
}
}
dynamic "parameters" {
for_each = var.lambda_processing_buffer_interval_in_seconds != null ? [1] : []
content {
parameter_name = "BufferIntervalInSeconds"
parameter_value = var.lambda_processing_buffer_interval_in_seconds
}
}
}
}
cloudwatch_logging_options {
enabled = var.enable_fh_cloudwatch_logging
log_group_name = aws_cloudwatch_log_group.kinesis_logs.name
log_stream_name = aws_cloudwatch_log_stream.kinesis_logs.name
}
}
tags = var.tags
}
module "hec_token_kms_secret" {
count = var.hec_token != null ? 1 : 0
source = "./modules/kms_secrets"
hec_token = var.hec_token
encryption_context = var.encryption_context
}
# S3 Bucket for Kinesis Firehose s3_backup_mode
resource "aws_s3_bucket" "kinesis_firehose_s3_bucket" {
bucket = var.s3_bucket_name
object_lock_enabled = var.s3_bucket_object_lock_enabled
tags = var.tags
}
resource "aws_s3_bucket_versioning" "kinesis_firehose_s3_bucket_versioning" {
count = var.aws_s3_bucket_versioning == null ? 0 : 1
bucket = aws_s3_bucket.kinesis_firehose_s3_bucket.id
versioning_configuration {
status = var.aws_s3_bucket_versioning
}
}
resource "aws_s3_bucket_acl" "kinesis_firehose_s3_bucket" {
depends_on = [aws_s3_bucket_ownership_controls.kinesis_firehose_s3_bucket]
bucket = aws_s3_bucket.kinesis_firehose_s3_bucket.bucket
acl = "private"
}
resource "aws_s3_bucket_ownership_controls" "kinesis_firehose_s3_bucket" {
#checkov:skip=CKV2_AWS_65: Ensure access control lists for S3 buckets are disabled
bucket = aws_s3_bucket.kinesis_firehose_s3_bucket.id
rule {
object_ownership = "BucketOwnerPreferred"
}
}
resource "aws_s3_bucket_server_side_encryption_configuration" "kinesis_firehose_s3_bucket" {
bucket = aws_s3_bucket.kinesis_firehose_s3_bucket.bucket
rule {
bucket_key_enabled = var.s3_bucket_key_enabled
apply_server_side_encryption_by_default {
kms_master_key_id = var.s3_bucket_server_side_encryption_kms_master_key_id
sse_algorithm = var.s3_bucket_server_side_encryption_algorithm
}
}
}
resource "aws_s3_bucket_public_access_block" "kinesis_firehose_s3_bucket" {
count = var.s3_bucket_block_public_access_enabled
bucket = aws_s3_bucket.kinesis_firehose_s3_bucket.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
resource "aws_s3_bucket_object_lock_configuration" "kinesis_firehose_s3_lock" {
count = var.s3_bucket_object_lock_enabled == "Enabled" ? 1 : 0
bucket = aws_s3_bucket.kinesis_firehose_s3_bucket.id
object_lock_enabled = var.s3_bucket_object_lock_enabled
expected_bucket_owner = var.expected_bucket_owner
token = var.object_lock_configuration_token
rule {
default_retention {
mode = var.object_lock_configuration_mode
days = var.object_lock_configuration_days
years = var.object_lock_configuration_years
}
}
}
# Cloudwatch logging group for Kinesis Firehose
resource "aws_cloudwatch_log_group" "kinesis_logs" {
#checkov:skip=CKV_AWS_338: Ensure CloudWatch log groups retains logs for at least 1 year
name = "/aws/kinesisfirehose/${var.firehose_name}"
retention_in_days = var.cloudwatch_log_retention
kms_key_id = var.cloudwach_log_group_kms_key_id
tags = var.tags
}
# Create the stream
resource "aws_cloudwatch_log_stream" "kinesis_logs" {
name = var.log_stream_name
log_group_name = aws_cloudwatch_log_group.kinesis_logs.name
}
# Role for the transformation Lambda function attached to the kinesis stream
resource "aws_iam_role" "kinesis_firehose_lambda" {
name = var.kinesis_firehose_lambda_role_name
description = "Role for Lambda function to transformation CloudWatch logs into Splunk compatible format"
assume_role_policy = <<POLICY
{
"Statement": [
{
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
}
}
],
"Version": "2012-10-17"
}
POLICY
tags = var.tags
}
data "aws_iam_policy_document" "lambda_policy_doc" {
dynamic "statement" {
for_each = var.arn_cloudwatch_logs_to_ship != null ? [var.arn_cloudwatch_logs_to_ship] : []
content {
actions = [
"logs:GetLogEvents",
]
resources = [
var.arn_cloudwatch_logs_to_ship,
]
effect = "Allow"
}
}
dynamic "statement" {
for_each = toset(var.cloudwatch_log_group_names_to_ship) != null ? toset(var.cloudwatch_log_group_names_to_ship) : []
content {
actions = [
"logs:GetLogEvents",
]
resources = [
"arn:${data.aws_partition.current.partition}:logs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:log-group:${statement.value}:*"
]
effect = "Allow"
}
}
dynamic "statement" {
for_each = var.name_cloudwatch_logs_to_ship != null ? [var.name_cloudwatch_logs_to_ship] : []
content {
actions = [
"logs:GetLogEvents",
]
resources = [
"arn:${data.aws_partition.current.partition}:logs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:log-group:${statement.value}:*"
]
effect = "Allow"
}
}
statement {
actions = [
"firehose:PutRecordBatch",
]
resources = [
aws_kinesis_firehose_delivery_stream.kinesis_firehose.arn,
]
}
statement {
actions = [
"logs:PutLogEvents",
]
resources = [
"*",
]
effect = "Allow"
}
statement {
actions = [
"logs:CreateLogGroup",
]
resources = [
"*",
]
effect = "Allow"
}
statement {
actions = [
"logs:CreateLogStream",
]
resources = [
"*",
]
effect = "Allow"
}
}
resource "aws_iam_policy" "lambda_transform_policy" {
name = var.lambda_iam_policy_name
policy = data.aws_iam_policy_document.lambda_policy_doc.json
}
resource "aws_iam_role_policy_attachment" "lambda_policy_role_attachment" {
role = aws_iam_role.kinesis_firehose_lambda.name
policy_arn = aws_iam_policy.lambda_transform_policy.arn
}
# Create the lambda function
# The lambda function to transform data from compressed format in Cloudwatch to something Splunk can handle (uncompressed)
resource "aws_lambda_function" "firehose_lambda_transform" {
function_name = var.lambda_function_name
description = "Transform data from CloudWatch format to Splunk compatible format"
filename = data.archive_file.lambda_function.output_path
role = aws_iam_role.kinesis_firehose_lambda.arn
handler = local.lambda_function_handler
source_code_hash = data.archive_file.lambda_function.output_base64sha256
runtime = var.nodejs_runtime
memory_size = var.lambda_function_memory_size
timeout = var.lambda_function_timeout
reserved_concurrent_executions = var.lambda_reserved_concurrent_executions
kms_key_arn = var.lambda_function_environment_variables != {} ? var.lambda_kms_key_arn : null
environment {
variables = var.lambda_function_environment_variables
}
dynamic "tracing_config" {
for_each = var.lambda_tracing_config == null ? [] : [1]
content {
mode = var.lambda_tracing_config
}
}
tags = var.tags
}
# Cloudwatch logging group for Transform Lambda
resource "aws_cloudwatch_log_group" "firehose_lambda_transform" {
#checkov:skip=CKV_AWS_338: Ensure CloudWatch log groups retains logs for at least 1 year
name = "/aws/lambda/${var.lambda_function_name}"
retention_in_days = var.cloudwatch_log_retention
kms_key_id = var.cloudwach_log_group_kms_key_id
tags = var.tags
}
# kinesis-firehose-cloudwatch-logs-processor.js was taken by copy/paste from the AWS UI. It is predefined blueprint
# code supplied to AWS by Splunk.
data "archive_file" "lambda_function" {
type = "zip"
source_dir = local.lambda_function_source
excludes = ["kinesis-firehose-cloudwatch-logs-processor.zip"]
output_path = "${path.module}/files/kinesis-firehose-cloudwatch-logs-processor.zip"
}
# Role for Kenisis Firehose
resource "aws_iam_role" "kinesis_firehose" {
name = var.kinesis_firehose_role_name
description = "IAM Role for Kenisis Firehose"
assume_role_policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Principal": {
"Service": "firehose.amazonaws.com"
},
"Action": "sts:AssumeRole",
"Effect": "Allow"
}
]
}
POLICY
tags = var.tags
}
data "aws_iam_policy_document" "kinesis_firehose_policy_document" {
statement {
actions = [
"s3:AbortMultipartUpload",
"s3:GetBucketLocation",
"s3:GetObject",
"s3:ListBucket",
"s3:ListBucketMultipartUploads",
"s3:PutObject",
]
resources = [
aws_s3_bucket.kinesis_firehose_s3_bucket.arn,
"${aws_s3_bucket.kinesis_firehose_s3_bucket.arn}/*",
]
effect = "Allow"
}
statement {
actions = [
"lambda:InvokeFunction",
"lambda:GetFunctionConfiguration",
]
resources = [
"${aws_lambda_function.firehose_lambda_transform.arn}:$LATEST",
]
}
statement {
actions = [
"logs:PutLogEvents",
]
resources = [
aws_cloudwatch_log_group.kinesis_logs.arn,
aws_cloudwatch_log_stream.kinesis_logs.arn,
]
effect = "Allow"
}
}
resource "aws_iam_policy" "kinesis_firehose_iam_policy" {
name = var.kinesis_firehose_iam_policy_name
policy = data.aws_iam_policy_document.kinesis_firehose_policy_document.json
}
resource "aws_iam_role_policy_attachment" "kinesis_fh_role_attachment" {
role = aws_iam_role.kinesis_firehose.name
policy_arn = aws_iam_policy.kinesis_firehose_iam_policy.arn
}
data "aws_iam_policy_document" "cloudwatch_to_firehose_trust_assume_policy" {
statement {
actions = ["sts:AssumeRole"]
effect = "Allow"
principals {
type = "Service"
identifiers = [for region in local.cloudwatch_log_regions : "logs.${region}.amazonaws.com"]
}
}
}
resource "aws_iam_role" "cloudwatch_to_firehose_trust" {
name = var.cloudwatch_to_firehose_trust_iam_role_name
description = "Role for CloudWatch Log Group subscriptions"
assume_role_policy = data.aws_iam_policy_document.cloudwatch_to_firehose_trust_assume_policy.json
}
data "aws_iam_policy_document" "cloudwatch_to_fh_access_policy" {
statement {
actions = [
"firehose:*",
]
effect = "Allow"
resources = [
aws_kinesis_firehose_delivery_stream.kinesis_firehose.arn,
]
}
statement {
actions = [
"iam:PassRole",
]
effect = "Allow"
resources = [
aws_iam_role.cloudwatch_to_firehose_trust.arn,
]
}
}
resource "aws_iam_policy" "cloudwatch_to_fh_access_policy" {
name = var.cloudwatch_to_fh_access_policy_name
description = "Cloudwatch to Firehose Subscription Policy"
policy = data.aws_iam_policy_document.cloudwatch_to_fh_access_policy.json
}
resource "aws_iam_role_policy_attachment" "cloudwatch_to_fh" {
role = aws_iam_role.cloudwatch_to_firehose_trust.name
policy_arn = aws_iam_policy.cloudwatch_to_fh_access_policy.arn
}
resource "aws_cloudwatch_log_subscription_filter" "cloudwatch_log_filter" {
count = var.name_cloudwatch_logs_to_ship != null ? signum(length(var.name_cloudwatch_logs_to_ship)) : 0
name = var.cloudwatch_log_filter_name
role_arn = aws_iam_role.cloudwatch_to_firehose_trust.arn
destination_arn = aws_kinesis_firehose_delivery_stream.kinesis_firehose.arn
log_group_name = var.name_cloudwatch_logs_to_ship
filter_pattern = var.subscription_filter_pattern
}
resource "aws_cloudwatch_log_subscription_filter" "cloudwatch_log_filters" {
for_each = var.cloudwatch_log_group_names_to_ship != null ? toset(var.cloudwatch_log_group_names_to_ship) : toset([])
name = "${var.cloudwatch_log_filter_name}_${each.value}"
role_arn = aws_iam_role.cloudwatch_to_firehose_trust.arn
destination_arn = aws_kinesis_firehose_delivery_stream.kinesis_firehose.arn
log_group_name = each.value
filter_pattern = var.subscription_filter_pattern
}