Scale an ECS Fargate service with Application Auto Scaling: CPU and request-count target tracking, scaling queue workers on backlog per task, and the cooldown and minimum settings that keep it stable.

An ECS service runs a fixed number of tasks until something changes desiredCount. With auto scaling, Application Auto Scaling watches CloudWatch metrics and raises or lowers that count between a minimum and maximum you choose. The common mistake is scaling on a metric that does not track load, such as CPU on a service that mostly waits on the database.
CPU is the default metric and often a poor one: an API waiting on Postgres can drown at 30 percent CPU, and a queue worker idling between batches looks underused while the backlog grows. This guide covers target tracking for HTTP services, backlog-based scaling for workers, and the settings that stop a service from flapping.
The ECS service auto scaling guide covers each type, including predictive scaling, which forecasts load from history.
Every policy hangs off a scalable target, the service plus its minimum and maximum task count.
Keep the minimum at two or more for anything user-facing, so a single task replacement does not take the service down. The maximum is a cost ceiling and a guard against a runaway policy, and it should stay below what your database connection limit can absorb: twenty tasks with a pool of 25 connections each is 500 connections.
Load test one task to find the request rate it handles at acceptable latency, then target about 70 percent of that. If a task handles 300 requests per minute comfortably, target 200.
A short scale-out cooldown and a long scale-in cooldown is the usual shape: add tasks quickly, remove them slowly. New Fargate tasks take a minute or more to pull the image, start and pass health checks, so a target set too close to the limit means users feel the spike before help arrives.
ApproximateNumberOfMessagesVisible alone is a poor target, since a backlog of 1,000 is fine for twenty tasks and terrible for one. AWS recommends backlog per instance: visible messages divided by running tasks. Target tracking supports metric math, so you can compute it without publishing a custom metric.
{
"TargetValue": 100,
"CustomizedMetricSpecification": {
"Metrics": [
{
"Id": "visible",
"MetricStat": {
"Metric": {
"Namespace": "AWS/SQS",
"MetricName": "ApproximateNumberOfMessagesVisible",
"Dimensions": [{ "Name": "QueueName", "Value": "image-resize" }]
},
"Stat": "Sum"
},
"ReturnData": false
},
{
"Id": "tasks",
"MetricStat": {
"Metric": {
"Namespace": "ECS/ContainerInsights",
"MetricName": "RunningTaskCount",
"Dimensions": [
{ "Name": "ClusterName", "Value": "prod" },
{ "Name": "ServiceName", "Value": "image-worker" }
]
},
"Stat": "Average"
},
"ReturnData": false
},
{ "Id": "backlog_per_task", "Expression": "visible / tasks", "ReturnData": true }
]
},
"ScaleOutCooldown": 60,
"ScaleInCooldown": 300
}
RunningTaskCount comes from Container Insights, which must be enabled on the cluster. Pick the target from throughput and latency: if one task handles 10 messages per second and messages should wait at most 10 seconds, the acceptable backlog per task is 100. For a worker that scales to zero, add a step scaling policy that sets the count to one when ApproximateNumberOfMessagesVisible is above zero, since the expression above divides by zero tasks.
Best fit: target tracking on requests per task for APIs, backlog per task for workers, step scaling to wake a worker from zero.
Encore brings infrastructure from code, and on AWS it deploys your services as Fargate services or Kubernetes deployments in your own account, behind an ALB, with SQS queues for each Pub/Sub subscription. Process allocation decides the shape: all services in one process to keep costs and latency low, or one process per service so each scales on its own, set per environment without code changes.
The services are ordinary ECS services in your account, and Encore's docs do not describe an auto scaling setting for Fargate, so the policies above are how you scale them: register the Encore-created service as a scalable target and attach a policy. Encore's configuration docs describe PATCH-style updates that keep changes made in the AWS console, but check the scalable target after the next deploy before you rely on it. In code, the lever you control is how much work each task takes on:
package images
import (
"context"
"time"
"encore.dev/pubsub"
)
type ResizeRequest struct {
ObjectKey string `json:"object_key"`
}
var ResizeRequests = pubsub.NewTopic[*ResizeRequest]("resize-requests", pubsub.TopicConfig{
DeliveryGuarantee: pubsub.AtLeastOnce,
})
var _ = pubsub.NewSubscription(ResizeRequests, "resize", pubsub.SubscriptionConfig[*ResizeRequest]{
Handler: Resize,
MaxConcurrency: 8, // per task: 10 tasks process up to 80 at once
AckDeadline: 2 * time.Minute, // long enough for the largest image
})
func Resize(ctx context.Context, r *ResizeRequest) error {
// Download, resize, upload. Safe to run twice for the same key.
return nil
}
MaxConcurrency is documented per instance, so it multiplies with the task count your scaling policy sets, and it is the number to load test when you pick a backlog-per-task target. With one process per service, the image worker scales on its queue while the API scales on requests.
For an agent, the useful part is that the concurrency and deadline sit next to the handler it is changing, and the change runs against a local broker before anyone touches a scaling policy.
Best fit: teams on Encore with one process per service in production who want per-service scaling policies in their own AWS account and per-task concurrency declared in code.
Use target tracking on ALBRequestCountPerTarget for APIs and on backlog per task for queue workers, with a short scale-out and long scale-in cooldown. Cap the maximum below your database connection budget. On Encore, choose one process per service for the services that need to scale independently, attach the same Application Auto Scaling policies, and tune MaxConcurrency alongside the backlog target.
ECS uses Application Auto Scaling to change a service's desired task count. You register the service as a scalable target with a minimum and maximum count, then attach target tracking, step scaling or scheduled actions. CloudWatch alarms created by the policy trigger the changes.
For HTTP services behind an ALB, ALBRequestCountPerTarget tracks load more directly than CPU for I/O-bound code, while average CPU works well for CPU-bound services. For queue workers, scale on backlog per task: visible messages divided by running tasks, compared with how many messages one task can work through in your latency target.
Yes, a scalable target can have a minimum capacity of 0, which suits queue workers. Target tracking on backlog per task cannot divide by zero tasks, so add a step scaling policy on queue depth to start the first task when messages arrive.
Encore deploys services as ECS Fargate services or Kubernetes deployments in your own AWS account, and with process allocation you choose whether services share a process or each run separately. Encore's docs do not describe an auto scaling setting for Fargate, so you attach Application Auto Scaling policies to the service in your account, and set MaxConcurrency on subscriptions to control work per task.
Deploy to your cloud on AWS and Google Cloud
without managing infrastructure
Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.