-
Notifications
You must be signed in to change notification settings - Fork 3
/
step_function.tf
94 lines (87 loc) · 3.13 KB
/
step_function.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
locals {
sfn_function_name = [aws_lambda_function.fetch_lambda.function_name, aws_lambda_function.scale_down_lambda.function_name, aws_lambda_function.terminate_lambda.function_name, aws_lambda_function.transient_lambda.function_name]
}
resource "aws_cloudwatch_log_group" "sfn_log_group" {
name = "/aws/vendedlogs/states/${var.prefix}-${var.cluster_name}-scale-down-sfn"
retention_in_days = 30
tags = var.tags_map
}
resource "aws_cloudwatch_event_rule" "event_rule" {
name = "${var.prefix}-${var.cluster_name}-scale-down-trigger-rule"
schedule_expression = "rate(1 minute)"
description = "CloudWatch trigger scale down step function every 1 minute"
tags = merge(var.tags_map, {
Name = "${var.prefix}-${var.cluster_name}-scale-down-trigger-rule"
})
}
resource "aws_cloudwatch_event_target" "step_function_event_target" {
target_id = "TriggerStepFunctionFromCloudWatch"
rule = aws_cloudwatch_event_rule.event_rule.name
arn = aws_sfn_state_machine.scale_down_state_machine.arn
role_arn = local.event_iam_role_arn
depends_on = [aws_cloudwatch_event_rule.event_rule, aws_sfn_state_machine.scale_down_state_machine]
}
resource "aws_lambda_permission" "invoke_lambda_permission" {
count = length(local.sfn_function_name)
action = "lambda:InvokeFunction"
function_name = local.sfn_function_name[count.index]
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.event_rule.arn
statement_id = "AllowExecutionFromCloudWatch"
}
resource "aws_sfn_state_machine" "scale_down_state_machine" {
name = "${var.prefix}-${var.cluster_name}-scale-down-state-machine"
role_arn = local.sfn_iam_role_arn
definition = <<EOF
{
"Comment": "Run Fetch function for scale down",
"StartAt": "Fetch",
"States": {
"Fetch": {
"Type": "Task",
"Resource": "${aws_lambda_function.fetch_lambda.arn}",
"Next": "ScaleDown"
},
"ScaleDown" : {
"Type": "Task",
"Resource": "${aws_lambda_function.scale_down_lambda.arn}",
"Next": "Terminate"
},
"Terminate":{
"Type": "Task",
"Resource": "${aws_lambda_function.terminate_lambda.arn}",
"Next": "ErrorCheck"
},
"ErrorCheck" :{
"Type": "Choice",
"Choices": [
{
"Variable": "$.TransientErrors",
"IsNull": false,
"Next": "Transient"
}
],
"Default": "Success"
},
"Success": {
"Type": "Succeed",
"Comment": "Scale Down Succeed."
},
"Transient": {
"Type": "Task",
"Resource": "${aws_lambda_function.transient_lambda.arn}",
"End":true
}
}
}
EOF
logging_configuration {
log_destination = "${aws_cloudwatch_log_group.sfn_log_group.arn}:*"
include_execution_data = true
level = "ALL"
}
tags = merge(var.tags_map, {
Name = "${var.prefix}-${var.cluster_name}-scale-up-sfn"
})
depends_on = [aws_lambda_function.scale_down_lambda, aws_lambda_function.fetch_lambda, aws_lambda_function.terminate_lambda, aws_lambda_function.transient_lambda, aws_cloudwatch_log_group.sfn_log_group]
}