ECS container health check not configured

Configure a container health check that tests actual application health.

Description

To have ECS report application health, define a HealthCheck on the essential container in the task definition. ECS does not report checks defined only in the image.

Potential impact

Without another check, an unresponsive container can remain undetected, delaying replacement.

Remediation

Choose a command that runs inside the container and check its exit code for healthy and failed responses. Set the interval, timeout and retries to suit the application’s startup time and response latency.

Examples

The excerpts omit other task properties. AppImage needs a shell, curl and an application listening at localhost:8080/. The check runs every 30 seconds, times out after five and marks the container unhealthy after three consecutive failures.

Before

yaml
Resources:
  TaskDefinition:
    Type: AWS::ECS::TaskDefinition
    Properties:
      ContainerDefinitions:
        - Name: app
          Image: !Ref AppImage
          Essential: true

After

yaml
Resources:
  TaskDefinition:
    Type: AWS::ECS::TaskDefinition
    Properties:
      ContainerDefinitions:
        - Name: app
          Image: !Ref AppImage
          Essential: true
          HealthCheck:
            Command:
              - CMD-SHELL
              - curl -f http://localhost:8080/ || exit 1
            Interval: 30
            Retries: 3
            Timeout: 5

References