AWS Application Auto-scaling for ECS with Terraform
Update: Target tracking scaling is now available for ECS services.
I’ve been working on setting up autoscaling settings for ECS services recently, and here are a couple notes from managing auto-scaling for ECS services using Terraform.
min_capacity
andmax_capacity
must both be set.schedule
uses the CloudWatch schedule expression syntax, with the addition of theat(...)
expression.
Creating multiple scheduled actions at once
Terraform will perform the following actions:
+ aws_appautoscaling_scheduled_action.green_evening
id:
arn:
name: "ecs"
resource_id: "service/default-production/green"
scalable_dimension: "ecs:service:DesiredCount"
scalable_target_action.#: "1"
scalable_target_action.0.max_capacity: "20"
scalable_target_action.0.min_capacity: "2"
schedule: "cron(0 15 * * ? *)"
service_namespace: "ecs"
+ aws_appautoscaling_scheduled_action.wapi_green_morning
id:
arn:
name: "ecs"
resource_id: "service/default-production/green"
scalable_dimension: "ecs:service:DesiredCount"
scalable_target_action.#: "1"
scalable_target_action.0.max_capacity: "20"
scalable_target_action.0.min_capacity: "3"
schedule: "cron(0 23 * * ? *)"
service_namespace: "ecs"
This fails with:
* aws_appautoscaling_scheduled_action.green_evening: ConcurrentUpdateException: You already have a pending update to an Auto Scaling resource.
To fix, the scheduled actions need to be executed serially.
resource "aws_appautoscaling_scheduled_action" "green_morning" {
name = "ecs"
service_namespace = "${module.green-autoscaling.service_namespace}"
resource_id = "${module.green-autoscaling.resource_id}"
scalable_dimension = "${module.green-autoscaling.scalable_dimension}"
schedule = "cron(0 23 * * ? *)"
scalable_target_action {
min_capacity = 3
max_capacity = 20
}
}
resource "aws_appautoscaling_scheduled_action" "green_evening" {
name = "ecs"
service_namespace = "${module.green-autoscaling.service_namespace}"
resource_id = "${module.green-autoscaling.resource_id}"
scalable_dimension = "${module.green-autoscaling.scalable_dimension}"
schedule = "cron(0 15 * * ? *)"
scalable_target_action {
min_capacity = 2
max_capacity = 20
}
# Application AutoScaling actions need to be executed serially
depends_on = ["aws_appautoscaling_scheduled_action.green_morning"]
}