Sub topic 14
Safe deployments with the circuit breaker
This chapter assumes you can already create an ECS service with the AWS CLI, that you know what a task definition revision is, and that you have attached a service to an Application Load Balancer target group with health checks. Here you will learn how a rolling deployment behaves, how the deployment circuit breaker detects a bad release, and how to read the signals when one fails.
How a rolling update works
When you create a service with the default ECS deployment controller, ECS uses a rolling update. Each time you point the service at a new task definition revision, ECS creates a new deployment. The service then has two sets of tasks: the old ones (the active deployment) and the new ones (the primary deployment). ECS starts new tasks and stops old ones until only the new revision runs.
Two percentages control the pace:
| Setting | Default for rolling update | Meaning |
|---|---|---|
| `minimumHealthyPercent` | 100 | The lowest number of healthy tasks during the deployment, as a percentage of desired count, rounded up. |
| `maximumPercent` | 200 | The highest number of tasks (old and new) allowed to run, as a percentage of desired count, rounded down. |
With a desired count of 4 and the defaults, ECS can run up to 8 tasks. It starts new tasks first and stops old ones only once the new ones are healthy. Capacity never drops below 4. If you set minimumHealthyPercent to 50, ECS may stop two old tasks before the new ones are ready. That saves money and speeds up the rollout, but it gives you less headroom if the new revision is broken.
Without protection, a rolling update with a broken image has a bad failure mode. ECS keeps starting replacement tasks that crash or fail health checks, and it keeps retrying. The deployment stays IN_PROGRESS indefinitely. The old tasks usually keep serving traffic, but you pay for the failing tasks and your event log fills with noise. The circuit breaker exists to stop this.
What the circuit breaker does
The deployment circuit breaker watches a rolling deployment and marks it FAILED when too many tasks fail. It counts a task as a failure in two cases:
- The task never reaches the
RUNNINGstate. Examples are an image pull error, a missing secret, no capacity in the subnet, or a container that exits during startup. - The task reaches
RUNNINGbut then fails health checks. These can be the load balancer health check, the container health check, or a Service Connect or Cloud Map health check.
When the failure count reaches a threshold, ECS stops the deployment. If you enabled rollback, ECS then starts a new deployment that returns the service to the last deployment that completed successfully.
The threshold is calculated for you. ECS uses half of the service's desired count, with a minimum of 3 and a maximum of 200. Because of that minimum, a small service still gets at least three failed tasks before the breaker trips. You cannot set the threshold from the service configuration.
Enabling it
You can enable the circuit breaker when you create the service, as in the earlier service example. For an existing service, use update-service:
aws ecs update-service \
--cluster demo \
--service web \
--deployment-configuration "deploymentCircuitBreaker={enable=true,rollback=true},minimumHealthyPercent=100,maximumPercent=200"Pass minimumHealthyPercent and maximumPercent explicitly when you change the deployment configuration. This keeps the values you expect, instead of relying on defaults that may differ by scheduling strategy.
There are two flags, and they do different things:
enable=trueturns on failure detection. A failed deployment is markedFAILEDand ECS stops launching replacements.rollback=truemakes ECS redeploy the last completed revision automatically. Withenable=true,rollback=false, the service stops trying but stays on whatever mix of tasks it has, and you must fix it by hand.
Running a deployment and watching it
Register a new revision, point the service at it, and wait for the service to become stable:
aws ecs register-task-definition --cli-input-json file://taskdef.json
aws ecs update-service \
--cluster demo \
--service web \
--task-definition web-app
aws ecs wait services-stable --cluster demo --services webIf you omit the revision number, ECS uses the latest active revision of the family. For repeatable pipelines, pin the revision explicitly, for example web-app:7, so a rollback target is unambiguous.
To see the state of each deployment, query the service:
aws ecs describe-services \
--cluster demo \
--services web \
--query "services[0].deployments[].{id:id,status:status,rollout:rolloutState,reason:rolloutStateReason,desired:desiredCount,running:runningCount,pending:pendingCount,failed:failedTasks}"The fields to read:
statusisPRIMARYfor the deployment ECS is moving toward,ACTIVEfor one being drained, andINACTIVEfor one that has finished.rolloutStateisIN_PROGRESS,COMPLETED, orFAILED.rolloutStateReasonexplains the state. A tripped breaker reports a reason such as "ECS deployment circuit breaker: tasks failed to start."failedTasksis the count the breaker compares to its threshold.
Note that aws ecs wait services-stable can fail or time out when a deployment fails, so check rolloutState in your pipeline instead of trusting the exit status alone.
Why a deployment might not trip the breaker
The breaker only reacts to failures that ECS can see. Several common problems pass through it:
- The app starts but returns wrong results. If
/healthreturns 200, ECS and the load balancer consider the task healthy. The breaker cannot detect logic bugs. - A health check grace period that is too short. If the app needs 90 seconds to start but the grace period is 60, the load balancer marks it unhealthy and ECS replaces it. Each replacement counts as a failure, and a good release gets rolled back. Set
--health-check-grace-period-secondsand the containerstartPeriodto cover real startup time. - A lenient health check. A health endpoint that does not touch the database will pass even when the database password is wrong.
- Failures after completion. Once the deployment is
COMPLETED, the breaker no longer watches it. A later crash is handled by normal service replacement, not by rollback.
Alarm-based rollback
You can add CloudWatch alarms as a second signal. If any listed alarm enters the ALARM state during the deployment, ECS fails it and can roll back. This catches problems the health checks miss, such as a spike in 5xx responses.
aws ecs update-service \
--cluster demo \
--service web \
--deployment-configuration "deploymentCircuitBreaker={enable=true,rollback=true},alarms={alarmNames=[web-5xx-high],enable=true,rollback=true},minimumHealthyPercent=100,maximumPercent=200"Choose alarms that are quiet in normal operation. An alarm that already fires before the deployment will block every release.
Practical guidance
- Keep
minimumHealthyPercentat 100 for production services with a load balancer, so a failed release never reduces serving capacity. - Make sure subnets have enough free IP addresses. Each awsvpc task uses one, and a deployment at 200 percent briefly needs double.
- Watch the service event log (
describe-services,events) during the first rollouts. It states why tasks stopped.
Exercise
A service has a desired count of 2, the circuit breaker enabled with rollback, and a new revision whose container exits immediately on start. Roughly how many failed tasks does ECS tolerate before it marks the deployment FAILED, and what happens next?
Show answer
The threshold is half the desired count (1), but it has a minimum of 3, so ECS tolerates about 3 failed tasks. Then it marks the deployment FAILED and, because rollback is enabled, starts a new deployment that returns the service to the last completed task definition revision. With maximumPercent at 200 and minimumHealthyPercent at 100, the old tasks keep serving traffic throughout.