-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
174 lines (157 loc) · 5.03 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
##########################################
# Data configurations for the module #
##########################################
data "aws_caller_identity" "current" {}
data "aws_region" "current" {}
data "aws_iam_policy_document" "cloudtrail_cloudwatch" {
statement {
sid = "AWSCloudTrailC"
actions = ["logs:CreateLogStream", "logs:PutLogEvents"]
resources = ["arn:aws:logs:*:${data.aws_caller_identity.current.account_id}:log-group:${aws_cloudwatch_log_group.cloudtrail.name}:log-stream:*"]
}
}
data "aws_iam_policy_document" "cloudtrail_key_policy" {
policy_id = "Key policy created by CloudTrail"
statement {
sid = "Enable IAM User Permissions"
principals {
type = "AWS"
identifiers = [
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"
]
}
actions = ["kms:*"]
resources = ["*"]
}
statement {
sid = "Allow CloudTrail to encrypt logs"
principals {
type = "Service"
identifiers = ["cloudtrail.amazonaws.com"]
}
actions = ["kms:GenerateDataKey*"]
resources = ["*"]
condition {
test = "StringLike"
variable = "kms:EncryptionContext:aws:cloudtrail:arn"
values = ["arn:aws:cloudtrail:*:${data.aws_caller_identity.current.account_id}:trail/*"]
}
dynamic "condition" {
for_each = var.organization ? [1] : []
content {
test = "StringEquals"
variable = "AWS:SourceArn"
values = ["arn:aws:cloudtrail:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:trail/${var.name}"]
}
}
}
statement {
sid = "Allow CloudTrail to describe key"
principals {
type = "Service"
identifiers = ["cloudtrail.amazonaws.com"]
}
actions = ["kms:DescribeKey"]
resources = ["*"]
}
statement {
sid = "Allow principals in the account to decrypt log files"
principals {
type = "AWS"
identifiers = ["*"]
}
actions = [
"kms:Decrypt",
"kms:ReEncryptFrom",
]
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:aws:cloudtrail:*:${data.aws_caller_identity.current.account_id}:trail/*"]
}
}
statement {
sid = "Allow alias creation during setup"
principals {
type = "AWS"
identifiers = ["*"]
}
actions = ["kms:CreateAlias"]
resources = ["*"]
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]
}
}
statement {
sid = "Enable cross account log decryption"
principals {
type = "AWS"
identifiers = ["*"]
}
actions = [
"kms:Decrypt",
"kms:ReEncryptFrom",
]
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:aws:cloudtrail:*:${data.aws_caller_identity.current.account_id}:trail/*"]
}
}
}
##########################################
# KMS and encryption resources #
##########################################
resource "aws_kms_key" "cloudtrail" {
description = var.description
enable_key_rotation = true
deletion_window_in_days = 7
policy = data.aws_iam_policy_document.cloudtrail_key_policy.json
tags = var.tags
}
resource "aws_kms_alias" "cloudtrail" {
target_key_id = aws_kms_key.cloudtrail.arn
name = "alias/cloudtrail"
}
##########################################
# CloudTrail Resources #
##########################################
resource "aws_cloudtrail" "this" {
name = var.name
s3_bucket_name = aws_s3_bucket.cloudtrail-logging.bucket
cloud_watch_logs_group_arn = "${aws_cloudwatch_log_group.cloudtrail.arn}:*"
cloud_watch_logs_role_arn = aws_iam_role.cloudtrail_cloudwatch_role.arn
kms_key_id = aws_kms_key.cloudtrail.arn
enable_log_file_validation = true
is_multi_region_trail = true
is_organization_trail = var.organization
tags = var.tags
depends_on = [aws_s3_bucket_policy.cloudtrail-s3]
}
##########################################
# CloudWatch resources #
##########################################
resource "aws_cloudwatch_log_group" "cloudtrail" {
name = var.cloudwatch_log_group_name
retention_in_days = var.cloudwatch_retention_in_days
tags = var.tags
}