Skip to content

Cloudfront 504 gateway Timeout

0

I have my backend application running in an ECS Task ( 1 task and 1 container) , I have setup a Cloudfront distribution to serve my backend , I have also attached a load balancer to forward request to my ecs-target-group ( EC2 Launch Type) , So request hit cloudfront and cloudfront forward requests to load balancer in case of cache miss. The issue is requests have random responses between 200 OK and 504 gateway timeout.

CloudFront attempted to establish a connection with the origin, but either the attempt failed or the origin closed the connection.

This is sample Access logs from load balancer logs

http 2024-10-24T07:23:32.408384Z app/<load-balancer-id> <cloudfront-ip>:35600 <ECS-Instance-Private-IP>:8080 0.000 0.114 0.000 200 200 623 1846 "GET http://<cloudfront-distribution>:80/<endpoint> HTTP/1.1" "PostmanRuntime/7.42.0" - - <arn-of-load-balancer> "Root=<AWS-xray-Trace_ID>" "-" "-" 0 2024-10-24T07:23:32.293000Z "forward" "-" "-" "<ECS-Instance-IP>:8080" "200" "-" "-" TID_<transaction_ID>

This is sample connection logs from load balancer

2024-10-24T07:23:32.293303Z <Cloudfront_Ip_address> 35600 80 - - - "-" - - - <Transaction_ID> I have checked the metrics as per the below image and i can see that the CPU Consumption hit the resource limits i.e. 1.93

Enter image description here

Does this mean that failure of cloudfront to connect to its origin (i.e. loadbalancer) fail and succeed randomly due to resource limits ?

am using a t3a.medium instance with 4 GIBs of Ram and 2 vCPUs and only running 1 task with 1 container

2 Answers
1

Based on the information provided, it appears that the random responses between 200 OK and 504 Gateway Timeout are likely related to the resource constraints on your ECS task. The high CPU consumption you've observed (hitting 1.93 out of 2 vCPUs) suggests that your application is experiencing performance issues due to resource limitations.

When the CPU usage is near its limit, it can cause the application to respond slowly or fail to respond within the expected timeframe. This can lead to CloudFront timing out while waiting for a response from the origin (your load balancer and ECS task), resulting in the 504 Gateway Timeout errors.

The successful 200 OK responses likely occur when the CPU usage momentarily drops or when the request happens to be processed during a less resource-intensive moment. The random nature of the responses suggests that your application is struggling to handle the incoming traffic consistently due to the resource constraints.

To address this issue, you should consider the following steps:

  1. Increase the resources allocated to your ECS task. Since you're using a t3a.medium instance with 2 vCPUs and 4 GB of RAM, you might want to consider upgrading to a larger instance type or increasing the number of tasks to distribute the load.

  2. Optimize your application code to reduce CPU usage if possible.

  3. Implement auto-scaling for your ECS service to automatically add more tasks when the CPU usage is high.

  4. Review your CloudFront and load balancer settings to ensure they are configured correctly. For example, check if the origin response timeout in CloudFront is set appropriately.

  5. Analyze your application logs to identify any specific operations or requests that might be causing high CPU usage.

  6. Consider implementing a caching strategy at the CloudFront level to reduce the load on your origin server for frequently requested content.

By addressing the resource constraints and optimizing your setup, you should be able to reduce the occurrence of 504 Gateway Timeout errors and improve the overall consistency of your application's responses.
Sources
HTTP 504 status code (Gateway Timeout) - Amazon CloudFront
HTTP 502 status code (Bad Gateway) - Amazon CloudFront
HTTP 500 status code (Internal Server Error) - Amazon CloudFront

answered 2 years ago

AWS
EXPERT

reviewed 2 years ago

0
Accepted Answer

Thanks for your feedback , turned out that issue was related to the load balancer configuration, By default it has to be in two different subnets (i.e. AZs) ,one of the subnets has a NAT gateway attached , which by default block inbound connections over the internet, so cloudfront couldn't establish connection with loadbalancer over the internet , But allow outbound connection through the NAT Gateway Instance.

So, Whenever cloudfront tries to forward requests to loadbalancer , If it forward the request to the loadbalncer in the subnet which has the NAT gateway attached , the request shall not reach the origin, that 's why the requests were successful occasionally based on the subnet(Az) in which the loadbalancer is processing the request.

so by replacing the subnet with the NAT gateway with another subnet that has an Internet gateway attached fixed the issue.

Request is as follows Client -> Cloudfront-> load balancer-> ECS-Target-Group(EC2 Launch Type)

answered 2 years ago

EXPERT

reviewed 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.