-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlambdas.tf
426 lines (411 loc) · 17.4 KB
/
lambdas.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
locals {
handler_name = "bootstrap"
source_dir = "${path.module}/lambdas"
s3_bucket = var.lambdas_custom_s3_bucket != null ? var.lambdas_custom_s3_bucket : "weka-tf-aws-releases-${local.region}"
s3_key = var.lambdas_custom_s3_key != null ? var.lambdas_custom_s3_key : "${var.lambdas_dist}/${var.lambdas_version}.zip"
functions = toset([
"deploy", "clusterize", "report", "clusterize-finalization", "status", "scale-down", "fetch", "terminate",
"transient", "join-nfs-finalization"
])
enable_lambda_vpc = var.enable_lambda_vpc_config ? 1 : 0
obs_prefix = lookup(var.custom_prefix, "obs", var.prefix)
lambda_prefix = lookup(var.custom_prefix, "lambda", var.prefix)
function_name = [for func in local.functions : "${local.lambda_prefix}-${var.cluster_name}-${func}-lambda"]
lambdas_hash = md5(join("", [
for f in fileset(local.source_dir, "**") : filemd5("${local.source_dir}/${f}")
]))
stripe_width_calculated = var.cluster_size - var.protection_level - 1
stripe_width = local.stripe_width_calculated < 16 ? local.stripe_width_calculated : 16
install_weka_url = var.install_weka_url != "" ? var.install_weka_url : "https://[email protected]/dist/v1/install/${var.weka_version}/${var.weka_version}?provider=aws®ion=${data.aws_region.current.name}"
}
resource "aws_cloudwatch_log_group" "lambdas_log_group" {
count = length(local.function_name)
name = "/aws/lambda/${local.cloudwatch_prefix}/${local.function_name[count.index]}"
retention_in_days = 30
tags = var.tags_map
}
resource "aws_lambda_function" "deploy_lambda" {
function_name = "${local.lambda_prefix}-${var.cluster_name}-deploy-lambda"
s3_bucket = local.s3_bucket
s3_key = local.s3_key
handler = local.handler_name
role = local.lambda_iam_role_arn
memory_size = 128
timeout = 20
runtime = "provided.al2"
architectures = ["arm64"]
dynamic "vpc_config" {
for_each = range(0, local.enable_lambda_vpc)
content {
security_group_ids = local.sg_ids
subnet_ids = local.subnet_ids
}
}
environment {
variables = {
LAMBDA = "deploy"
REGION = local.region
TOKEN_ID = var.get_weka_io_token_secret_id != "" ? var.get_weka_io_token_secret_id : aws_secretsmanager_secret.get_weka_io_token[0].id
STATE_TABLE = local.dynamodb_table_name
STATE_TABLE_HASH_KEY = local.dynamodb_hash_key_name
STATE_KEY = local.state_key
NFS_STATE_KEY = local.nfs_state_key
CLUSTER_NAME = var.cluster_name
COMPUTE_MEMORY = var.set_dedicated_fe_container ? var.containers_config_map[var.instance_type].memory[1] : var.containers_config_map[var.instance_type].memory[0]
COMPUTE_CONTAINER_CORES_NUM = var.set_dedicated_fe_container ? var.containers_config_map[var.instance_type].compute : var.containers_config_map[var.instance_type].compute + 1
FRONTEND_CONTAINER_CORES_NUM = var.set_dedicated_fe_container ? var.containers_config_map[var.instance_type].frontend : 0
DRIVE_CONTAINER_CORES_NUM = var.containers_config_map[var.instance_type].drive
INSTALL_URL = local.install_weka_url
INSTALL_DPDK = var.install_cluster_dpdk
NICS_NUM = var.containers_config_map[var.instance_type].nics
CLUSTERIZE_LAMBDA_NAME = aws_lambda_function.clusterize_lambda.function_name
REPORT_LAMBDA_NAME = aws_lambda_function.report_lambda.function_name
FETCH_LAMBDA_NAME = aws_lambda_function.fetch_lambda.function_name
STATUS_LAMBDA_NAME = aws_lambda_function.status_lambda.function_name
JOIN_NFS_FINALIZATION_LAMBDA_NAME = aws_lambda_function.join_nfs_finalization_lambda.function_name
PROXY_URL = var.proxy_url
NFS_INTERFACE_GROUP_NAME = var.nfs_interface_group_name
NFS_SECONDARY_IPS_NUM = var.nfs_protocol_gateway_secondary_ips_per_nic
NFS_PROTOCOL_GATEWAY_FE_CORES_NUM = var.nfs_protocol_gateway_fe_cores_num
SMB_PROTOCOL_GATEWAY_FE_CORES_NUM = var.smb_protocol_gateway_fe_cores_num
S3_PROTOCOL_GATEWAY_FE_CORES_NUM = var.s3_protocol_gateway_fe_cores_num
ALB_ARN_SUFFIX = var.create_alb ? aws_lb.alb[0].arn_suffix : ""
}
}
tags = var.tags_map
depends_on = [aws_cloudwatch_log_group.lambdas_log_group]
lifecycle {
precondition {
condition = var.lambdas_dist == "release" || var.lambdas_version == local.lambdas_hash
error_message = "Please update lambdas version."
}
precondition {
condition = var.install_weka_url != "" || var.weka_version != ""
error_message = "Please provide either 'install_weka_url' or 'weka_version' variables."
}
}
}
resource "aws_lambda_function" "clusterize_lambda" {
function_name = "${local.lambda_prefix}-${var.cluster_name}-clusterize-lambda"
s3_bucket = local.s3_bucket
s3_key = local.s3_key
handler = local.handler_name
role = local.lambda_iam_role_arn
memory_size = 128
timeout = 20
runtime = "provided.al2"
architectures = ["arm64"]
dynamic "vpc_config" {
for_each = range(0, local.enable_lambda_vpc)
content {
security_group_ids = local.sg_ids
subnet_ids = local.subnet_ids
}
}
environment {
variables = {
LAMBDA = "clusterize"
REGION = local.region
HOSTS_NUM = var.cluster_size
CLUSTER_NAME = var.cluster_name
PREFIX = local.obs_prefix
NVMES_NUM = var.containers_config_map[var.instance_type].nvme
ADMIN_PASSWORD_ID = aws_secretsmanager_secret.weka_password.id
DEPLOYMENT_PASSWORD_ID = aws_secretsmanager_secret.weka_deployment_password.id
STATE_TABLE = local.dynamodb_table_name
STATE_TABLE_HASH_KEY = local.dynamodb_hash_key_name
STATE_KEY = local.state_key
NFS_STATE_KEY = local.nfs_state_key
STRIPE_WIDTH = var.stripe_width != -1 ? var.stripe_width : local.stripe_width
PROTECTION_LEVEL = var.protection_level
HOTSPARE = var.hotspare
SET_OBS = var.tiering_enable_obs_integration
OBS_NAME = var.tiering_obs_name
OBS_TIERING_SSD_PERCENT = var.tiering_enable_ssd_percent
TIERING_TARGET_SSD_RETENTION = var.tiering_obs_target_ssd_retention
TIERING_START_DEMOTE = var.tiering_obs_start_demote
SET_DEFAULT_FS = var.set_default_fs
POST_CLUSTER_SETUP_SCRIPT = var.post_cluster_setup_script
FRONTEND_CONTAINER_CORES_NUM = var.set_dedicated_fe_container ? var.containers_config_map[var.instance_type].frontend : 0
PROXY_URL = var.proxy_url
CREATE_CONFIG_FS = (var.smbw_enabled && var.smb_setup_protocol) || var.s3_setup_protocol
WEKA_HOME_URL = var.weka_home_url
INSTALL_DPDK = var.install_cluster_dpdk
# pass lambda function names
CLUSTERIZE_FINALIZATION_LAMBDA_NAME = aws_lambda_function.clusterize_finalization_lambda.function_name
REPORT_LAMBDA_NAME = aws_lambda_function.report_lambda.function_name
FETCH_LAMBDA_NAME = aws_lambda_function.fetch_lambda.function_name
NFS_INTERFACE_GROUP_NAME = var.nfs_interface_group_name
NFS_PROTOCOL_GATEWAYS_NUM = var.nfs_protocol_gateways_number
ALB_ARN_SUFFIX = var.create_alb ? aws_lb.alb[0].arn_suffix : ""
}
}
tags = var.tags_map
depends_on = [aws_cloudwatch_log_group.lambdas_log_group]
}
resource "aws_lambda_function" "clusterize_finalization_lambda" {
function_name = substr("${local.lambda_prefix}-${var.cluster_name}-clusterize-finalization-lambda", 0, 64)
s3_bucket = local.s3_bucket
s3_key = local.s3_key
handler = local.handler_name
role = local.lambda_iam_role_arn
memory_size = 128
timeout = 20
runtime = "provided.al2"
architectures = ["arm64"]
dynamic "vpc_config" {
for_each = range(0, local.enable_lambda_vpc)
content {
security_group_ids = local.sg_ids
subnet_ids = local.subnet_ids
}
}
environment {
variables = {
LAMBDA = "clusterizeFinalization"
STATE_TABLE = local.dynamodb_table_name
STATE_TABLE_HASH_KEY = local.dynamodb_hash_key_name
STATE_KEY = local.state_key
NFS_STATE_KEY = local.nfs_state_key
}
}
tags = var.tags_map
depends_on = [aws_cloudwatch_log_group.lambdas_log_group]
}
resource "aws_lambda_function" "join_nfs_finalization_lambda" {
function_name = substr("${local.lambda_prefix}-${var.cluster_name}-join-nfs-finalization-lambda", 0, 64)
s3_bucket = local.s3_bucket
s3_key = local.s3_key
handler = local.handler_name
role = local.lambda_iam_role_arn
memory_size = 128
timeout = 20
runtime = "provided.al2"
architectures = ["arm64"]
dynamic "vpc_config" {
for_each = range(0, local.enable_lambda_vpc)
content {
security_group_ids = local.sg_ids
subnet_ids = local.subnet_ids
}
}
environment {
variables = {
LAMBDA = "joinNfsFinalization"
}
}
tags = var.tags_map
depends_on = [aws_cloudwatch_log_group.lambdas_log_group]
}
resource "aws_lambda_function" "management" {
function_name = "${local.lambda_prefix}-${var.cluster_name}-management-lambda"
s3_bucket = local.s3_bucket
s3_key = local.s3_key
handler = local.handler_name
role = local.lambda_iam_role_arn
memory_size = 128
timeout = 20
runtime = "provided.al2"
architectures = ["arm64"]
vpc_config {
security_group_ids = local.sg_ids
subnet_ids = local.subnet_ids
}
environment {
variables = {
LAMBDA = "management"
REGION = local.region
CLUSTER_NAME = var.cluster_name
USERNAME_ID = aws_secretsmanager_secret.weka_username.id
DEPLOYMENT_PASSWORD_ID = aws_secretsmanager_secret.weka_deployment_password.id
ADMIN_PASSWORD_ID = aws_secretsmanager_secret.weka_password.id
USE_SECRETMANAGER_ENDPOINT = var.secretmanager_use_vpc_endpoint
}
}
tags = var.tags_map
depends_on = [aws_cloudwatch_log_group.lambdas_log_group]
}
resource "aws_lambda_function" "report_lambda" {
function_name = "${local.lambda_prefix}-${var.cluster_name}-report-lambda"
s3_bucket = local.s3_bucket
s3_key = local.s3_key
handler = local.handler_name
role = local.lambda_iam_role_arn
memory_size = 128
timeout = 20
runtime = "provided.al2"
architectures = ["arm64"]
dynamic "vpc_config" {
for_each = range(0, local.enable_lambda_vpc)
content {
security_group_ids = local.sg_ids
subnet_ids = local.subnet_ids
}
}
environment {
variables = {
LAMBDA = "report"
REGION = local.region
STATE_TABLE = local.dynamodb_table_name
STATE_TABLE_HASH_KEY = local.dynamodb_hash_key_name
STATE_KEY = local.state_key
NFS_STATE_KEY = local.nfs_state_key
}
}
tags = var.tags_map
depends_on = [aws_cloudwatch_log_group.lambdas_log_group]
}
resource "aws_lambda_function" "status_lambda" {
function_name = "${local.lambda_prefix}-${var.cluster_name}-status-lambda"
s3_bucket = local.s3_bucket
s3_key = local.s3_key
handler = local.handler_name
role = local.lambda_iam_role_arn
memory_size = 128
timeout = 20
runtime = "provided.al2"
architectures = ["arm64"]
dynamic "vpc_config" {
for_each = range(0, local.enable_lambda_vpc)
content {
security_group_ids = local.sg_ids
subnet_ids = local.subnet_ids
}
}
environment {
variables = {
LAMBDA = "status"
STATE_TABLE = local.dynamodb_table_name
STATE_TABLE_HASH_KEY = local.dynamodb_hash_key_name
STATE_KEY = local.state_key
NFS_STATE_KEY = local.nfs_state_key
CLUSTER_NAME = var.cluster_name
MANAGEMENT_LAMBDA = aws_lambda_function.management.function_name
USERNAME_ID = aws_secretsmanager_secret.weka_username.id
DEPLOYMENT_PASSWORD_ID = aws_secretsmanager_secret.weka_deployment_password.id
ADMIN_PASSWORD_ID = aws_secretsmanager_secret.weka_password.id
USE_SECRETMANAGER_ENDPOINT = var.secretmanager_use_vpc_endpoint
}
}
tags = var.tags_map
depends_on = [aws_cloudwatch_log_group.lambdas_log_group]
}
resource "aws_lambda_function" "fetch_lambda" {
function_name = "${local.lambda_prefix}-${var.cluster_name}-fetch-lambda"
s3_bucket = local.s3_bucket
s3_key = local.s3_key
handler = local.handler_name
role = local.lambda_iam_role_arn
memory_size = 128
timeout = 20
runtime = "provided.al2"
architectures = ["arm64"]
dynamic "vpc_config" {
for_each = range(0, local.enable_lambda_vpc)
content {
security_group_ids = local.sg_ids
subnet_ids = local.subnet_ids
}
}
environment {
variables = {
LAMBDA = "fetch"
REGION = local.region
STATE_TABLE = local.dynamodb_table_name
ROLE = "backend"
DOWN_BACKENDS_REMOVAL_TIMEOUT = var.debug_down_backends_removal_timeout
CLUSTER_NAME = var.cluster_name
ASG_NAME = "${local.ec2_prefix}-${var.cluster_name}-autoscaling-group"
NFS_ASG_NAME = "${local.ec2_prefix}-${var.cluster_name}-nfs-protocol-gateway"
USERNAME_ID = aws_secretsmanager_secret.weka_username.id
DEPLOYMENT_PASSWORD_ID = aws_secretsmanager_secret.weka_deployment_password.id
ADMIN_PASSWORD_ID = aws_secretsmanager_secret.weka_password.id
USE_SECRETMANAGER_ENDPOINT = var.secretmanager_use_vpc_endpoint
}
}
tags = var.tags_map
depends_on = [aws_cloudwatch_log_group.lambdas_log_group]
}
resource "aws_lambda_function" "scale_down_lambda" {
function_name = "${local.lambda_prefix}-${var.cluster_name}-scale-down-lambda"
s3_bucket = local.s3_bucket
s3_key = local.s3_key
handler = local.handler_name
role = local.lambda_iam_role_arn
memory_size = 128
timeout = 20
runtime = "provided.al2"
architectures = ["arm64"]
vpc_config {
security_group_ids = local.sg_ids
subnet_ids = local.subnet_ids
}
environment {
variables = {
LAMBDA = "scaleDown"
REGION = local.region
CLUSTER_NAME = var.cluster_name
USERNAME_ID = aws_secretsmanager_secret.weka_username.id
DEPLOYMENT_PASSWORD_ID = aws_secretsmanager_secret.weka_deployment_password.id
ADMIN_PASSWORD_ID = aws_secretsmanager_secret.weka_password.id
}
}
tags = var.tags_map
depends_on = [aws_cloudwatch_log_group.lambdas_log_group]
}
resource "aws_lambda_function" "transient_lambda" {
function_name = "${local.lambda_prefix}-${var.cluster_name}-transient-lambda"
s3_bucket = local.s3_bucket
s3_key = local.s3_key
handler = local.handler_name
role = local.lambda_iam_role_arn
memory_size = 128
timeout = 20
runtime = "provided.al2"
architectures = ["arm64"]
dynamic "vpc_config" {
for_each = range(0, local.enable_lambda_vpc)
content {
security_group_ids = local.sg_ids
subnet_ids = local.subnet_ids
}
}
environment {
variables = {
LAMBDA = "transient"
REGION = local.region
CLUSTER_NAME = var.cluster_name
}
}
tags = var.tags_map
depends_on = [aws_cloudwatch_log_group.lambdas_log_group]
}
resource "aws_lambda_function" "terminate_lambda" {
function_name = "${local.lambda_prefix}-${var.cluster_name}-terminate-lambda"
s3_bucket = local.s3_bucket
s3_key = local.s3_key
handler = local.handler_name
role = local.lambda_iam_role_arn
memory_size = 128
timeout = 20
runtime = "provided.al2"
architectures = ["arm64"]
dynamic "vpc_config" {
for_each = range(0, local.enable_lambda_vpc)
content {
security_group_ids = local.sg_ids
subnet_ids = local.subnet_ids
}
}
environment {
variables = {
LAMBDA = "terminate"
REGION = local.region
CLUSTER_NAME = var.cluster_name
ASG_NAME = "${local.ec2_prefix}-${var.cluster_name}-autoscaling-group"
NFS_ASG_NAME = "${local.ec2_prefix}-${var.cluster_name}-nfs-protocol-gateway"
}
}
tags = var.tags_map
depends_on = [aws_cloudwatch_log_group.lambdas_log_group]
}