Fix AWS Error 504 Gateway Timeout On ALB And API Gateway (2025)
Updated: 11/29/2025
You call your application endpoint on AWS, the browser or client hangs for what feels like forever, and then you get a 504 Gateway Timeout instead of a proper response. It feels random because sometimes the same URL works fine and other times it dies after a long wait. On AWS this usually means the load balancer or API Gateway could not get a response from your backend in time, either because of slow code, exhausted resources, or mismatched timeout settings between services.
Table of Contents
Method 1: Use CloudWatch Logs And Traces To Find The Slow Hop
Before changing settings blindly you need to know exactly where the request is getting stuck. On AWS the real cause is often hidden in CloudWatch logs, X Ray traces, or service specific logs like Lambda or ECS, which show whether the backend is slow, erroring, or never even reached.
Step 1: Enable Access Logging For The Load Balancer Or API Gateway
In the AWS Console open EC2 then Load Balancers, select your Application Load Balancer, and turn on Access logs to an S3 bucket if they are not already enabled. For API Gateway, enable Execution logging and detailed metrics in the Stages settings so you see latency and integration response data.
Step 2: Inspect CloudWatch Logs For Backend Errors Or Long Latency
Open CloudWatch Logs and find the groups for your Lambda functions, ECS tasks, or EC2 applications. Filter by the timestamp of a 504 event and look for requests with very high processing time, database timeouts, or unhandled exceptions, since these are prime suspects for causing the gateway to give up.
Step 3: Use X Ray Or Distributed Tracing Where Possible
If your architecture supports it, enable AWS X Ray or another tracing tool on Lambda, containers, and databases. Review traces for transactions that line up with the 504, paying close attention to which downstream service, such as RDS or DynamoDB, is consuming most of the end to end latency.
Method 2: Tune Timeouts And Health Checks Between Layers
For ALB target groups, ensure that the Health check path returns a fast and stable 200 style status code and that the Healthy threshold and Timeout values are realistic. Misconfigured health checks can cause targets to flap between healthy and unhealthy, leading to timeouts when traffic is routed to instances that are actually struggling.
Method 3: Fix Backend Performance Bottlenecks And Network Paths
If users are far from your backend region, consider placing API Gateway or ALB behind CloudFront to terminate TLS closer to them and smooth out network variability. For global applications use regional endpoints rather than a single far away region to reduce latency and the chance that cross region delays cause apparent 504 issues.