-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoscaling.tf
94 lines (76 loc) · 3.88 KB
/
autoscaling.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
resource "aws_appautoscaling_target" "main" {
count = var.autoscaling_enabled && var.enable_module ? 1 : 0
max_capacity = var.autoscale_max_capacity
min_capacity = var.autoscale_min_capacity
resource_id = "service/${var.cluster_name}/${aws_ecs_service.main[count.index].name}"
role_arn = var.execution_role_arn
scalable_dimension = "ecs:service:DesiredCount"
service_namespace = "ecs"
}
resource "aws_cloudwatch_metric_alarm" "high_threshold" {
count = var.autoscaling_enabled && var.enable_module ? 1 : 0
alarm_name = "{aws_ecs_service.main[count.index].name}-${lookup(var.metrics_alarms[count.index], "metric_name")}-high-threshold"
comparison_operator = lookup(var.metrics_alarms[count.index], "comparison_operator_high")
evaluation_periods = lookup(var.metrics_alarms[count.index], "evaluation_periods")
metric_name = lookup(var.metrics_alarms[count.index], "metric_name")
namespace = lookup(var.metrics_alarms[count.index], "namespace")
period = lookup(var.metrics_alarms[count.index], "period")
statistic = lookup(var.metrics_alarms[count.index], "statistic")
threshold = lookup(var.metrics_alarms[count.index], "scale_up_threshold")
dimensions = {
ClusterName = var.cluster_name
ServiceName = aws_ecs_service.main[count.index].name
}
alarm_actions = [aws_appautoscaling_policy.app_up[count.index].arn]
depends_on = [aws_appautoscaling_policy.app_up]
}
resource "aws_cloudwatch_metric_alarm" "low_threshold" {
count = var.autoscaling_enabled && var.enable_module ? 1 : 0
alarm_name = "{aws_ecs_service.main[count.index].name}-${lookup(var.metrics_alarms[count.index], "metric_name")}-low-threshold"
comparison_operator = lookup(var.metrics_alarms[count.index], "comparison_operator_low")
evaluation_periods = lookup(var.metrics_alarms[count.index], "evaluation_periods")
metric_name = lookup(var.metrics_alarms[count.index], "metric_name")
namespace = lookup(var.metrics_alarms[count.index], "namespace")
period = lookup(var.metrics_alarms[count.index], "period")
statistic = lookup(var.metrics_alarms[count.index], "statistic")
threshold = lookup(var.metrics_alarms[count.index], "scale_down_threshold")
dimensions = {
ClusterName = var.cluster_name
ServiceName = aws_ecs_service.main[count.index].name
}
alarm_actions = [aws_appautoscaling_policy.app_down[count.index].arn]
}
resource "aws_appautoscaling_policy" "app_up" {
count = var.autoscaling_enabled && var.enable_module ? 1 : 0
name = "app-scale-up"
service_namespace = aws_appautoscaling_target.main[count.index].service_namespace
resource_id = aws_appautoscaling_target.main[count.index].resource_id
scalable_dimension = aws_appautoscaling_target.main[count.index].scalable_dimension
step_scaling_policy_configuration {
adjustment_type = "ChangeInCapacity"
cooldown = 60
metric_aggregation_type = "Average"
step_adjustment {
metric_interval_lower_bound = 0
scaling_adjustment = 1
}
}
depends_on = [aws_appautoscaling_target.main]
}
resource "aws_appautoscaling_policy" "app_down" {
count = var.autoscaling_enabled && var.enable_module ? 1 : 0
name = "app-scale-down"
service_namespace = aws_appautoscaling_target.main[count.index].service_namespace
resource_id = aws_appautoscaling_target.main[count.index].resource_id
scalable_dimension = aws_appautoscaling_target.main[count.index].scalable_dimension
step_scaling_policy_configuration {
adjustment_type = "ChangeInCapacity"
cooldown = 300
metric_aggregation_type = "Average"
step_adjustment {
metric_interval_upper_bound = 0
scaling_adjustment = -1
}
}
depends_on = [aws_appautoscaling_target.main]
}