-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
387 lines (333 loc) · 12 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
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
locals {
lambda_path_include = ["**"]
lambda_path_exclude = ["**/__pycache__/**"]
# event dispatcher lambda
event_dispatcher_source_path = "${path.module}/lambdas/event_dispatcher"
event_dispatcher_files_include = setunion([
for f in local.lambda_path_include : fileset(local.event_dispatcher_source_path, f)
]...)
event_dispatcher_files_exclude = setunion([
for f in local.lambda_path_exclude : fileset(local.event_dispatcher_source_path, f)
]...)
event_dispatcher_files = sort(setsubtract(local.event_dispatcher_files_include, local.event_dispatcher_files_exclude))
event_dispatcher_dir_sha = sha1(join("", [
for f in local.event_dispatcher_files : filesha1("${local.event_dispatcher_source_path}/${f}")
]))
# create iceberg table lambda
create_iceberg_table_source_path = "${path.module}/lambdas/create_iceberg_table"
create_iceberg_table_files_include = setunion([
for f in local.lambda_path_include : fileset(local.create_iceberg_table_source_path, f)
]...)
create_iceberg_table_files_exclude = setunion([
for f in local.lambda_path_exclude : fileset(local.create_iceberg_table_source_path, f)
]...)
create_iceberg_table_files = sort(setsubtract(local.create_iceberg_table_files_include, local.create_iceberg_table_files_exclude))
create_iceberg_table_dir_sha = sha1(join("", [
for f in local.create_iceberg_table_files : filesha1("${local.create_iceberg_table_source_path}/${f}")
]))
}
module "lambda_create_iceberg_table_builder" {
source = "terraform-aws-modules/lambda/aws//modules/docker-build"
version = "7.15.0"
create_ecr_repo = true
ecr_repo = "${var.resource_prefix}-create-iceberg-table"
ecr_repo_lifecycle_policy = jsonencode({
"rules" : [
{
"rulePriority" : 1,
"description" : "Keep only the latest images",
"selection" : {
"tagStatus" : "any",
"countType" : "imageCountMoreThan",
"countNumber" : 5
},
"action" : {
"type" : "expire"
}
}
]
})
use_image_tag = true
image_tag = local.create_iceberg_table_dir_sha
keep_remotely = true
source_path = local.create_iceberg_table_source_path
triggers = {
dir_sha = local.create_iceberg_table_dir_sha
}
}
# creating a partitioned Iceberg table in Glue via Terraform is not possible: https://github.com/hashicorp/terraform-provider-aws/issues/36531
# Instead we use a lambda function that use Athena DDL to create the table, and then we invoke it via Terraform
module "lambda_create_iceberg_table" {
source = "terraform-aws-modules/lambda/aws"
version = "7.15.0"
function_name = "${var.resource_prefix}-create-iceberg-table"
description = "Takes care of creating an Iceberg table in Glue Catalog to be used by Firehose Delivery Stream"
memory_size = 128
timeout = 60
create_package = false
package_type = "Image"
# when using arm64 be sure to use the right image in the Dockerfile
architectures = ["arm64"]
attach_policy_statements = true
environment_variables = {
WORKGROUP_NAME = var.create_table_athena_workgroup_name
GLUE_DATABASE_NAME = var.glue_database_name
GLUE_TABLE_NAME = var.glue_table_name
S3_BUCKET_TABLE_LOCATION = var.s3_bucket_table_location
S3_TABLE_PREFIX = var.s3_table_location_prefix
FORCE_TABLE_CREATION = var.force_table_creation
}
policy_statements = {
athena = {
effect = "Allow",
actions = [
"athena:GetQueryExecution",
"athena:GetQueryResults",
"athena:StartQueryExecution",
],
resources = [
"*"
]
}
glue = {
effect = "Allow",
actions = [
"glue:CreateTable",
"glue:GetDatabase",
"glue:GetTable",
"glue:DeleteTable"
],
resources = [
"*"
]
}
s3_athena_query = {
effect = "Allow",
actions = [
"s3:GetBucketLocation",
"s3:GetObject",
"s3:PutObject",
],
resources = [
"arn:aws:s3:::${var.create_table_athena_s3_output_bucket_name}",
"arn:aws:s3:::${var.create_table_athena_s3_output_bucket_name}/*"
]
}
s3_iceberg_table_location = {
effect = "Allow",
actions = [
"s3:GetBucketLocation",
"s3:GetObject",
"s3:PutObject",
],
resources = [
"arn:aws:s3:::${var.s3_bucket_table_location}",
"arn:aws:s3:::${var.s3_bucket_table_location}/${var.s3_table_location_prefix}*",
]
}
}
cloudwatch_logs_retention_in_days = var.lambda_create_iceberg_table_log_group_retention_in_days
image_uri = module.lambda_create_iceberg_table_builder.image_uri
}
resource "aws_lambda_invocation" "athena_query_observability_lambda_create_iceberg_table_invoke" {
function_name = module.lambda_create_iceberg_table.lambda_function_name
input = jsonencode({
"trigger" : var.force_table_creation_trigger
})
lifecycle_scope = "CRUD"
}
resource "aws_iam_role" "athena_observability_firehose_iceberg_role" {
name = "${var.resource_prefix}-firehose-iceberg-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "firehose.amazonaws.com"
}
},
]
})
}
resource "aws_iam_role_policy" "athena_observability_firehose_iceberg_role_policy" {
name = "${var.resource_prefix}-firehose-iceberg-role"
role = aws_iam_role.athena_observability_firehose_iceberg_role.id
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = [
"glue:GetTable",
"glue:GetTableVersions",
"glue:GetTableVersion",
"glue:GetDatabase",
"glue:UpdateTable",
]
Effect = "Allow"
Resource = "*"
},
{
Action = [
"s3:AbortMultipartUpload",
"s3:GetBucketLocation",
"s3:GetObject",
"s3:ListBucket",
"s3:ListBucketMultipartUploads",
"s3:PutObject"
]
Effect = "Allow"
Resource = [
"arn:aws:s3:::${var.s3_bucket_table_location}",
"arn:aws:s3:::${var.s3_bucket_table_location}/${var.s3_table_location_prefix}*",
"arn:aws:s3:::${var.s3_bucket_table_location}/${var.firehose_s3_error_output_prefix}*",
]
},
{
Action = [
"logs:PutLogEvents",
"logs:CreateLogStream",
"logs:CreateLogGroup"
]
Effect = "Allow"
Resource = [
"${aws_cloudwatch_log_group.athena_observability_firehose_log_group.arn}:*",
"${aws_cloudwatch_log_group.athena_observability_firehose_log_group.arn}:*:*"
]
},
]
})
}
resource "aws_cloudwatch_log_group" "athena_observability_firehose_log_group" {
name = "/aws/kinesisfirehose/${var.resource_prefix}-iceberg-delivery"
retention_in_days = var.firehose_log_group_retention_in_days
}
resource "aws_cloudwatch_log_stream" "athena_observability_firehose_log_stream" {
name = "DeliveryLogs"
log_group_name = aws_cloudwatch_log_group.athena_observability_firehose_log_group.name
}
resource "aws_kinesis_firehose_delivery_stream" "athena_observability_iceberg_delivery" {
name = "${var.resource_prefix}-iceberg-delivery${var.firehose_name_suffix}"
destination = "iceberg"
iceberg_configuration {
role_arn = aws_iam_role.athena_observability_firehose_iceberg_role.arn
catalog_arn = "arn:${data.aws_partition.current.partition}:glue:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:catalog"
buffering_size = var.firehose_buffering_size
buffering_interval = var.firehose_buffering_interval
retry_duration = var.firehose_retry_duration
s3_configuration {
role_arn = aws_iam_role.athena_observability_firehose_iceberg_role.arn
bucket_arn = "arn:aws:s3:::${var.s3_bucket_table_location}"
error_output_prefix = var.firehose_s3_error_output_prefix
}
destination_table_configuration {
database_name = var.glue_database_name
table_name = var.glue_table_name
s3_error_output_prefix = var.firehose_s3_error_iceberg_prefix
}
cloudwatch_logging_options {
enabled = true
log_group_name = aws_cloudwatch_log_group.athena_observability_firehose_log_group.name
log_stream_name = aws_cloudwatch_log_stream.athena_observability_firehose_log_stream.name
}
}
depends_on = [
aws_lambda_invocation.athena_query_observability_lambda_create_iceberg_table_invoke
]
}
resource "aws_cloudwatch_event_rule" "athena_query_observability_lambda_event_bridge_rule" {
name = "${var.resource_prefix}-lambda-event-bridge-rule"
description = "Capture Athena queries"
state = var.event_bridge_rule_state
event_pattern = jsonencode({
source = ["aws.athena"],
"detail-type" = ["Athena Query State Change"]
"detail" = {
"currentState" = var.athena_query_state_change_event_pattern
}
})
}
module "athena_query_observability_lambda_event_dispatcher_builder" {
source = "terraform-aws-modules/lambda/aws//modules/docker-build"
version = "7.15.0"
create_ecr_repo = true
ecr_repo = "${var.resource_prefix}-event-dispatcher"
ecr_repo_lifecycle_policy = jsonencode({
"rules" : [
{
"rulePriority" : 1,
"description" : "Keep only the latest images",
"selection" : {
"tagStatus" : "any",
"countType" : "imageCountMoreThan",
"countNumber" : 5
},
"action" : {
"type" : "expire"
}
}
]
})
use_image_tag = true
image_tag = local.event_dispatcher_dir_sha
keep_remotely = true
source_path = local.event_dispatcher_source_path
triggers = {
dir_sha = local.event_dispatcher_dir_sha
}
}
module "athena_query_observability_event_dispatcher_lambda" {
source = "terraform-aws-modules/lambda/aws"
version = "7.15.0"
function_name = "${var.resource_prefix}-event-dispatcher"
description = "It receives an event from Event Bridge and it dispatches to Firehose Delivery Stream"
memory_size = 128
timeout = 30
create_package = false
package_type = "Image"
# when using arm64 be sure to use the right image in the Dockerfile
architectures = ["arm64"]
attach_policy_statements = true
environment_variables = {
FIREHOSE_DELIVERY_STREAM = aws_kinesis_firehose_delivery_stream.athena_observability_iceberg_delivery.name
}
policy_statements = {
athena = {
effect = "Allow",
actions = [
"athena:GetQueryExecution",
"athena:GetQueryExecutions"
],
resources = [
"*"
]
}
firehose = {
effect = "Allow",
actions = [
"firehose:PutRecord",
"firehose:PutRecordBatch"
],
resources = [
aws_kinesis_firehose_delivery_stream.athena_observability_iceberg_delivery.arn
]
}
}
cloudwatch_logs_retention_in_days = var.lambda_event_dispatcher_log_group_retention_in_days
image_uri = module.athena_query_observability_lambda_event_dispatcher_builder.image_uri
}
resource "aws_lambda_permission" "athena_query_observability_event_dispatcher_lambda_permission" {
action = "lambda:InvokeFunction"
function_name = module.athena_query_observability_event_dispatcher_lambda.lambda_function_arn
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.athena_query_observability_lambda_event_bridge_rule.arn
}
resource "aws_cloudwatch_event_target" "athena_query_observability_event_lambda_target" {
rule = aws_cloudwatch_event_rule.athena_query_observability_lambda_event_bridge_rule.name
arn = module.athena_query_observability_event_dispatcher_lambda.lambda_function_arn
retry_policy {
maximum_event_age_in_seconds = var.event_bridge_retry_policy_maximum_event_age_in_seconds
maximum_retry_attempts = var.event_bridge_retry_policy_maximum_retry_attempts
}
}