Sub topic 12
Health checks and the grace period
This chapter assumes you can read a task definition and an ECS service command, and that you know what an Application Load Balancer (ALB) target group is. Two separate health checks decide whether a Fargate task keeps running, and they are easy to confuse.
Two checks, two decisions
- Container health check. It is defined in the task definition. ECS runs a command inside the container and marks the container
HEALTHYorUNHEALTHY. If an essential container becomes unhealthy, ECS stops the task and the service starts a replacement. - Load balancer health check. It is defined on the target group. The ALB sends an HTTP request to each task's IP address and port. A target that fails is removed from rotation, and the ECS service may also replace it.
The container check answers "is the process working?". The ALB check answers "can traffic reach it?". Both can fail independently, so configure both.
The container health check
The task definition from the earlier chapter includes this block:
"healthCheck": {
"command": ["CMD-SHELL", "curl -f http://localhost:8080/health || exit 1"],
"interval": 30,
"timeout": 5,
"retries": 3,
"startPeriod": 30
}intervalis the number of seconds between checks.timeoutis how long one check may run before it counts as a failure.retriesis how many consecutive failures mark the container unhealthy.startPeriodis a window after container start during which failures do not count towardretries. A success in this window still marks the container healthy.
The command runs inside the container, so the image must contain curl or whatever tool you call. A minimal image without curl fails every check, and the task is killed in a loop.
The grace period
When you attach a load balancer, a new task is registered in the target group before the application may be ready. If the ALB check fails during startup, the service scheduler can stop the task as unhealthy before it finishes booting. The service setting --health-check-grace-period-seconds tells ECS to ignore ALB health check results for that many seconds after a task starts.
aws ecs update-service \
--cluster demo \
--service web \
--health-check-grace-period-seconds 90Set it a little above your slowest normal startup time. Too short, and slow-starting tasks are killed repeatedly. Too long, and a truly broken task stays in the service longer before it is replaced.
Common errors
| Symptom | Likely cause |
|---|---|
| Tasks restart every few minutes during deploys | Grace period shorter than startup time |
| Container always `UNHEALTHY` | Health check tool missing from the image |
| Target group shows `unhealthy` with a timeout | Security group does not allow ALB to reach the container port |
| Target group shows a 404 or 301 mismatch | Health check path wrong, or the matcher does not accept the response code |
Exercise
A new service's tasks start, run for about 40 seconds, then are stopped and replaced. The application needs 60 seconds to start and the grace period is 30 seconds. What is the fix?
Show answer
The ALB check fails while the app is booting, and the 30-second grace period ends before startup completes, so ECS stops the task. Raise --health-check-grace-period-seconds to a value above 60, such as 90. Also set startPeriod in the container health check to at least 60.