ECS ASG Scheduled Actions

Scheduled ASG actions can be used for a variety of reasons; one typical is to handle predicted rush hours at certain times of the days, special dates etc.

This is a well known feature in EC2, however in ECS it’s relatively hidden, available via the CLI only; I assume the team will work on it at some point — if you find it useful for you own needs too — please vote here!

Create an ECS Scheduled Action via CLI:

aws application-autoscaling put-scheduled-action --service-namespace ecs --scalable-dimension ecs:service:DesiredCount --resource-id service/<cluster-name>/<service-name> --scheduled-action-name some_name --scalable-target-action MinCapacity=3 --schedule "cron(59 * * * ? *)" --region <region>

Describe Existing ECS Scheduled Actions via CLI:

aws application-autoscaling describe-scheduled-actions --service-namespace ecs  --region <region>

When working with EC2/ECS, for this to work we must connect a capacity provider to the cluster: the scheduled action will adjust the desired task number however the capacity provider will control the desired underlining EC2s.

Finally we can automate using Terraform:

resource "aws_appautoscaling_target" "ecs" {
max_capacity = "${var.aws_appautoscaling_target_max_capacity}"
min_capacity = "${var.aws_appautoscaling_target_min_capacity}"
resource_id = "${join("/",list("service", "${var.cluster_arn}", "${var.service_name}"))}"
scalable_dimension = "ecs:service:DesiredCount"
service_namespace = "ecs"
}

resource "aws_appautoscaling_scheduled_action" "scale_in" {
name = "scale_in"
service_namespace = "ecs"
resource_id = "${join("/",list("service", "${var.cluster_arn}", "${var.service_name}"))}"
scalable_dimension = "ecs:service:DesiredCount"
schedule = "${var.aws_appautoscaling_scheduled_action_scale_in_schedule}"

scalable_target_action {
min_capacity = "${var.aws_appautoscaling_scheduled_action_scale_in_scalable_target_action_min_capacity}"
max_capacity = "${var.aws_appautoscaling_scheduled_action_scale_in_scalable_target_action_max_capacity}"
}
}

resource "aws_appautoscaling_scheduled_action" "scale_out" {
name = "scale_out"
service_namespace = "ecs"
resource_id = "${join("/",list("service", "${var.cluster_arn}", "${var.service_name}"))}"
scalable_dimension = "ecs:service:DesiredCount"
schedule = "${var.aws_appautoscaling_scheduled_action_scale_out_schedule}"

scalable_target_action {
min_capacity = "${var.aws_appautoscaling_scheduled_action_scale_out_scalable_target_action_min_capacity}"
max_capacity = "${var.aws_appautoscaling_scheduled_action_scale_out_scalable_target_action_max_capacity}"
}

https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appautoscaling_scheduled_action

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store