AWSMid

Application Load Balancer 502, 503 and 504 errors

502, 503 and 504 from an ALB each point to a different problem between the load balancer and the targets. Match the code, then check health checks and timeouts.

What you see

  • HTTP 502: Bad gateway
  • HTTP 503: Service unavailable
  • HTTP 504: Gateway timeout

What it means

These errors are generated by the load balancer, not your app. They are counted in the HTTPCode_ELB_5XX_Count metric. Errors your targets return themselves are counted separately in HTTPCode_Target_5XX_Count. That split tells you which side to look at first.

How to confirm the cause

  1. 1
    See target health and the failure reason
    aws elbv2 describe-target-health --target-group-arn TARGET_GROUP_ARN

    The console resource map labels failures as Unhealthy: HTTP Response Mismatch, Unhealthy: Request timed out and Unhealthy: FailedHealthChecks. The docs list the full set under health check reason codes.

  2. 2
    Check the load balancer access logs
    # Enable access logs on the load balancer and read the error reason code on failing requests.

    The docs point to the access log error reason codes for deeper detail.

Causes and fixes

  • 502: target closed the connection or sent a bad response

    HOW TO TELL
    The target sent a TCP RST or FIN with a request outstanding, or the response was malformed.
    FIX
    Make sure the target's keep-alive timeout is longer than the load balancer's idle timeout, and that traffic is allowed from the load balancer subnets to the target port. Also check the target is not returning invalid headers.
  • 502: Lambda target problem

    HOW TO TELL
    The target is a Lambda function that errored, timed out or returned a body over 1 MB.
    FIX
    Fix the function error, raise its timeout, or shrink the response.
  • 503: no usable targets

    HOW TO TELL
    The target groups have no registered targets, or all are unused.
    FIX
    Register targets and make sure they are in the target group the listener rule routes to.
  • 504: could not connect or the target was too slow

    HOW TO TELL
    Connection could not be established within 10 seconds, or the target did not answer before the idle timeout.
    FIX
    Check security groups and network ACLs allow traffic between the load balancer and targets, including the ephemeral ports 1024-65535. If the app is slow, raise the idle timeout or make the endpoint faster.
  • Targets are unhealthy

    HOW TO TELL
    The target shows Unhealthy: HTTP Response Mismatch, Unhealthy: Request timed out or Unhealthy: FailedHealthChecks.
    FIX
    For a response mismatch, make the app return the expected success code (200 by default) on the health check path, or change the expected codes. For a timeout, check security groups, ACLs, target capacity and use a simpler health check page.

How to stop it coming back

  • Create a cheap dedicated health check endpoint.
  • Set the app's keep-alive timeout above the load balancer idle timeout.
  • Alert on HTTPCode_ELB_5XX_Count separately from target errors.

Keep going

Sources

AWS documentation (docs.aws.amazon.com), read 18 Nov 2026.

Search

Search cheat sheets and projects