-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.tf
444 lines (405 loc) · 12.6 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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#################
# Data and Local Variables
#################
data "aws_region" "this" {
}
data "aws_caller_identity" "this" {
}
data "aws_vpc" "this" {
id = var.vpc_id
}
data "aws_subnet" "private" {
count = length(var.private_subnet_ids)
id = var.private_subnet_ids[count.index]
}
data "aws_kms_key" "ebs" {
key_id = var.ebs_kms_key_id
}
data "aws_ami" "amzn2" {
count = local.is_image_id_provided ? 0 : 1
name_regex = "amzn2-ami-kernel-.*-hvm-2\\.0\\.2022.*"
owners = ["amazon"]
filter {
name = "architecture"
values = ["x86_64"]
}
filter {
name = "description"
values = ["Amazon Linux 2 Kernel * AMI 2.0.2022* x86_64 HVM gp2"]
}
filter {
name = "ena-support"
values = ["true"]
}
filter {
name = "root-device-type"
values = ["ebs"]
}
filter {
name = "block-device-mapping.volume-type"
values = ["gp2"]
}
filter {
name = "state"
values = ["available"]
}
most_recent = true
}
locals {
is_admin_password_systems_manager = length(regexall("^parameter/.*", var.admin_password)) > 0
is_admin_password_secrets_manager = length(regexall("^secrets/.*", var.admin_password)) > 0
is_hosted_zone_provided = length(var.hosted_zone_id) > 0
is_image_id_provided = length(var.image_id) > 0
instance_type_support_recovery = contains(
["a1", "c3", "c4", "c5", "c5n", "m3", "m4", "m5", "m5a", "m5n", "p3", "r3", "r4", "r5", "r5a", "r5n", "t2", "t3", "t3a", "x1", "x1e"],
split(".", var.instance_type)[0]
)
prefix = length(trimspace(var.prefix)) > 0 ? format("%s-", trimspace(var.prefix)) : ""
storage_ebs_flag = var.storage_type == "ebs" ? 1 : 0
}
#################
# Security Groups
#################
resource "aws_security_group" "private" {
name_prefix = "${local.prefix}influxdb-private-"
vpc_id = var.vpc_id
description = "Security group for InfluxDB"
tags = merge(var.tags, {Name: "${local.prefix}influxdb-private"})
}
resource "aws_security_group_rule" "egress-all" {
from_port = 0
protocol = "all"
security_group_id = aws_security_group.private.id
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
to_port = 65535
type = "egress"
}
resource "aws_security_group_rule" "ingress-ssh" {
type = "ingress"
security_group_id = aws_security_group.private.id
cidr_blocks = [
data.aws_vpc.this.cidr_block
]
from_port = "22"
to_port = "22"
protocol = "tcp"
}
resource "aws_security_group_rule" "ingress-api" {
description = "API"
type = "ingress"
security_group_id = aws_security_group.private.id
cidr_blocks = [
data.aws_vpc.this.cidr_block
]
from_port = 8086
to_port = 8086
protocol = "tcp"
}
resource "aws_security_group_rule" "ingress-admin" {
description = "RPC Admin"
type = "ingress"
security_group_id = aws_security_group.private.id
cidr_blocks = [
data.aws_vpc.this.cidr_block
]
from_port = 8088
to_port = 8088
protocol = "tcp"
}
#################
# S3 Bucket
#################
resource "random_id" "s3" {
byte_length = 16
}
resource "aws_s3_bucket" "source" {
acl = "private"
bucket = substr("${var.prefix}-influxdb-oss-${data.aws_region.this.name}-${random_id.s3.hex}", 0, 63)
region = data.aws_region.this.name
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "aws:kms"
}
}
}
tags = var.tags
}
resource "aws_s3_bucket_public_access_block" "source" {
bucket = aws_s3_bucket.source.id
block_public_acls = false
block_public_policy = false
}
data "archive_file" "setup" {
type = "zip"
output_path = "${path.module}/setup.zip"
source_dir = "${path.module}/ansible"
}
resource "aws_s3_bucket_object" "setup" {
acl = "private"
bucket = aws_s3_bucket.source.bucket
content_type = "application/zip"
etag = data.archive_file.setup.output_md5
key = "setup.zip"
source = data.archive_file.setup.output_path
storage_class = "ONEZONE_IA"
tags = var.tags
}
#################
# IAM
#################
data "aws_iam_policy_document" "assume" {
statement {
actions = [
"sts:AssumeRole"
]
effect = "Allow"
principals {
identifiers = [
"ec2.amazonaws.com"
]
type = "Service"
}
}
}
resource "aws_iam_role" "node" {
assume_role_policy = data.aws_iam_policy_document.assume.json
name_prefix = "${local.prefix}influxdb-"
tags = merge(var.tags, {Name: "${local.prefix}influxdb"})
}
resource "aws_iam_instance_profile" "node" {
name_prefix = "${local.prefix}influxdb-"
role = aws_iam_role.node.id
}
data "aws_iam_policy_document" "s3" {
statement {
actions = [
"s3:Get*"
]
effect = "Allow"
resources = [
"${aws_s3_bucket.source.arn}/*"
]
}
}
data "aws_iam_policy_document" "system-manager" {
count = local.is_admin_password_systems_manager ? 1 : 0
statement {
actions = [
"ssm:GetParameter"
]
effect = "Allow"
resources = [
"arn:aws:ssm:${data.aws_region.this.name}:${data.aws_caller_identity.this.account_id}:${var.admin_password}"
]
}
}
data "aws_iam_policy_document" "secrets-manager" {
count = local.is_admin_password_secrets_manager ? 1 : 0
statement {
actions = [
"secretsmanager:GetSecretValue"
]
effect = "Allow"
resources = [
"arn:aws:secretsmanager:${data.aws_region.this.name}:${data.aws_caller_identity.this.account_id}:secret:${regex("secrets/(.*)", var.admin_password)[0]}"
]
}
}
resource "aws_iam_role_policy" "s3" {
name_prefix = "s3-"
policy = data.aws_iam_policy_document.s3.json
role = aws_iam_role.node.id
}
resource "aws_iam_role_policy" "ssm" {
count = local.is_admin_password_systems_manager ? 1 : 0
name_prefix = "system-manager-"
policy = data.aws_iam_policy_document.system-manager[0].json
role = aws_iam_role.node.id
}
resource "aws_iam_role_policy" "secrets-manager" {
count = local.is_admin_password_secrets_manager ? 1 : 0
name_prefix = "secrets-manager-"
policy = data.aws_iam_policy_document.secrets-manager[0].json
role = aws_iam_role.node.id
}
resource "aws_iam_role_policy_attachment" "ssm" {
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
role = aws_iam_role.node.id
}
#################
# EBS Storage
#################
resource "aws_ebs_volume" "data" {
count = var.nodes * local.storage_ebs_flag
availability_zone = data.aws_subnet.private[count.index % length(data.aws_subnet.private)].availability_zone
encrypted = true
iops = var.data_storage_volume_type == "io1" ? var.data_storage_volume_iops : null
kms_key_id = data.aws_kms_key.ebs.arn
size = var.data_storage_volume_size
type = var.data_storage_volume_type
tags = merge(var.tags, {Name: "${local.prefix}influxdb-${format("%02d", count.index + 1)}-data"})
lifecycle {
ignore_changes = [
encrypted,
kms_key_id,
snapshot_id,
type
]
}
}
resource "aws_ebs_volume" "wal" {
count = var.nodes * local.storage_ebs_flag
availability_zone = data.aws_subnet.private[count.index % length(data.aws_subnet.private)].availability_zone
encrypted = true
iops = var.wal_storage_volume_type == "io1" ? var.wal_storage_volume_iops : null
kms_key_id = data.aws_kms_key.ebs.arn
size = var.wal_storage_volume_size
type = var.wal_storage_volume_type
tags = merge(var.tags, {Name: "${local.prefix}influxdb-${format("%02d", count.index + 1)}-wal"})
lifecycle {
ignore_changes = [
encrypted,
kms_key_id,
snapshot_id,
type
]
}
}
#################
# EC2 Instance
#################
data "template_file" "user_data" {
count = local.is_image_id_provided ? 0 : 1
template = file("${path.module}/files/init.sh")
vars = {
admin_username = var.admin_username
admin_password = var.admin_password
storage_type = var.storage_type
region = data.aws_region.this.name
flux_enabled = var.flux_enabled
setup_dist = "s3://${aws_s3_bucket_object.setup.bucket}/${aws_s3_bucket_object.setup.key}"
setup_dist_etag = aws_s3_bucket_object.setup.etag
}
}
resource "aws_instance" "node" {
count = var.nodes
ami = local.is_image_id_provided ? var.image_id : data.aws_ami.amzn2[0].id
iam_instance_profile = aws_iam_instance_profile.node.id
instance_type = var.instance_type
lifecycle {
create_before_destroy = true
}
ebs_optimized = var.ec2_ebs_optimized
credit_specification {
cpu_credits = var.ec2_cpu_credits
}
key_name = var.key_pair_name
root_block_device {
volume_size = var.root_volume_size
encrypted = true
}
subnet_id = data.aws_subnet.private[count.index % length(data.aws_subnet.private)].id
tags = merge(var.tags, {Name: "${local.prefix}influxdb-${format("%02d", count.index + 1)}"})
user_data = local.is_image_id_provided ? var.ec2_user_data : data.template_file.user_data[0].rendered
vpc_security_group_ids = [
aws_security_group.private.id
]
}
#################
# Static Network Interface
#################
resource "aws_network_interface" "static" {
count = var.nodes
security_groups = [
aws_security_group.private.id
]
subnet_id = data.aws_subnet.private[count.index % length(data.aws_subnet.private)].id
tags = merge(var.tags, {Name: "${local.prefix}influxdb-${format("%02d", count.index + 1)}"})
}
resource "aws_network_interface_attachment" "static" {
count = var.nodes
device_index = 1
instance_id = aws_instance.node[count.index].id
network_interface_id = aws_network_interface.static[count.index].id
}
#################
# EBS Storage Attachments
#################
resource "aws_volume_attachment" "data" {
count = var.nodes * local.storage_ebs_flag
device_name = "/dev/sdf"
instance_id = aws_instance.node[count.index].id
volume_id = aws_ebs_volume.data[count.index].id
force_detach = true
}
resource "aws_volume_attachment" "wal" {
count = var.nodes * local.storage_ebs_flag
device_name = "/dev/sdg"
instance_id = aws_instance.node[count.index].id
volume_id = aws_ebs_volume.wal[count.index].id
force_detach = true
}
#################
# Recovery
#################
resource "aws_cloudwatch_metric_alarm" "reboot" {
count = local.instance_type_support_recovery ? var.nodes : 0
alarm_actions = [
"arn:aws:automate:${data.aws_region.this.name}:ec2:reboot"
]
alarm_description = "Reboot Linux instance when Instance status check failed for 5 minutes"
alarm_name = "${local.prefix}influxdb-${format("%02d", count.index + 1)}-reboot"
comparison_operator = "GreaterThanOrEqualToThreshold"
datapoints_to_alarm = 5
evaluation_periods = 5
threshold = 1
metric_name = "StatusCheckFailed_Instance"
namespace = "AWS/EC2"
period = 60
statistic = "Maximum"
dimensions = {
InstanceId = aws_instance.node[count.index].id
}
tags = merge(var.tags, {Name: "${local.prefix}influxdb-${format("%02d", count.index + 1)}-reboot"})
lifecycle {
create_before_destroy = true
}
}
resource "aws_cloudwatch_metric_alarm" "recovery" {
count = local.instance_type_support_recovery ? var.nodes : 0
alarm_actions = [
"arn:aws:automate:${data.aws_region.this.name}:ec2:recover"
]
alarm_description = "Recover Linux instance when System status check failed for 10 minutes"
alarm_name = "${local.prefix}influxdb-${format("%02d", count.index + 1)}-recovery"
comparison_operator = "GreaterThanOrEqualToThreshold"
datapoints_to_alarm = 10
evaluation_periods = 10
threshold = 1
metric_name = "StatusCheckFailed_System"
namespace = "AWS/EC2"
period = 60
statistic = "Maximum"
dimensions = {
InstanceId = aws_instance.node[count.index].id
}
tags = merge(var.tags, {Name: "${local.prefix}influxdb-${format("%02d", count.index + 1)}-recovery"})
lifecycle {
create_before_destroy = true
}
}
#################
# Routing
#################
resource "aws_route53_record" "alias" {
count = local.is_hosted_zone_provided ? var.nodes : 0
name = "${local.prefix}influxdb-${format("%02d", count.index + 1)}"
records = [
aws_network_interface.static[count.index].private_ip
]
ttl = 3600
type = "A"
zone_id = var.hosted_zone_id
}