generated from snowplow-devops/terraform-module-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.tf
338 lines (283 loc) · 9.33 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
locals {
module_name = "s3-loader-kinesis-ec2"
module_version = "0.5.0"
app_name = "s3-loader"
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
)
s3_object_prefix = trimsuffix(var.s3_object_prefix, "/")
cloudwatch_log_group_name = "/aws/ec2/${var.name}"
}
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 S3 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
policy = jsonencode({
Version = "2012-10-17",
Statement = concat(
local.private_ecr_registry_statement_final,
[
{
Effect = "Allow",
Action = [
"kinesis:DescribeStream",
"kinesis:DescribeStreamSummary",
"kinesis:List*"
],
Resource = [
"arn:${local.iam_partition}:kinesis:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:stream/${var.in_stream_name}",
"arn:${local.iam_partition}:kinesis:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:stream/${var.bad_stream_name}"
]
},
{
Effect = "Allow",
Action = [
"kinesis:Get*"
],
Resource = [
"arn:${local.iam_partition}:kinesis:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:stream/${var.in_stream_name}"
]
},
{
Effect = "Allow",
Action = [
"kinesis:Put*"
],
Resource = [
"arn:${local.iam_partition}:kinesis:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:stream/${var.bad_stream_name}"
]
},
{
Effect = "Allow",
Action = [
"s3:PutObject"
],
Resource = [
"arn:${local.iam_partition}:s3:::${var.s3_bucket_name}/${local.s3_object_prefix}/*"
]
},
{
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}:*"
]
}
]
)
})
}
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 {
hocon = templatefile("${path.module}/templates/config.hocon.tmpl", {
app_name = var.name
in_stream_name = var.in_stream_name
out_stream_name = var.bad_stream_name
region = data.aws_region.current.name
purpose = var.purpose
s3_bucket_path = "${var.s3_bucket_name}/${local.s3_object_prefix}"
s3_format = var.s3_format
initial_position = var.initial_position
byte_limit = var.byte_limit
record_limit = var.record_limit
time_limit_ms = var.time_limit_ms
partition_format = var.partition_format
})
user_data = templatefile("${path.module}/templates/user-data.sh.tmpl", {
accept_limited_use_license = var.accept_limited_use_license
config_b64 = base64encode(local.hocon)
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
# Used to determine image tag to pull down
s3_format = var.s3_format
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 = var.min_size
max_size = var.max_size
subnet_ids = var.subnet_ids
enable_auto_scaling = var.enable_auto_scaling
scale_up_cooldown_sec = var.scale_up_cooldown_sec
scale_up_cpu_threshold_percentage = var.scale_up_cpu_threshold_percentage
scale_up_eval_minutes = var.scale_up_eval_minutes
scale_down_cooldown_sec = var.scale_down_cooldown_sec
scale_down_cpu_threshold_percentage = var.scale_down_cpu_threshold_percentage
scale_down_eval_minutes = var.scale_down_eval_minutes
}