-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.tf
277 lines (250 loc) · 9.27 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
locals {
ec2_prefix = lookup(var.custom_prefix, "ec2", var.prefix)
ssh_path = "/tmp/${local.ec2_prefix}-${var.cluster_name}"
nics = var.install_cluster_dpdk ? var.containers_config_map[var.instance_type].nics : 1
public_ssh_key = var.enable_key_pair && var.ssh_public_key == null ? tls_private_key.key[0].public_key_openssh : var.ssh_public_key
tags_dest = ["instance", "network-interface", "volume"]
weka_volume_size = var.backends_weka_volume_size + 10 * (local.nics - 1)
user_data = templatefile("${path.module}/user_data.sh", {
region = local.region
proxy = var.proxy_url
subnet_id = local.subnet_ids[0]
groups = join(" ", local.sg_ids)
nics_num = local.nics
deploy_lambda_name = aws_lambda_function.deploy_lambda.function_name
weka_log_group_name = "/wekaio/${local.ec2_prefix}-${var.cluster_name}"
custom_data = var.custom_data
})
backends_placement_group_name = var.use_placement_group ? var.placement_group_name == null ? aws_placement_group.placement_group[0].name : var.placement_group_name : null
create_kms_key = var.ebs_encrypted && var.ebs_kms_key_id == null
kms_key_id = local.create_kms_key ? aws_kms_key.kms_key[0].arn : var.ebs_kms_key_id
kms_prefix = lookup(var.custom_prefix, "kms", var.prefix)
iam_prefix = lookup(var.custom_prefix, "iam", var.prefix)
root_device_name = var.ami_id != null ? data.aws_ami.provided_ami[0].root_device_name : data.aws_ami.amzn_ami[0].root_device_name
}
data "aws_caller_identity" "current" {}
data "aws_subnet" "this" {
count = length(local.subnet_ids)
id = local.subnet_ids[count.index]
depends_on = [module.network]
}
data "aws_ami" "amzn_ami" {
count = var.ami_id == null ? 1 : 0
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["amzn2-ami-kernel-5.*-x86_64-gp2"]
}
}
data "aws_ami" "provided_ami" {
count = var.ami_id != null ? 1 : 0
filter {
name = "image-id"
values = [var.ami_id]
}
}
# =============== ssh key ===================
resource "tls_private_key" "key" {
count = var.enable_key_pair && var.ssh_public_key == null ? 1 : 0
algorithm = "RSA"
rsa_bits = 4096
}
resource "aws_key_pair" "generated_key" {
count = var.enable_key_pair && var.key_pair_name == null ? 1 : 0
key_name = "${local.ec2_prefix}-${var.cluster_name}-ssh-key"
public_key = local.public_ssh_key
tags = var.tags_map
}
resource "local_file" "public_key" {
count = var.enable_key_pair && var.key_pair_name == null && var.ssh_public_key == null ? 1 : 0
content = tls_private_key.key[count.index].public_key_openssh
filename = "${local.ssh_path}-public-key.pub"
file_permission = "0600"
}
resource "local_file" "private_key" {
count = var.enable_key_pair && var.key_pair_name == null && var.ssh_public_key == null ? 1 : 0
content = tls_private_key.key[count.index].private_key_pem
filename = "${local.ssh_path}-private-key.pem"
file_permission = "0600"
}
resource "aws_placement_group" "placement_group" {
count = var.use_placement_group && var.placement_group_name == null ? 1 : 0
name = "${local.ec2_prefix}-${var.cluster_name}-placement-group"
strategy = "cluster"
tags = merge(var.tags_map, {
CreationDate = timestamp()
})
depends_on = [module.network]
}
resource "aws_kms_key" "kms_key" {
count = local.create_kms_key ? 1 : 0
enable_key_rotation = true
deletion_window_in_days = 20
is_enabled = true
tags = merge(var.tags_map, {
Name = "${local.kms_prefix}-${var.cluster_name}"
})
}
resource "aws_kms_alias" "kms_alias" {
count = local.create_kms_key ? 1 : 0
name = "alias/${local.kms_prefix}-${var.cluster_name}"
target_key_id = aws_kms_key.kms_key[0].key_id
depends_on = [aws_kms_key.kms_key]
}
resource "aws_kms_key_policy" "kms_key_policy" {
count = local.create_kms_key ? 1 : 0
key_id = aws_kms_key.kms_key[0].id
policy = jsonencode({
Version = "2012-10-17"
Id = "key-default-1"
Statement = [
{
"Sid" : "Enable IAM User Permissions",
"Effect" : "Allow",
"Principal" : {
"AWS" : "arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"
},
"Action" : "kms:*",
"Resource" : "*"
},
{
Sid = "Allow service-linked role use of the customer managed key",
Effect = "Allow",
Principal = {
AWS = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling"
},
Action = [
"kms:Encrypt",
"kms:Decrypt",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:DescribeKey"
],
Resource = "*"
},
{
Sid = "Allow service-linked role use of the customer managed key",
Effect = "Allow",
Principal = {
AWS = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling"
},
Action = [
"kms:CreateGrant"
],
Resource = "*"
Condition = {
Bool = {
"kms:GrantIsForAWSResource" : true
}
}
}
]
})
depends_on = [aws_kms_key.kms_key]
}
resource "aws_launch_template" "launch_template" {
name_prefix = "${local.ec2_prefix}-${var.cluster_name}-backend"
disable_api_termination = true
disable_api_stop = true
ebs_optimized = true
image_id = var.ami_id != null ? var.ami_id : data.aws_ami.amzn_ami[0].id
instance_initiated_shutdown_behavior = "terminate"
instance_type = var.instance_type
key_name = var.enable_key_pair ? var.key_pair_name != null ? var.key_pair_name : aws_key_pair.generated_key[0].key_name : null
update_default_version = true
block_device_mappings {
device_name = "/dev/sdp"
ebs {
volume_size = local.weka_volume_size
volume_type = "gp3"
delete_on_termination = true
kms_key_id = local.kms_key_id
encrypted = var.ebs_encrypted
}
}
block_device_mappings {
device_name = local.root_device_name
ebs {
volume_size = var.backends_root_volume_size
encrypted = var.ebs_encrypted
kms_key_id = local.kms_key_id
}
}
credit_specification {
cpu_credits = "standard"
}
iam_instance_profile {
arn = local.instance_iam_profile_arn
}
metadata_options {
http_endpoint = "enabled"
http_tokens = var.metadata_http_tokens
http_put_response_hop_limit = 1
instance_metadata_tags = "enabled"
}
monitoring {
enabled = true
}
network_interfaces {
associate_public_ip_address = local.assign_public_ip
delete_on_termination = true
device_index = 0
security_groups = local.sg_ids
subnet_id = local.subnet_ids[0]
}
placement {
availability_zone = data.aws_subnet.this[0].availability_zone
group_name = local.backends_placement_group_name
}
capacity_reservation_specification {
capacity_reservation_target {
capacity_reservation_id = var.capacity_reservation_id
}
}
dynamic "tag_specifications" {
for_each = local.tags_dest
content {
resource_type = tag_specifications.value
tags = merge(var.tags_map, {
Name = "${local.ec2_prefix}-${var.cluster_name}-${tag_specifications.value}-backend"
weka_cluster_name = var.cluster_name
weka_hostgroup_type = "backend"
user = data.aws_caller_identity.current.user_id
})
}
}
user_data = base64encode(local.user_data)
depends_on = [aws_placement_group.placement_group, aws_kms_key.kms_key]
}
resource "aws_autoscaling_group" "autoscaling_group" {
name = "${local.ec2_prefix}-${var.cluster_name}-autoscaling-group"
#availability_zones = [ for z in var.availability_zones: format("%s%s", local.region,z) ]
desired_capacity = var.cluster_size
max_size = var.cluster_size * 7
min_size = var.cluster_size
vpc_zone_identifier = [local.subnet_ids[0]]
suspended_processes = ["ReplaceUnhealthy"]
protect_from_scale_in = true
launch_template {
id = aws_launch_template.launch_template.id
version = aws_launch_template.launch_template.latest_version
}
tag {
key = "${local.ec2_prefix}-${var.cluster_name}-asg"
propagate_at_launch = true
value = var.cluster_name
}
dynamic "tag" {
for_each = var.tags_map
content {
key = tag.key
value = tag.value
propagate_at_launch = false # already propagated by launch template
}
}
lifecycle {
ignore_changes = [desired_capacity, min_size, max_size]
}
depends_on = [aws_launch_template.launch_template, aws_placement_group.placement_group]
}