-
Notifications
You must be signed in to change notification settings - Fork 20
/
appautoscaling.tf
108 lines (94 loc) · 3.97 KB
/
appautoscaling.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
resource "aws_appautoscaling_target" "ecs" {
count = var.autoscaling_cpu || var.autoscaling_memory || length(var.autoscaling_custom) > 0 ? 1 : 0
max_capacity = var.autoscaling_max
min_capacity = var.autoscaling_min
resource_id = "service/${var.cluster_name}/${aws_ecs_service.default.name}"
scalable_dimension = "ecs:service:DesiredCount"
service_namespace = "ecs"
}
resource "aws_appautoscaling_policy" "scale_cpu" {
count = var.autoscaling_cpu ? 1 : 0
name = "scale-cpu"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.ecs[0].resource_id
scalable_dimension = aws_appautoscaling_target.ecs[0].scalable_dimension
service_namespace = aws_appautoscaling_target.ecs[0].service_namespace
target_tracking_scaling_policy_configuration {
target_value = var.autoscaling_target_cpu
disable_scale_in = false
scale_in_cooldown = var.autoscaling_scale_in_cooldown
scale_out_cooldown = var.autoscaling_scale_out_cooldown
predefined_metric_specification {
predefined_metric_type = "ECSServiceAverageCPUUtilization"
}
}
}
resource "aws_appautoscaling_policy" "scale_memory" {
count = var.autoscaling_memory ? 1 : 0
name = "scale-memory"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.ecs[0].resource_id
scalable_dimension = aws_appautoscaling_target.ecs[0].scalable_dimension
service_namespace = aws_appautoscaling_target.ecs[0].service_namespace
target_tracking_scaling_policy_configuration {
target_value = var.autoscaling_target_memory
disable_scale_in = false
scale_in_cooldown = var.autoscaling_scale_in_cooldown
scale_out_cooldown = var.autoscaling_scale_out_cooldown
predefined_metric_specification {
predefined_metric_type = "ECSServiceAverageMemoryUtilization"
}
}
}
resource "aws_appautoscaling_policy" "scale_custom" {
for_each = { for custom in var.autoscaling_custom : custom.name => custom }
name = each.value.name
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.ecs[0].resource_id
scalable_dimension = aws_appautoscaling_target.ecs[0].scalable_dimension
service_namespace = aws_appautoscaling_target.ecs[0].service_namespace
target_tracking_scaling_policy_configuration {
scale_in_cooldown = each.value.scale_in_cooldown
scale_out_cooldown = each.value.scale_out_cooldown
target_value = each.value.target_value
customized_metric_specification {
metric_name = each.value.metric_name
namespace = each.value.namespace
statistic = each.value.statistic
dimensions {
name = "ClusterName"
value = var.cluster_name
}
dimensions {
name = "ServiceName"
value = var.name
}
}
}
}
resource "aws_appautoscaling_scheduled_action" "scale_service_out" {
count = var.enable_schedule ? 1 : 0
name = "${var.name}-scale-out"
service_namespace = aws_appautoscaling_target.ecs[0].service_namespace
resource_id = aws_appautoscaling_target.ecs[0].resource_id
scalable_dimension = aws_appautoscaling_target.ecs[0].scalable_dimension
schedule = var.schedule_cron_stop
timezone = "UTC"
scalable_target_action {
min_capacity = 0
max_capacity = 0
}
}
resource "aws_appautoscaling_scheduled_action" "scale_service_in" {
count = var.enable_schedule ? 1 : 0
name = "${var.name}-scale-in"
service_namespace = aws_appautoscaling_target.ecs[0].service_namespace
resource_id = aws_appautoscaling_target.ecs[0].resource_id
scalable_dimension = aws_appautoscaling_target.ecs[0].scalable_dimension
schedule = var.schedule_cron_start
timezone = "UTC"
scalable_target_action {
min_capacity = var.autoscaling_min
max_capacity = var.autoscaling_max
}
}