Sub topic 13
Health checks and the grace period
This chapter assumes you can read a Fargate task definition, know what an ECS service and an Application Load Balancer (ALB) target group are, and have a service registered with a target group using awsvpc networking. It covers the two health checks that decide whether a task stays alive, how they differ, and how the grace period stops a slow-starting task from being killed.
Two checks with two jobs
A Fargate service behind an ALB has two independent health checks.
| Row | Container health check | ALB target group health check |
|---|---|---|
| Defined in | Task definition (`healthCheck`) | Target group |
| Run by | The container agent, inside the container | The load balancer, over the network |
| Probe | A command; exit code 0 means healthy | An HTTP request to the task's ENI IP address |
| Failure effect | ECS stops the task if the container is essential | The task stops receiving traffic. ECS replaces it once the grace period is over |
| Needs in the image | A shell and the probe tool, such as `curl` | Nothing. The app only has to answer HTTP |
The container check asks "is the process working?" from the inside. The target group check asks "can the load balancer reach it?" from the outside. A task can pass one and fail the other. If the app binds to 127.0.0.1, the container check passes and the ALB check fails.
The container health check
The task definition's healthCheck block uses Docker semantics:
{
"healthCheck": {
"command": ["CMD-SHELL", "curl -f http://localhost:8080/health || exit 1"],
"interval": 30,
"timeout": 5,
"retries": 3,
"startPeriod": 30
}
}intervalis the seconds between runs (5 to 300).timeoutis how long one run may take before it counts as a failure (2 to 60).retriesis the number of consecutive failures before the container is markedUNHEALTHY(1 to 10).startPeriodis a window after container start (0 to 300 seconds) in which failures are not counted. A success inside the window still marks the containerHEALTHY.
A container reports UNKNOWN until the first result arrives, then HEALTHY or UNHEALTHY. When an essential container becomes UNHEALTHY, ECS stops the task. A service then starts a replacement to restore the desired count.
The command runs inside your image. If the image is minimal, such as distroless or scratch, curl does not exist and the check fails every time. Either install a probe tool, build a small binary into the image, or rely on the ALB check alone.
The target group health check
Create the target group with explicit health settings. Fargate tasks need --target-type ip:
aws elbv2 create-target-group \
--name web \
--protocol HTTP \
--port 8080 \
--vpc-id vpc-0123456789abcdef0 \
--target-type ip \
--health-check-path /health \
--health-check-interval-seconds 15 \
--health-check-timeout-seconds 5 \
--healthy-threshold-count 2 \
--unhealthy-threshold-count 3 \
--matcher HttpCode=200-299A new target starts in initial state. It becomes healthy after healthy-threshold-count consecutive passes, which takes at least 2 × 15 = 30 seconds here. It becomes unhealthy after unhealthy-threshold-count consecutive failures. An unhealthy target is removed from rotation, and ECS stops and replaces the task.
The load balancer checks the response status against the matcher. A /health path that redirects with 302 to a login page fails a 200-299 matcher. A path that needs authentication returns 401 and fails too.
What the grace period does
A service that uses a load balancer can set healthCheckGracePeriodSeconds. While it runs, ECS ignores unhealthy target health results for that task. The ALB still does not send traffic to the task until it passes. The grace period only stops ECS from killing the task for failing before it has finished starting.
Key facts:
- The grace period starts when the task reaches the
RUNNINGstate. - It applies to the load balancer's health result. The container health check has its own
startPeriod. - It is only valid on services with a load balancer. The default is 0.
- A task that passes the target group check early is not penalised. The grace period only protects against early failures.
Without a grace period, a JVM or Rails app that takes 90 seconds to boot can fail three ALB checks in 45 seconds. ECS then stops it, starts a replacement, and the cycle repeats. The service never stabilises.
aws ecs update-service \
--cluster demo \
--service web \
--health-check-grace-period-seconds 120Choosing a value
A workable estimate is:
grace period ≥ app startup time + (interval × healthy threshold) + marginFor an app that needs 45 seconds to start, with a 15-second interval and a threshold of 2, the minimum is 45 + 30 = 75 seconds. A grace period of 120 leaves a margin. Check your real startup time in the logs rather than guessing.
Do not set it to the maximum. During the grace period a genuinely broken task is not replaced, and a deployment with a bad image takes longer to fail.
How health checks interact with deployments
During a rolling update, ECS starts new tasks and waits for them to become healthy before stopping old ones. A new task that never passes the ALB check blocks the rollout. With the deployment circuit breaker enabled, repeated task launch failures or failed health checks mark the deployment as failed and trigger a rollback. A correct health check path is what lets the circuit breaker detect a bad release.
When ECS stops an old task, the ALB keeps the target in draining state for the deregistration delay, which is 300 seconds by default. For stateless apps with short requests this slows deployments for no benefit:
aws elbv2 modify-target-group-attributes \
--target-group-arn arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/web/0123456789abcdef \
--attributes Key=deregistration_delay.timeout_seconds,Value=30Common errors
- Security group blocks the ALB. The task's security group must allow inbound traffic on the container port from the ALB's security group. Symptom: targets stay
unhealthywith "Request timed out". - App listens on localhost. Bind to
0.0.0.0. Symptom: the container check passes and the ALB check fails with a connection error. - Wrong port or path. The health check port defaults to
traffic-port, which is the container port from the load balancer mapping. A mismatch with the app's real port gives timeouts. - Missing
curl. The container check exits with "command not found", the container goesUNHEALTHY, and tasks restart in a loop. - Grace period too short. Tasks are replaced repeatedly with the service event "Task failed ELB health checks".
- Deep health checks. A
/healthendpoint that queries the database makes every task fail together when the database has a brief outage. The service then replaces every task at once. Keep the load balancer check shallow and report dependency problems through metrics and alarms.
Exercise
A service has a 20-second ALB health check interval, a healthy threshold of 3, and an unhealthy threshold of 2. The app takes 60 seconds to start. The grace period is 0, and the service keeps cycling tasks. What is the minimum grace period you would set, and what else could still stop the tasks?
Show answer
The minimum is 60 + (20 × 3) = 120 seconds, so something like 150 gives a margin. If the image has a container health check, its startPeriod must also cover the 60-second boot, or the container is marked UNHEALTHY and ECS stops the task regardless of the grace period.