forked from DNXLabs/terraform-aws-log-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudwatch-logs-exporter.tf
263 lines (232 loc) · 6.8 KB
/
cloudwatch-logs-exporter.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
locals {
arn_format = "arn:${data.aws_partition.current.partition}"
}
data "archive_file" "log_exporter" {
type = "zip"
source_file = "${path.module}/lambda/cloudwatch-to-s3.py"
output_path = "${path.module}/lambda/tmp/cloudwatch-to-s3.zip"
}
data "aws_region" "current" {}
data "aws_caller_identity" "current" {}
data "aws_partition" "current" {}
resource "random_string" "random" {
length = 8
special = false
upper = false
number = false
}
resource "aws_iam_role" "log_exporter" {
name = "log-exporter-${var.cloudwatch_logs_export_bucket}"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_role_policy" "log_exporter" {
name = "log-exporter-${var.cloudwatch_logs_export_bucket}"
role = aws_iam_role.log_exporter.id
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"logs:CreateExportTask",
"logs:Describe*",
"logs:ListTagsLogGroup"
],
"Effect": "Allow",
"Resource": "*"
},
{
"Action": [
"ssm:DescribeParameters",
"ssm:GetParameter",
"ssm:GetParameters",
"ssm:GetParametersByPath",
"ssm:PutParameter"
],
"Resource": "arn:aws:ssm:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:parameter/log-exporter-last-export/*",
"Effect": "Allow"
},
{
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:log-group:/aws/lambda/log-exporter-*",
"Effect": "Allow"
},
{
"Sid": "AllowCrossAccountObjectAcc",
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:PutObjectACL"
],
"Resource": "arn:aws:s3:::${var.cloudwatch_logs_export_bucket}/*"
},
{
"Sid": "AllowCrossAccountBucketAcc",
"Effect": "Allow",
"Action": [
"s3:PutBucketAcl",
"s3:GetBucketAcl"
],
"Resource": "arn:aws:s3:::${var.cloudwatch_logs_export_bucket}"
}
]
}
EOF
}
resource "aws_lambda_function" "log_exporter" {
filename = data.archive_file.log_exporter.output_path
function_name = "log-exporter-${var.cloudwatch_logs_export_bucket}"
role = aws_iam_role.log_exporter.arn
handler = "cloudwatch-to-s3.lambda_handler"
source_code_hash = data.archive_file.log_exporter.output_base64sha256
timeout = 300
runtime = "python3.8"
environment {
variables = {
S3_BUCKET = var.cloudwatch_logs_export_bucket,
AWS_ACCOUNT = data.aws_caller_identity.current.account_id
}
}
depends_on = [
aws_iam_role_policy_attachment.lambda_logs,
aws_cloudwatch_log_group.this,
]
}
resource "aws_cloudwatch_event_rule" "log_exporter" {
name = "log-exporter-${var.cloudwatch_logs_export_bucket}"
description = "Fires periodically to export logs to S3"
schedule_expression = "rate(4 hours)"
}
resource "aws_cloudwatch_event_target" "log_exporter" {
rule = aws_cloudwatch_event_rule.log_exporter.name
target_id = "log-exporter-${var.cloudwatch_logs_export_bucket}"
arn = aws_lambda_function.log_exporter.arn
}
resource "aws_lambda_permission" "log_exporter" {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.log_exporter.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.log_exporter.arn
}
# ---------------------------------------------------------------------------------------------------------------------
# CREATE A KMS
# We can attach KMS to CloudWatch Log.
# ---------------------------------------------------------------------------------------------------------------------
data "aws_iam_policy_document" "kms" {
statement {
sid = "Enable Root User Permissions"
effect = "Allow"
actions = [
"kms:Create*",
"kms:Describe*",
"kms:Enable*",
"kms:List*",
"kms:Put*",
"kms:Update*",
"kms:Revoke*",
"kms:Disable*",
"kms:Get*",
"kms:Delete*",
"kms:Tag*",
"kms:Untag*",
"kms:ScheduleKeyDeletion",
"kms:CancelKeyDeletion"
]
#bridgecrew:skip=CKV_AWS_109:This policy applies only to the key it is attached to
#bridgecrew:skip=CKV_AWS_111:This policy applies only to the key it is attached to
resources = [
"*"
]
principals {
type = "AWS"
identifiers = [
"${local.arn_format}:iam::${data.aws_caller_identity.current.account_id}:root"
]
}
}
statement {
sid = "Allow KMS to CloudWatch Log Group ${var.cloudwatch_logs_export_bucket}"
effect = "Allow"
actions = [
"kms:Encrypt*",
"kms:Decrypt*",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:Describe*"
]
resources = [
"*"
]
principals {
type = "Service"
identifiers = [
"logs.${data.aws_region.current.name}.amazonaws.com"
]
}
condition {
test = "ArnEquals"
variable = "kms:EncryptionContext:aws:logs:arn"
values = ["arn:aws:logs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:log-group:/aws/lambda/log-exporter-${var.cloudwatch_logs_export_bucket}"]
}
}
}
resource "aws_kms_key" "kms" {
description = "KMS key for ${var.cloudwatch_logs_export_bucket}"
deletion_window_in_days = 10
enable_key_rotation = true
policy = join("", data.aws_iam_policy_document.kms.*.json)
}
resource "aws_kms_alias" "this" {
name = "alias/log-exporter-${var.cloudwatch_logs_export_bucket}"
target_key_id = aws_kms_key.kms.key_id
}
resource "aws_cloudwatch_log_group" "this" {
name = "/aws/lambda/log-exporter-${var.cloudwatch_logs_export_bucket}"
retention_in_days = 365
kms_key_id = aws_kms_key.kms.arn
}
# See also the following AWS managed policy: AWSLambdaBasicExecutionRole
resource "aws_iam_policy" "lambda_logging" {
name = "lambda_logging"
path = "/"
description = "IAM policy for logging from a lambda"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*",
"Effect": "Allow"
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "lambda_logs" {
role = aws_iam_role.log_exporter.name
policy_arn = aws_iam_policy.lambda_logging.arn
}