-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
205 lines (178 loc) · 6.87 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
data "aws_caller_identity" "current" {}
locals {
account_id = data.aws_caller_identity.current.account_id
lambda_assume_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow"
}
]
}
EOF
}
# ====== log-retention ======
resource "aws_iam_role" "log_retention" {
name = var.log_retention_role_name
assume_role_policy = local.lambda_assume_policy
force_detach_policies = true
tags = var.tags
}
data "aws_iam_policy_document" "log_retention_policy" {
# checkov:skip=CKV_AWS_111: Write access required
statement {
effect = "Allow"
actions = [
"ec2:DescribeRegions",
"logs:DescribeLogGroups",
"logs:PutRetentionPolicy"
]
resources = ["*"]
}
}
resource "aws_iam_role_policy" "log_retention_policy" {
name = var.log_retention_role_name
role = aws_iam_role.log_retention.id
policy = data.aws_iam_policy_document.log_retention_policy.json
}
resource "aws_iam_role_policy_attachment" "log_retention_logs" {
role = aws_iam_role.log_retention.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
resource "aws_cloudwatch_event_rule" "log_retention" {
name = "UpdateLogRetention"
description = "Triggers a lambda function periodically which updates retention period of all log groups"
is_enabled = length(var.aws_regions) > 1 ? true : false
schedule_expression = "cron(${var.cron_expression})"
}
resource "aws_cloudwatch_event_target" "log_retention" {
rule = aws_cloudwatch_event_rule.log_retention.name
target_id = "TriggerUpdateLogRetentionLambda"
arn = aws_lambda_function.log_retention.arn
}
resource "aws_lambda_permission" "log_retention" {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.log_retention.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.log_retention.arn
}
resource "aws_cloudwatch_log_group" "log_retention" {
name = "/aws/lambda/${var.log_retention_function_name}"
retention_in_days = var.lambda_cw_log_group_retention
kms_key_id = var.lambda_cw_logs_kms_key_arn
tags = var.tags
}
resource "aws_lambda_function" "log_retention" {
# checkov:skip=CKV_AWS_50: Enabling X-Ray tracing depends on user
# checkov:skip=CKV_AWS_115: Setting reserved concurrent execution depends on user
# checkov:skip=CKV_AWS_116: DLQ not required
# checkov:skip=CKV_AWS_117: VPC deployment not required
# checkov:skip=CKV_AWS_173: By default environment variables are encrypted at rest
# checkov:skip=CKV_AWS_272: Code signing not required
function_name = var.log_retention_function_name
description = "Update log group retention period"
role = aws_iam_role.log_retention.arn
filename = data.archive_file.retention.output_path
source_code_hash = data.archive_file.retention.output_base64sha256
handler = "retention.handler"
runtime = var.lambda_runtime
memory_size = var.lambda_memory_size
timeout = var.lambda_timeout
reserved_concurrent_executions = var.lambda_reserved_concurrent_executions
tracing_config {
mode = var.lambda_xray_tracing_mode
}
environment {
variables = {
AWS_REGIONS = join(", ", var.aws_regions)
LOG_RETENTION_DAYS = var.log_retention_days
}
}
tags = var.tags
}
# ====== log-encryption ======
resource "aws_iam_role" "log_encryption" {
name = var.log_encryption_role_name
assume_role_policy = local.lambda_assume_policy
force_detach_policies = true
tags = var.tags
}
data "aws_iam_policy_document" "log_encryption_policy" {
# checkov:skip=CKV_AWS_111: Write access required
statement {
effect = "Allow"
actions = [
"ec2:DescribeRegions",
"logs:DescribeLogGroups",
"logs:AssociateKmsKey",
"logs:DisassociateKmsKey"
]
resources = ["*"]
}
}
resource "aws_iam_role_policy" "log_encryption_policy" {
name = var.log_encryption_role_name
role = aws_iam_role.log_encryption.id
policy = data.aws_iam_policy_document.log_encryption_policy.json
}
resource "aws_iam_role_policy_attachment" "log_encryption_logs" {
role = aws_iam_role.log_encryption.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
resource "aws_cloudwatch_event_rule" "log_encryption" {
name = "UpdateLogEncryption"
description = "Triggers a lambda function periodically which updates/removes KMS key for all log groups"
is_enabled = length(var.log_encryption_config) > 1 ? true : false
schedule_expression = "cron(${var.cron_expression})"
}
resource "aws_cloudwatch_event_target" "log_encryption" {
rule = aws_cloudwatch_event_rule.log_encryption.name
target_id = "TriggerUpdateLogEncryptionLambda"
arn = aws_lambda_function.log_encryption.arn
}
resource "aws_lambda_permission" "log_encryption" {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.log_encryption.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.log_encryption.arn
}
resource "aws_cloudwatch_log_group" "log_encryption" {
name = "/aws/lambda/${var.log_encryption_function_name}"
retention_in_days = var.lambda_cw_log_group_retention
kms_key_id = var.lambda_cw_logs_kms_key_arn
tags = var.tags
}
resource "aws_lambda_function" "log_encryption" {
# checkov:skip=CKV_AWS_50: Enabling X-Ray tracing depends on user
# checkov:skip=CKV_AWS_115: Setting reserved concurrent execution depends on user
# checkov:skip=CKV_AWS_116: DLQ not required
# checkov:skip=CKV_AWS_117: VPC deployment not required
# checkov:skip=CKV_AWS_173: By default environment variables are encrypted at rest
# checkov:skip=CKV_AWS_272: Code signing not required
function_name = var.log_encryption_function_name
description = "Update/Remove KMS key for log group"
role = aws_iam_role.log_encryption.arn
filename = data.archive_file.encryption.output_path
source_code_hash = data.archive_file.encryption.output_base64sha256
handler = "encryption.handler"
runtime = var.lambda_runtime
memory_size = var.lambda_memory_size
timeout = var.lambda_timeout
reserved_concurrent_executions = var.lambda_reserved_concurrent_executions
tracing_config {
mode = var.lambda_xray_tracing_mode
}
environment {
variables = {
LOG_ENCRYPTION_CONFIG = jsonencode(var.log_encryption_config)
}
}
tags = var.tags
}