forked from cloudposse/terraform-aws-cloudtrail
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
94 lines (87 loc) · 2.68 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
resource "aws_cloudwatch_log_group" "self" {
name = "/cloudtrail/${var.name}"
}
resource "aws_iam_role" "self" {
name = "cloudtrail-cloudwatch-${var.name}-role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "cloudtrail.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}
resource "aws_iam_role_policy" "self" {
name = "cloudtrail-cloudwatch-${var.name}-policy"
role = aws_iam_role.self.id
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AWSCloudTrailCreateLogStream20230110",
"Effect": "Allow",
"Action": [
"logs:CreateLogStream"
],
"Resource": [
"${aws_cloudwatch_log_group.self.arn}:*"
]
},
{
"Sid": "AWSCloudTrailPutLogEvents20230110",
"Effect": "Allow",
"Action": [
"logs:PutLogEvents"
],
"Resource": [
"${aws_cloudwatch_log_group.self.arn}:*"
]
}
]
}
EOF
}
resource "aws_cloudtrail" "default" {
count = module.this.enabled ? 1 : 0
name = module.this.id
enable_logging = var.enable_logging
s3_bucket_name = var.s3_bucket_name
enable_log_file_validation = var.enable_log_file_validation
sns_topic_name = var.sns_topic_name
is_multi_region_trail = var.is_multi_region_trail
include_global_service_events = var.include_global_service_events
cloud_watch_logs_role_arn = var.cloud_watch_logs ? aws_iam_role.self.arn : ""
cloud_watch_logs_group_arn = var.cloud_watch_logs ? "${aws_cloudwatch_log_group.self.arn}:*" : ""
tags = module.this.tags
kms_key_id = var.kms_key_arn
is_organization_trail = var.is_organization_trail
s3_key_prefix = var.s3_key_prefix
dynamic "event_selector" {
for_each = var.event_selector
content {
include_management_events = lookup(event_selector.value, "include_management_events", null)
read_write_type = lookup(event_selector.value, "read_write_type", null)
dynamic "data_resource" {
for_each = lookup(event_selector.value, "data_resource", [])
content {
type = data_resource.value.type
values = data_resource.value.values
}
}
}
}
dynamic "insight_selector" {
for_each = var.insight_selector
content {
insight_type = lookup(insight_selector.value, "insight_type", null)
}
}
}