Introduction
To save costs, we can scale down the ECS services during off-hours and scale them up when needed1, particularly in development and testing environments.
Terraform code snippet
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
resource "aws_appautoscaling_target" "ecs" {
max_capacity = 4
min_capacity = 2
resource_id = "service/clusterName/serviceName"
scalable_dimension = "ecs:service:DesiredCount"
service_namespace = "ecs"
}
resource "aws_appautoscaling_scheduled_action" "scale_down" {
name = "ecs_scale_down"
service_namespace = aws_appautoscaling_target.ecs.service_namespace
resource_id = aws_appautoscaling_target.ecs.resource_id
scalable_dimension = aws_appautoscaling_target.ecs.scalable_dimension
schedule = "cron(00 18 ? * MON-FRI *)"
scalable_target_action {
min_capacity = 0
max_capacity = 0
}
}
resource "aws_appautoscaling_scheduled_action" "scale_up" {
name = "ecs_scale_up"
service_namespace = aws_appautoscaling_target.ecs.service_namespace
resource_id = aws_appautoscaling_target.ecs.resource_id
scalable_dimension = aws_appautoscaling_target.ecs.scalable_dimension
schedule = "cron(00 07 ? * MON-FRI *)"
scalable_target_action {
min_capacity = 2
max_capacity = 4
}
}
We can specify the time zone used when setting aws_appautoscaling_scheduled_action
. If a time zone is not provided, UTC is used by default2.