- Newest
- Most votes
- Most comments
When a task is marked as unhealthy in an Amazon ECS service, the service scheduler follows a specific process for replacement. The scheduler first starts a replacement task, and then evaluates the health status of this new task.
If the replacement task becomes healthy, the service scheduler stops the unhealthy task. However, if the replacement task also becomes unhealthy, the scheduler will stop either the unhealthy replacement task or the existing unhealthy task to maintain the desired count.
In your scenario with desired count of 1 and maximum percent of 200%, when your original task becomes unhealthy, ECS starts a replacement task. If both tasks (original and replacement) become unhealthy, ECS will stop one of them randomly to maintain the desired count of 1, even if this occurs within the health check grace period.
The health check grace period (healthCheckGracePeriodSeconds) doesn't prevent ECS from terminating unhealthy tasks. It only gives newly launched tasks time to bootstrap before they start receiving health checks from the load balancer. For container health checks, this grace period doesn't apply in the same way.
There isn't a configuration option to tell ECS to hold off on terminating the old unhealthy task while waiting for the replacement to become healthy. The service scheduler's behavior is to maintain the desired count while trying to replace unhealthy tasks, and if both old and new tasks are unhealthy, it will stop one of them to maintain the desired count.
If you need more resilience in this scenario, you might consider increasing your desired count to have multiple healthy tasks running, or implementing application-level health management logic that can handle temporary unhealthy states without failing health checks immediately.
Sources
Amazon ECS services - Amazon Elastic Container Service
Update Amazon ECS service parameters - Amazon Elastic Container Service
HealthCheck - Amazon Elastic Container Service
answered a year ago

I think most of it is accurate but upon my testing I think the health check grace period applies the same way on both containers and ALB.