Target Groups and Health Checks
Target groups route traffic to tasks and drop ones that fail health checks.
A target group is the set of task IPs the load balancer sends traffic to. For awsvpc tasks the target type must be ip.
Health checks
The target group pings a path (e.g. /health) on each task. A task must pass a number of checks to become healthy and start receiving traffic; consecutive failures mark it unhealthy and it is drained.
ECS uses these results to decide whether a new deployment is stable.
Example
aws elbv2 create-target-group \
--name web-tg \
--protocol HTTP --port 80 \
--vpc-id vpc-0abc123 \
--target-type ip \
--health-check-path /health \
--healthy-threshold-count 2 \
--unhealthy-threshold-count 3When to use it
- An ops engineer sets a longer deregistration delay on the target group so ECS tasks have 60 seconds to finish in-flight requests before being removed from service.
- A developer adjusts the health check threshold from 5 to 2 consecutive successes so newly deployed tasks are marked healthy and receive traffic faster.
- A team configures separate target groups for blue and green ECS services to enable a CodeDeploy traffic shift with instant rollback capability.
More examples
Tune Health Check Settings
Tunes the health check to require only 2 consecutive successes before marking a task healthy, reducing the time new tasks wait before receiving traffic.
aws elbv2 modify-target-group \
--target-group-arn arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/api-tg/abc \
--health-check-interval-seconds 15 \
--healthy-threshold-count 2 \
--unhealthy-threshold-count 3 \
--health-check-timeout-seconds 5Set Deregistration Delay
Sets a 60-second deregistration delay so the ALB gives stopping tasks time to finish in-flight requests before removing them from the target group.
aws elbv2 modify-target-group-attributes \
--target-group-arn arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/api-tg/abc \
--attributes Key=deregistration_delay.timeout_seconds,Value=60Check Target Health Status
Lists every registered target (task ENI IP) in the group with its current health state and reason code to diagnose unhealthy tasks quickly.
aws elbv2 describe-target-health \
--target-group-arn arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/api-tg/abc \
--query 'TargetHealthDescriptions[*].{ip:Target.Id,port:Target.Port,state:TargetHealth.State,reason:TargetHealth.Reason}' \
--output table
Discussion