-
Notifications
You must be signed in to change notification settings - Fork 1
/
autoscaling.tf
100 lines (85 loc) · 2.96 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
95
96
97
98
99
100
resource "aws_appautoscaling_target" "main" {
/*
Auto scaling target for the ECS services
*/
depends_on = [aws_ecs_service.main]
for_each = local.grouped_services
resource_id = "service/${var.cluster_name}/${each.value.name}"
min_capacity = each.value.auto_scaling.min_instances
max_capacity = each.value.auto_scaling.max_instances
scalable_dimension = "ecs:service:DesiredCount"
service_namespace = "ecs"
}
resource "aws_appautoscaling_policy" "cpu" {
/*
Scale services based on CPU use
*/
depends_on = [aws_ecs_service.main]
for_each = {
for name, service in local.grouped_services:
(name) => service
if service.auto_scaling.cpu_threshold != null
}
name = "cpu"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.main[each.key].resource_id
scalable_dimension = aws_appautoscaling_target.main[each.key].scalable_dimension
service_namespace = aws_appautoscaling_target.main[each.key].service_namespace
target_tracking_scaling_policy_configuration {
target_value = each.value.auto_scaling.cpu_threshold
predefined_metric_specification {
predefined_metric_type = "ECSServiceAverageCPUUtilization"
}
}
}
resource "aws_appautoscaling_policy" "memory" {
/*
Scale services based memory use
*/
for_each = {
for name, service in local.grouped_services:
(name) => service
if service.auto_scaling.memory_threshold != null
}
name = "memory"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.main[each.key].resource_id
scalable_dimension = aws_appautoscaling_target.main[each.key].scalable_dimension
service_namespace = aws_appautoscaling_target.main[each.key].service_namespace
target_tracking_scaling_policy_configuration {
target_value = each.value.auto_scaling.memory_threshold
predefined_metric_specification {
predefined_metric_type = "ECSServiceAverageMemoryUtilization"
}
}
}
resource "aws_appautoscaling_scheduled_action" "shutdown" {
/*
Scheduled shutdown of services
*/
for_each = (var.scheduled_shutdown != null ? local.grouped_services : {})
name = "ecs"
service_namespace = aws_appautoscaling_target.main[each.key].service_namespace
scalable_dimension = aws_appautoscaling_target.main[each.key].scalable_dimension
resource_id = aws_appautoscaling_target.main[each.key].resource_id
schedule = var.scheduled_shutdown
scalable_target_action {
min_capacity = 0
max_capacity = 0
}
}
resource "aws_appautoscaling_scheduled_action" "start" {
/*
Scheduled start of services
*/
for_each = (var.scheduled_start != null ? local.grouped_services : {})
name = "ecs"
service_namespace = aws_appautoscaling_target.main[each.key].service_namespace
scalable_dimension = aws_appautoscaling_target.main[each.key].scalable_dimension
resource_id = aws_appautoscaling_target.main[each.key].resource_id
schedule = var.scheduled_start
scalable_target_action {
min_capacity = 1
max_capacity = 1
}
}