Roll back unhealthy Amazon ECS Fargate deployments automatically

Roll back unhealthy Amazon ECS Fargate deployments automatically

A Fargate task can start successfully and still fail its load balancer health check. When that happens during a rollout, Amazon ECS can keep replacing tasks without getting the new revision to a steady state. The deployment circuit breaker gives the rollout a failure condition and can return the service to its last completed deployment.

This walkthrough configures the circuit breaker on an existing staging service and verifies the outcome using ECS deployment history and an application smoke test.

What the circuit breaker watches

During a rolling deployment, the circuit breaker first counts task launches that fail to reach RUNNING. After tasks reach that state, it evaluates supported health checks such as container health checks and Application Load Balancer target health. An app can pass those checks while still failing a customer workflow. Use a representative smoke test or a CloudWatch deployment alarm for failures that health checks cannot see.

Rollback requires a previous deployment in COMPLETED state. A brand-new service has no fallback until its first successful deployment. Rollback also restores the service deployment, so it cannot undo a database migration or repair a shared dependency.

Check the staging service

You need an existing Fargate service using the ECS deployment controller and rolling strategy, at least one successfully completed deployment, and an AWS CLI profile authorized to describe and update the staging service. If you plan to inspect stopped tasks, the profile also needs permission to list and describe tasks. Use staging revisions with harmless test data.

REGION=us-east-1
CLUSTER=staging
SERVICE=web

aws ecs describe-services \
  --region "$REGION" --cluster "$CLUSTER" --services "$SERVICE" \
  --query 'services[0].{controller:deploymentController.type,configuration:deploymentConfiguration,desired:desiredCount,deployments:deployments[].{revision:taskDefinition,state:rolloutState}}'

Look for the ECS controller and a completed deployment. Check the existing minimum healthy and maximum task percentages before changing anything. With two desired tasks, a minimum of 100% and maximum of 200% permits up to four tasks during the rollout while retaining capacity for two healthy tasks. The scheduler still needs enough subnet IP addresses and Fargate quota to start replacements.

Enable rollback and deploy a revision

For a staging service managed directly with the CLI, enable the circuit breaker with the following command. If infrastructure as code owns the service, make the equivalent change in that definition so the next infrastructure deployment keeps it. Preserve any existing alarm configuration and deployment settings.

aws ecs update-service \
  --region "$REGION" --cluster "$CLUSTER" --service "$SERVICE" \
  --deployment-configuration 'deploymentCircuitBreaker={enable=true,rollback=true}' \
  --query 'service.deploymentConfiguration.deploymentCircuitBreaker'

Deploy a specific revision. Replace web:42 with an existing task definition revision from your staging pipeline.

REVISION=web:42

aws ecs update-service \
  --region "$REGION" --cluster "$CLUSTER" --service "$SERVICE" \
  --task-definition "$REVISION" \
  --query 'service.deployments[].{revision:taskDefinition,state:rolloutState,reason:rolloutStateReason}'

Verify the revision that actually won

Wait for the service to stabilize, then inspect the primary deployment. Stability alone can also mean the old revision recovered after rollback. Make the expected revision and smoke test explicit checks in the release pipeline.

aws ecs wait services-stable \
  --region "$REGION" --cluster "$CLUSTER" --services "$SERVICE"

aws ecs describe-services \
  --region "$REGION" --cluster "$CLUSTER" --services "$SERVICE" \
  --query "services[0].deployments[?status=='PRIMARY'].{revision:taskDefinition,state:rolloutState,running:runningCount,reason:rolloutStateReason}"

aws ecs list-service-deployments \
  --region "$REGION" --cluster "$CLUSTER" --service "$SERVICE" \
  --query 'serviceDeployments[].{status:status,created:createdAt,target:targetServiceRevisionArn}'

Compare the primary task definition to the revision that you intended to deploy. Send a request to the staging endpoint and verify an application-level result, such as the expected build version or a read-only workflow. To rehearse rollback, use a dedicated staging service and intentionally fail a test task’s health check. Confirm that deployment history shows the rollback and that the previously healthy revision handles requests.

Diagnose a rollout that fails

  • Tasks never start. Inspect stopped task reasons and the service events. Check image references, execution-role permissions, access to the image registry, and available subnet IP addresses.
  • Tasks run but targets never become healthy. Compare the target group’s health path, expected status code, port, and service health-check grace period with the application’s actual startup behavior.
  • The service stabilizes on the old revision. Read the failed deployment’s details and correct the task definition before deploying a new revision. Keep immutable image versions so the previous definition still points to its original build.

A rehearsal can briefly run extra Fargate tasks and generate logs. Check the Fargate pricing page for regional costs. Remove any disposable staging resources you created for the exercise, including dedicated load balancer resources and log groups after preserving the evidence you need.

Further reading includes how ECS counts deployment failures, service update settings, and deployment history and rollback states.

Related reading

Techmandra Avatar

Leave a Reply

Your email address will not be published. Required fields are marked *