Skip to main content
Application Auto Scaling adjusts the number of running ECS tasks based on CloudWatch metrics or a schedule. The service module enables autoscaling by default and creates an aws_appautoscaling_target, aws_appautoscaling_policy, and optionally aws_appautoscaling_scheduled_action resources.
The service module always ignores desired_count. Once autoscaling is active, the number of tasks is controlled by Application Auto Scaling, not Terraform. See Managing desired count externally for the workaround when autoscaling is disabled.

Default behavior

Autoscaling is enabled by default (enable_autoscaling = true) with the following defaults: The two default target tracking policies are:
  • ECSServiceAverageCPUUtilization — scales out when average CPU across tasks exceeds the target.
  • ECSServiceAverageMemoryUtilization — scales out when average memory across tasks exceeds the target.
Both policies scale in when utilization drops below the target.

Custom autoscaling policies

Override or extend the default policies by providing a autoscaling_policies map. Each key names the policy:
Start with target values around 70% for CPU and 80% for memory. This leaves enough headroom for traffic spikes before new tasks become healthy.

Scheduled scaling actions

Use autoscaling_scheduled_actions to pre-scale the service before predictable traffic changes (for example, before business hours):
The schedule field accepts:
  • cron(...) — a cron expression in UTC or the specified timezone.
  • rate(...) — a rate expression such as rate(5 minutes).
  • at(...) — a one-time action at a specific UTC timestamp.

Suspending scaling activities

You can temporarily suspend specific scaling processes without removing the policies:

Disabling autoscaling

Set enable_autoscaling = false to disable autoscaling entirely. No aws_appautoscaling_target or policies will be created:
When autoscaling is disabled, the desired_count you set in Terraform controls the initial number of tasks. However, because the service module always ignores desired_count in subsequent applies (to avoid conflicts with autoscaling), any changes to desired_count in Terraform will not be applied to the running service after the first creation.

Managing desired count externally

Because desired_count is always ignored by the module’s lifecycle rules, changes to the number of running tasks must be made outside of Terraform when autoscaling is disabled. One approach is a null_resource that calls the AWS CLI:
Change the desired_count value in the triggers block to trigger the CLI call on the next terraform apply. This pattern is equivalent to the EKS desired size hack.
This workaround requires the AWS CLI to be installed on the machine running Terraform. It also requires appropriate IAM permissions to call ecs:UpdateService.

Task set scale

When using an external deployment controller with task sets, the equivalent of desired_count is the task set scale attribute. The service module ignores scale in the same way it ignores desired_count. To update scale externally: