Skip to content

ECS Task Replacement

0

Hi

I have a question in regards to the ECS task replacement mechanism using the service health check grace period (healthCheckGracePeriodSeconds) https://docs.aws.amazon.com/AmazonECS/latest/developerguide/service_definition_parameters.html#sd-networkconfiguration)

First let me layout my setup (note that the numbers are just for examples and not really used in production). Basically I have an ECS service which has the following configuration:

  • desired count: 1
  • health check grace period: 600 seconds
  • min/max task percentage: 100/200
  • launch type: Fargate
  • health check configured within the container

Scenario

After a successful deployment I would have 1 running healthy task. However after some time the one container started reporting unhealthy on its checks with ECS task replacement mechanism and since the max number of tasks that can run is configured to 200% which means 2 task, ECS will spin up 1 more task with now a total of 2 tasks. And within the health check grace period the new replacement task may report unhealthy therefore at some point you would get 2 running tasks (the old and new one) that are unhealthy. In this scenario I would expect ECS to wait either until the health check grace period has expired or the new container becomes healthy (whichever comes first) before terminating the old running unhealthy container, however the actual behaviour is that ECS terminates the old unhealthy running container when both the old and new replacement containers becomes unhealthy even if it’s still within the health check grace period. My question is: Is this the correct behaviour or is there something I can configure to tell ECS to hold off in terminating the old running unhealthy container and either wait for the replacement container to become healthy (as long as it is within the health check grace period) or the health check grace period has expired and the replacement container is still unhealthy?

Thanks

1 Answer
0
Accepted Answer

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.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.