ALB returns `502 Bad Gateway`. Where do you look?
IntermediateTroubleshootingEC2, Auto Scaling and Load Balancing
Answer
Likely causes → The target closed the connection, sent a malformed response, or reset during a request. Common reasons: the app crashed, or its keep-alive timeout is shorter than the ALB idle timeout (60s), so the target drops idle connections the ALB still reuses. A Lambda target that returns an invalid response also causes 502.
Diagnose →
aws cloudwatch get-metric-statistics --namespace AWS/ApplicationELB --metric-name HTTPCode_ELB_502_Count \
--dimensions Name=LoadBalancer,Value=app/web/abc123 --statistics Sum --period 60 \
--start-time $(date -u -d '-1 hour' +%FT%TZ) --end-time $(date -u +%FT%TZ)
aws elbv2 describe-load-balancer-attributes --load-balancer-arn <alb-arn>
# access logs: elb_status_code=502 with target_status_code="-" means no response from target
Fix →
- Set the app keep-alive timeout above the ALB idle timeout (for example, Node.js
server.keepAliveTimeout = 65000, Nginxkeepalive_timeout 75s) - Fix the app crashes and response headers
Prevent → Turn on ALB access logs to S3 and alarm on the ratio of HTTPCode_ELB_5XX_Count to request count.