-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
401 lines (347 loc) · 12.1 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
locals {
module_name = "redshift-loader-ec2"
module_version = "0.1.2"
app_name = "rdb-loader-redshift"
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}"
}
data "aws_region" "current" {}
data "aws_caller_identity" "current" {}
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
}
# --- 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 Redshift Loader 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
tags = local.tags
policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Effect = "Allow",
Action = [
"s3:ListBucket",
"s3:PutObject",
"s3:GetObject"
],
Resource = [
"arn:aws:s3:::${var.redshift_aws_s3_bucket_name}/",
"arn:aws:s3:::${var.redshift_aws_s3_bucket_name}/*"
]
},
{
Effect = "Allow",
Action = ["s3:GetObject"],
Resource = [
"arn:aws:s3:::${var.redshift_aws_s3_bucket_name}/*/shredding_complete.json"
]
},
{
Effect = "Allow",
Action = [
"sqs:DeleteMessage",
"sqs:GetQueueUrl",
"sqs:ListQueues",
"sqs:ChangeMessageVisibility",
"sqs:ReceiveMessage",
"sqs:SendMessage"
],
Resource = [
"arn:aws:sqs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:${var.sqs_queue_name}"
]
},
{
Effect = "Allow",
Action = [
"logs:PutLogEvents",
"logs:CreateLogStream",
"logs:DescribeLogStreams"
],
Resource = [
"arn:aws:logs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:log-group:${local.cloudwatch_log_group_name}:*"
]
},
{
Effect = "Allow",
Action = [
"sts:AssumeRole"
],
Resource = [
aws_iam_role.sts_credentials_role.arn
]
}
]
})
}
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
}
resource "aws_iam_role" "sts_credentials_role" {
name = "${var.name}-sts-credentials"
description = "Allows the Redshift Loader to access the S3 buckets to perform loading"
tags = local.tags
permissions_boundary = var.iam_permissions_boundary
assume_role_policy = data.aws_iam_policy_document.sts_credentials_role.json
}
data "aws_iam_policy_document" "sts_credentials_role" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "AWS"
identifiers = [
aws_iam_role.iam_role.arn
]
}
}
}
resource "aws_iam_policy" "sts_credentials_policy" {
name = "${var.name}-sts-credentials"
policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Effect = "Allow",
Action = [
"s3:ListBucket",
"s3:GetBucketLocation",
"s3:GetObject",
"s3:GetObjectVersion",
],
Resource = [
"arn:aws:s3:::${var.redshift_aws_s3_bucket_name}",
"arn:aws:s3:::${var.redshift_aws_s3_bucket_name}/",
"arn:aws:s3:::${var.redshift_aws_s3_bucket_name}/*"
]
}
]
})
}
resource "aws_iam_role_policy_attachment" "sts_credentials_policy_attachement" {
role = aws_iam_role.sts_credentials_role.name
policy_arn = aws_iam_policy.sts_credentials_policy.arn
}
# --- 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
}
resource "aws_security_group_rule" "egress_tcp_redshift" {
type = "egress"
from_port = var.redshift_port
to_port = var.redshift_port
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
}
# Needed for statsd
resource "aws_security_group_rule" "egress_udp_statsd" {
count = var.statsd_enabled ? 1 : 0
type = "egress"
from_port = var.statsd_port
to_port = var.statsd_port
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", {
region = data.aws_region.current.name
message_queue = var.sqs_queue_name
rs_host = var.redshift_host
rs_database = var.redshift_database
rs_port = var.redshift_port
rs_schema = var.redshift_schema
rs_username = var.redshift_loader_user
rs_password = var.redshift_password
rs_max_error = var.redshift_max_error
rs_jsonpaths_bucket = var.redshift_jsonpaths_bucket
temp_credentials_role_arn = aws_iam_role.sts_credentials_role.arn
sp_tracking_enabled = var.sp_tracking_enabled
sp_tracking_app_id = var.sp_tracking_app_id
sp_tracking_collector_url = var.sp_tracking_collector_url
sentry_enabled = var.sentry_enabled
sentry_dsn = var.sentry_dsn
statsd_enabled = var.statsd_enabled
statsd_host = var.statsd_host
statsd_port = var.statsd_port
stdout_metrics_enabled = var.stdout_metrics_enabled
webhook_enabled = var.webhook_enabled
webhook_collector = var.webhook_collector
folder_monitoring_enabled = var.folder_monitoring_enabled
folder_monitoring_staging = var.redshift_aws_s3_folder_monitoring_stage_url
folder_monitoring_transformer_output = var.redshift_aws_s3_folder_monitoring_transformer_output_stage_url
folder_monitoring_period = var.folder_monitoring_period
folder_monitoring_since = var.folder_monitoring_since
folder_monitoring_until = var.folder_monitoring_until
health_check_enabled = var.health_check_enabled
health_check_freq = var.health_check_freq
health_check_timeout = var.health_check_timeout
retry_queue_enabled = var.retry_queue_enabled
retry_period = var.retry_period
retry_queue_size = var.retry_queue_size
retry_queue_max_attempt = var.retry_queue_max_attempt
retry_queue_interval = var.retry_queue_interval
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", {
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
})
}
module "service" {
source = "snowplow-devops/service-ec2/aws"
version = "0.2.0"
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
}