This repository was archived by the owner on Feb 11, 2026. It is now read-only.
forked from trussworks/terraform-aws-cloudtrail
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
448 lines (366 loc) · 10.7 KB
/
main.tf
File metadata and controls
448 lines (366 loc) · 10.7 KB
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
# The AWS region currently being used.
data "aws_region" "current" {}
# The AWS account id
data "aws_caller_identity" "current" {}
# The AWS partition (commercial or govcloud)
data "aws_partition" "current" {}
locals {
s3_bucket_account_id = var.s3_bucket_account_id != null ? var.s3_bucket_account_id : data.aws_caller_identity.current.account_id
}
locals {
base_resources = [
"arn:${data.aws_partition.current.partition}:logs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:log-group:${var.cloudwatch_log_group_name}:*",
]
conditional_resources = var.enable_monitoring_vpc ? [
"arn:${data.aws_partition.current.partition}:logs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:log-group:${var.vpc_flow_logs_log_group_name}:*",
] : []
all_resources = concat(local.base_resources, local.conditional_resources)
}
locals {
base_princepals = [
"cloudtrail.amazonaws.com",
]
conditional_princepals = var.enable_monitoring_vpc ? [
"vpc-flow-logs.amazonaws.com",
] : []
all_princepals = concat(local.base_princepals, local.conditional_princepals)
}
#
# CloudTrail - CloudWatch
#
# This section is used for allowing CloudTrail to send logs to CloudWatch.
#
# This policy allows the CloudTrail service for any account to assume this role.
data "aws_iam_policy_document" "cloudtrail_assume_role" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = local.all_princepals
}
}
}
# This role is used by CloudTrail to send logs to CloudWatch.
resource "aws_iam_role" "cloudtrail_cloudwatch_role" {
name = var.iam_role_name
assume_role_policy = data.aws_iam_policy_document.cloudtrail_assume_role.json
}
# This CloudWatch Group is used for storing CloudTrail logs.
resource "aws_cloudwatch_log_group" "cloudtrail" {
name = var.cloudwatch_log_group_name
retention_in_days = var.log_retention_days
kms_key_id = aws_kms_key.cloudtrail.arn
tags = var.tags
}
data "aws_iam_policy_document" "cloudtrail_cloudwatch_logs" {
statement {
sid = "WriteCloudWatchLogs"
effect = "Allow"
actions = [
"logs:CreateLogStream",
"logs:PutLogEvents",
]
resources = local.all_resources
}
}
resource "aws_iam_policy" "cloudtrail_cloudwatch_logs" {
name = var.iam_policy_name
policy = data.aws_iam_policy_document.cloudtrail_cloudwatch_logs.json
}
resource "aws_iam_policy_attachment" "main" {
name = "${var.iam_policy_name}-attachment"
policy_arn = aws_iam_policy.cloudtrail_cloudwatch_logs.arn
roles = [aws_iam_role.cloudtrail_cloudwatch_role.name]
}
#
# KMS
#
# This policy is a translation of the default created by AWS when you
# manually enable CloudTrail; you can see it here:
# https://docs.aws.amazon.com/awscloudtrail/latest/userguide/default-cmk-policy.html
data "aws_iam_policy_document" "cloudtrail_kms_policy_doc" {
statement {
sid = "Enable IAM User Permissions"
effect = "Allow"
actions = ["kms:*"]
principals {
type = "AWS"
identifiers = ["arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:root"]
}
resources = ["*"]
}
statement {
sid = "Allow CloudTrail to encrypt logs"
effect = "Allow"
actions = ["kms:GenerateDataKey*"]
principals {
type = "Service"
identifiers = ["cloudtrail.amazonaws.com"]
}
resources = ["*"]
condition {
test = "StringLike"
variable = "kms:EncryptionContext:aws:cloudtrail:arn"
values = ["arn:${data.aws_partition.current.partition}:cloudtrail:*:${data.aws_caller_identity.current.account_id}:trail/*"]
}
}
statement {
sid = "Allow CloudTrail to describe key"
effect = "Allow"
actions = ["kms:DescribeKey"]
principals {
type = "Service"
identifiers = ["cloudtrail.amazonaws.com"]
}
resources = ["*"]
}
statement {
sid = "Allow principals in the account to decrypt log files"
effect = "Allow"
actions = [
"kms:Decrypt",
"kms:ReEncryptFrom",
]
principals {
type = "AWS"
identifiers = ["*"]
}
resources = ["*"]
condition {
test = "StringEquals"
variable = "kms:CallerAccount"
values = [data.aws_caller_identity.current.account_id]
}
condition {
test = "StringLike"
variable = "kms:EncryptionContext:aws:cloudtrail:arn"
values = ["arn:${data.aws_partition.current.partition}:cloudtrail:*:${data.aws_caller_identity.current.account_id}:trail/*"]
}
}
statement {
sid = "Allow alias creation during setup"
effect = "Allow"
actions = ["kms:CreateAlias"]
principals {
type = "AWS"
identifiers = ["*"]
}
condition {
test = "StringEquals"
variable = "kms:ViaService"
values = ["ec2.${data.aws_region.current.name}.amazonaws.com"]
}
condition {
test = "StringEquals"
variable = "kms:CallerAccount"
values = [data.aws_caller_identity.current.account_id]
}
resources = ["*"]
}
statement {
sid = "Enable cross account log decryption"
effect = "Allow"
actions = [
"kms:Decrypt",
"kms:ReEncryptFrom",
]
principals {
type = "AWS"
identifiers = ["*"]
}
condition {
test = "StringEquals"
variable = "kms:CallerAccount"
values = [local.s3_bucket_account_id]
}
condition {
test = "StringLike"
variable = "kms:EncryptionContext:aws:cloudtrail:arn"
values = ["arn:${data.aws_partition.current.partition}:cloudtrail:*:${data.aws_caller_identity.current.account_id}:trail/*"]
}
resources = ["*"]
}
statement {
sid = "Allow logs KMS access"
effect = "Allow"
principals {
type = "Service"
identifiers = [
"logs.${data.aws_region.current.name}.amazonaws.com",
"config.amazonaws.com"
]
}
actions = [
"kms:Encrypt*",
"kms:Decrypt*",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:Describe*",
]
resources = ["*"]
}
statement {
sid = "Allow Cloudtrail to decrypt and generate key for sns access"
effect = "Allow"
principals {
type = "Service"
identifiers = [
"sns.amazonaws.com"
]
}
actions = [
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:Encrypt*",
"kms:Describe*",
"kms:Decrypt*",
]
resources = ["*"]
}
statement {
sid = "Allow Cloudtrail to decrypt and generate key for sqs"
effect = "Allow"
principals {
type = "Service"
identifiers = [
"sqs.amazonaws.com",
]
}
actions = [
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:Encrypt*",
"kms:Describe*",
"kms:Decrypt*",
]
resources = ["*"]
}
statement {
sid = "Allow Cloudtrail to decrypt and generate key for posting to sns"
effect = "Allow"
principals {
type = "Service"
identifiers = [
"cloudtrail.amazonaws.com",
]
}
actions = [
"kms:GenerateDataKey*",
"kms:Describe*",
"kms:Decrypt*",
]
resources = ["*"]
}
statement {
sid = "allow vpc-endpoint"
effect = "Allow"
actions = [
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:Encrypt*",
"kms:Describe*",
"kms:Decrypt*",
]
resources = ["*"]
principals {
type = "AWS"
identifiers = ["*"]
}
condition {
test = "StringEquals"
variable = "aws:sourceVpce"
values = [var.vpc_endpoint]
}
}
dynamic "statement" {
for_each = var.enable_cssp_user ? [1] : []
content {
sid = "AllowCSSRCrossAccountKMSAccess"
effect = "Allow"
principals {
type = "AWS"
identifiers = [
"arn:aws-us-gov:iam::015466132150:user/cssp-apicwl-user",
"arn:aws-us-gov:iam::015466132150:role/CSSP-Cross-AccountRole"
]
}
actions = [
"kms:Decrypt*",
"kms:GenerateDataKey*",
"kms:Encrypt*",
"kms:ReEncrypt*",
"kms:Describe*"
]
resources = ["*"]
}
}
}
resource "aws_kms_key" "cloudtrail" {
description = "A KMS key used to encrypt CloudTrail log files stored in S3."
deletion_window_in_days = var.key_deletion_window_in_days
enable_key_rotation = "true"
policy = data.aws_iam_policy_document.cloudtrail_kms_policy_doc.json
tags = var.tags
}
resource "aws_kms_alias" "cloudtrail" {
name = "alias/${var.trail_name}"
target_key_id = aws_kms_key.cloudtrail.key_id
}
#
# CloudTrail
#
resource "aws_cloudtrail" "main" {
name = var.trail_name
# Send logs to CloudWatch Logs
cloud_watch_logs_group_arn = "${aws_cloudwatch_log_group.cloudtrail.arn}:*"
cloud_watch_logs_role_arn = aws_iam_role.cloudtrail_cloudwatch_role.arn
# Send logs to S3
s3_key_prefix = var.s3_key_prefix
s3_bucket_name = var.s3_bucket_name
# Note that organization trails can *only* be created in organization
# master accounts; this will fail if run in a non-master account.
is_organization_trail = var.org_trail
# use a single s3 bucket for all aws regions
is_multi_region_trail = true
# enable log file validation to detect tampering
enable_log_file_validation = true
kms_key_id = aws_kms_key.cloudtrail.arn
# Enables logging for the trail. Defaults to true. Setting this to false will pause logging.
enable_logging = var.enabled
# Enables SNS log notification
sns_topic_name = var.sns_topic_arn
# Enable Insights
dynamic "insight_selector" {
for_each = compact([
var.api_call_rate_insight ? "ApiCallRateInsight" : null,
var.api_error_rate_insight ? "ApiErrorRateInsight" : null,
])
content {
insight_type = insight_selector.value
}
}
dynamic "advanced_event_selector" {
for_each = var.advanced_event_selectors
content {
name = advanced_event_selector.value.name
dynamic "field_selector" {
for_each = advanced_event_selector.value.field_selectors
content {
field = field_selector.value.field
equals = field_selector.value.equals
starts_with = field_selector.value.starts_with
ends_with = field_selector.value.ends_with
not_equals = field_selector.value.not_equals
not_starts_with = field_selector.value.not_starts_with
not_ends_with = field_selector.value.not_ends_with
}
}
}
}
tags = var.tags
depends_on = [
aws_kms_key.cloudtrail,
aws_kms_alias.cloudtrail,
]
}