michaelhly's blog

logs.blobs.lol/five

From AWS documentation on implementing health checks:

Health checks are a way of asking a service on a particular server whether or not it is capable of performing work successfully. Load balancers ask each server this question periodically to determine which servers it is safe to direct traffic to.

Okay, so why does my LB target group tell me that my that my target is healthy:

Load Balancer Health Check

But on ECS, it says my container is UNHEALTHY: ECS Health Check

???????


Turns out, ECS's health check status is not the same as my load balancer's health check status. ECS's health check is performed by Amazon's ECS container agent, which runs automated checks on every running container instance. The container agent is automatically installed by AWS on Amazon ECS-optimized Amazon Machine Images (AMIs).

From AWS ECS documentation on HealthCheck:

The Amazon ECS container agent only monitors and reports on the health checks specified in the task definition.

Here is the health check I defined in my task definition:

{
      // ...
      "healthCheck": {
        "retries": 3,
        "command": [
          "CMD-SHELL",
          "curl -f http://localhost:${PORT}/api/health || exit 1"
        ],
        "timeout": 5,
        "interval": 30,
        "startPeriod": 30
      },
      // ...
}

Oh, looks like curl is not packaged with the Alpine Linux image hosted by Docker.

I need to change my container health check to:

{
      // ...
      "healthCheck": {
        "retries": 3,
        "command": [
          "CMD-SHELL",
          "wget --no-verbose --tries=1 --spider http://localhost:${PORT}/api/health || exit 1"
        ],
        "timeout": 5,
        "interval": 30,
        "startPeriod": 30
      },
      // ...
}

Now my ECS task is healthy and my container is deployed properly 🙂.

#AWS #ECS #bloblogs #health checks