-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.tf
428 lines (368 loc) · 11.7 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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
locals {
module_name = "transformer-kinesis-ec2"
module_version = "0.4.1"
app_name = "transformer-kinesis"
app_version = var.app_version
local_tags = {
Name = var.name
app_name = local.app_name
app_version = local.app_version
module_name = local.module_name
module_version = local.module_version
}
tags = merge(
var.tags,
local.local_tags
)
cloudwatch_log_group_name = "/aws/ec2/${var.name}"
sqs_enabled = var.sqs_queue_name != ""
iam_queue_statement = local.sqs_enabled ? [
{
Effect = "Allow",
Action = [
"sqs:DeleteMessage",
"sqs:GetQueueUrl",
"sqs:ListQueues",
"sqs:ChangeMessageVisibility",
"sqs:SendMessageBatch",
"sqs:ReceiveMessage",
"sqs:SendMessage",
"sqs:DeleteMessageBatch",
"sqs:ChangeMessageVisibilityBatch"
],
Resource = [
"arn:${local.iam_partition}:sqs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:${var.sqs_queue_name}"
]
}
] : [
{
Effect = "Allow",
Action = [
"sns:Publish"
],
Resource = [
var.sns_topic_arn
]
}
]
s3_path = "${var.s3_bucket_name}/${var.s3_bucket_object_prefix}"
}
data "aws_region" "current" {}
data "aws_caller_identity" "current" {}
locals {
is_aws_global = replace(data.aws_region.current.name, "cn-", "") == data.aws_region.current.name
iam_partition = local.is_aws_global ? "aws" : "aws-cn"
is_private_ecr_registry = var.private_ecr_registry != ""
private_ecr_registry_statement = [{
Action = [
"ecr:GetAuthorizationToken",
"ecr:BatchGetImage",
"ecr:GetDownloadUrlForLayer"
]
Effect = "Allow"
Resource = [
"*"
]
}]
private_ecr_registry_statement_final = local.is_private_ecr_registry ? local.private_ecr_registry_statement : []
}
module "telemetry" {
source = "snowplow-devops/telemetry/snowplow"
version = "0.5.0"
count = var.telemetry_enabled ? 1 : 0
user_provided_id = var.user_provided_id
cloud = "AWS"
region = data.aws_region.current.name
app_name = local.app_name
app_version = local.app_version
module_name = local.module_name
module_version = local.module_version
}
# --- DynamoDB: KCL Table
resource "aws_dynamodb_table" "kcl" {
name = var.name
hash_key = "leaseKey"
write_capacity = 1
read_capacity = 1
attribute {
name = "leaseKey"
type = "S"
}
lifecycle {
ignore_changes = [write_capacity, read_capacity]
}
tags = local.tags
}
module "kcl_autoscaling" {
source = "snowplow-devops/dynamodb-autoscaling/aws"
version = "0.2.0"
table_name = aws_dynamodb_table.kcl.id
read_min_capacity = var.kcl_read_min_capacity
read_max_capacity = var.kcl_read_max_capacity
write_min_capacity = var.kcl_write_min_capacity
write_max_capacity = var.kcl_write_max_capacity
}
# --- CloudWatch: Logging
resource "aws_cloudwatch_log_group" "log_group" {
count = var.cloudwatch_logs_enabled ? 1 : 0
name = local.cloudwatch_log_group_name
retention_in_days = var.cloudwatch_logs_retention_days
tags = local.tags
}
# --- IAM: Roles & Permissions
resource "aws_iam_role" "iam_role" {
name = var.name
description = "Allows the Transformer nodes to access required services"
tags = local.tags
assume_role_policy = <<EOF
{
"Version" : "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "Service": [ "ec2.amazonaws.com" ]},
"Action": [ "sts:AssumeRole" ]
}
]
}
EOF
permissions_boundary = var.iam_permissions_boundary
}
resource "aws_iam_policy" "iam_policy" {
name = var.name
policy = jsonencode({
Version = "2012-10-17",
Statement = concat(
local.iam_queue_statement,
local.private_ecr_registry_statement_final,
[
{
Effect = "Allow",
Action = [
"kinesis:DescribeStream",
"kinesis:DescribeStreamSummary",
"kinesis:RegisterStreamConsumer",
"kinesis:List*",
"kinesis:Get*"
],
Resource = [
"arn:${local.iam_partition}:kinesis:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:stream/${var.stream_name}"
]
},
{
Effect = "Allow",
Action = [
"kinesis:DescribeStreamConsumer",
"kinesis:SubscribeToShard"
],
Resource = [
"arn:${local.iam_partition}:kinesis:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:stream/${var.stream_name}/consumer/*"
]
},
{
Effect = "Allow",
Action = [
"dynamodb:BatchWriteItem",
"dynamodb:PutItem",
"dynamodb:DescribeTable",
"dynamodb:DeleteItem",
"dynamodb:GetItem",
"dynamodb:Scan",
"dynamodb:UpdateItem"
],
Resource = [
"${aws_dynamodb_table.kcl.arn}"
]
},
{
Effect = "Allow",
Action = [
"logs:PutLogEvents",
"logs:CreateLogStream",
"logs:DescribeLogStreams"
],
Resource = [
"arn:${local.iam_partition}:logs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:log-group:${local.cloudwatch_log_group_name}:*"
]
},
{
Effect = "Allow",
Action = [
"cloudwatch:ListMetrics",
"cloudwatch:PutMetricData"
],
Resource = "*"
},
{
Effect = "Allow",
Action = [
"s3:ListBucket"
],
Resource = [
"arn:${local.iam_partition}:s3:::${var.s3_bucket_name}"
]
},
{
Effect = "Allow",
Action = [
"s3:GetObject",
"s3:GetObjectVersion",
"s3:PutObject",
"s3:Delete*"
],
Resource = [
"arn:${local.iam_partition}:s3:::${local.s3_path}",
"arn:${local.iam_partition}:s3:::${local.s3_path}/*"
]
}
]
)
})
}
resource "aws_iam_role_policy_attachment" "policy_attachment" {
role = aws_iam_role.iam_role.name
policy_arn = aws_iam_policy.iam_policy.arn
}
resource "aws_iam_instance_profile" "instance_profile" {
name = var.name
role = aws_iam_role.iam_role.name
}
# --- EC2: Security Group Rules
resource "aws_security_group" "sg" {
name = var.name
vpc_id = var.vpc_id
tags = local.tags
}
resource "aws_security_group_rule" "ingress_tcp_22" {
type = "ingress"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = var.ssh_ip_allowlist
security_group_id = aws_security_group.sg.id
}
resource "aws_security_group_rule" "egress_tcp_80" {
type = "egress"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.sg.id
}
resource "aws_security_group_rule" "egress_tcp_443" {
type = "egress"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.sg.id
}
# Needed for clock synchronization
resource "aws_security_group_rule" "egress_udp_123" {
type = "egress"
from_port = 123
to_port = 123
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.sg.id
}
# --- EC2: Auto-scaling group & Launch Configurations
module "instance_type_metrics" {
source = "snowplow-devops/ec2-instance-type-metrics/aws"
version = "0.1.2"
instance_type = var.instance_type
}
locals {
resolvers_raw = concat(var.default_iglu_resolvers, var.custom_iglu_resolvers)
resolvers_open = [
for resolver in local.resolvers_raw : merge(
{
name = resolver["name"],
priority = resolver["priority"],
vendorPrefixes = resolver["vendor_prefixes"],
connection = {
http = {
uri = resolver["uri"]
}
}
}
) if resolver["api_key"] == ""
]
resolvers_closed = [
for resolver in local.resolvers_raw : merge(
{
name = resolver["name"],
priority = resolver["priority"],
vendorPrefixes = resolver["vendor_prefixes"],
connection = {
http = {
uri = resolver["uri"]
apikey = resolver["api_key"]
}
}
}
) if resolver["api_key"] != ""
]
resolvers = flatten([
local.resolvers_open,
local.resolvers_closed
])
iglu_resolver = templatefile("${path.module}/templates/iglu_resolver.json.tmpl", { resolvers = jsonencode(local.resolvers) })
config = templatefile("${path.module}/templates/config.json.tmpl", {
app_name = var.name
stream_name = var.stream_name
region = data.aws_region.current.name
initial_position = var.initial_position
transformed_output = local.s3_path
compression = var.transformer_compression
window_period = "${var.window_period_min} minutes"
sqs_enabled = local.sqs_enabled
sqs_queue_name = var.sqs_queue_name
sns_topic_arn = var.sns_topic_arn
transformation_type = var.transformation_type
default_shred_format = var.default_shred_format
schemas_json = jsonencode(var.schemas_json)
schemas_tsv = jsonencode(var.schemas_tsv)
schemas_skip = jsonencode(var.schemas_skip)
widerow_file_format = var.widerow_file_format
telemetry_disable = !var.telemetry_enabled
telemetry_collector_uri = join("", module.telemetry.*.collector_uri)
telemetry_collector_port = 443
telemetry_secure = true
telemetry_user_provided_id = var.user_provided_id
telemetry_auto_gen_id = join("", module.telemetry.*.auto_generated_id)
telemetry_module_name = local.module_name
telemetry_module_version = local.module_version
})
user_data = templatefile("${path.module}/templates/user-data.sh.tmpl", {
accept_limited_use_license = var.accept_limited_use_license
config_b64 = var.config_override_b64 == "" ? base64encode(local.config) : var.config_override_b64
iglu_resolver_b64 = base64encode(local.iglu_resolver)
version = local.app_version
telemetry_script = join("", module.telemetry.*.amazon_linux_2_user_data)
cloudwatch_logs_enabled = var.cloudwatch_logs_enabled
cloudwatch_log_group_name = local.cloudwatch_log_group_name
container_memory = "${module.instance_type_metrics.memory_application_mb}m"
java_opts = var.java_opts
is_private_ecr_registry = local.is_private_ecr_registry
private_ecr_registry = var.private_ecr_registry
region = data.aws_region.current.name
})
}
module "service" {
source = "snowplow-devops/service-ec2/aws"
version = "0.2.1"
user_supplied_script = local.user_data
name = var.name
tags = local.tags
amazon_linux_2_ami_id = var.amazon_linux_2_ami_id
instance_type = var.instance_type
ssh_key_name = var.ssh_key_name
iam_instance_profile_name = aws_iam_instance_profile.instance_profile.name
associate_public_ip_address = var.associate_public_ip_address
security_groups = [aws_security_group.sg.id]
min_size = 1
max_size = 1
subnet_ids = var.subnet_ids
enable_auto_scaling = false
}