Resolving AWS API Gateway 502 Bad Gateway Error

If you’ve been using AWS API Gateway, you might have encountered a 502 Bad Gateway error. This error typically means that a server upstream, which is acting as a gateway or proxy, has received an invalid response from an inbound server it accessed while trying to fulfill a request. In terms of AWS API Gateway, this means there’s likely a problem with your Lambda function or the API Gateway service itself.

What Causes a 502 Bad Gateway Error?

A 502 error code is related to the service your API Gateway integrates with. It means that API Gateway couldn’t understand the response

It can occur due to several reasons:

  • An issue with the Lambda function – if the function is not returning a response that fits the format expected by the API Gateway, the API Gateway is unable to parse the response, leading to an error.
  • Issue with EC2 or ECS/EKS – it could be that API Gateway can’t connect to the VM or container because they aren’t running (correctly).
  • Problem with the API Gateway – sometimes, an issue with the API Gateway service can lead to this error.

How Do I Fix 502 Bad Gateway on AWS?

Lambda function issues

If the Lambda function is the issue, you would need to verify whether the function is returning a response that complies with the format expected by the API Gateway. Lambda functions should return an object that includes statusCodeheaders, and body properties.

Suppose the Lambda function returns something like "Hello from Lambda!". The API Gateway would not be able to parse this response as it doesn’t match the expected format, and thus, a 502 Bad Gateway error occurs.

Instead, the Lambda function should return in JSON format:

{
    "statusCode": 200,
    "body": "Hello from Lambda!"
}

API Gateway issues

If the 502 Bad Gateway error is caused by an issue with the API Gateway service, you would need to contact AWS support for further assistance.

However, you can also check the following:

  1. Monitor the AWS Service Health Dashboard for any potential incident impacting the API Gateway service.
  2. Validate the API Gateway configuration, ensure it’s correctly set to interact with the respective resources.

For more large scale API Gateway Debugging and Troubleshooting you can use Dashbird.

Read our blog

ANNOUNCEMENT: new pricing and the end of free tier

Today we are announcing a new, updated pricing model and the end of free tier for Dashbird.

4 Tips for AWS Lambda Performance Optimization

In this article, we’re covering 4 tips for AWS Lambda optimization for production. Covering error handling, memory provisioning, monitoring, performance, and more.

AWS Lambda Free Tier: Where Are The Limits?

In this article we’ll go through the ins and outs of AWS Lambda pricing model, how it works, what additional charges you might be looking at and what’s in the fine print.

More articles

Made by developers for developers

Dashbird was born out of our own need for an enhanced serverless debugging and monitoring tool, and we take pride in being developers.

What our customers say

Dashbird gives us a simple and easy to use tool to have peace of mind and know that all of our Serverless functions are running correctly. We are instantly aware now if there’s a problem. We love the fact that we have enough information in the Slack notification itself to take appropriate action immediately and know exactly where the issue occurred.

Thanks to Dashbird the time to discover the occurrence of an issue reduced from 2-4 hours to a matter of seconds or minutes. It also means that hundreds of dollars are saved every month.

Great onboarding: it takes just a couple of minutes to connect an AWS account to an organization in Dashbird. The UI is clean and gives a good overview of what is happening with the Lambdas and API Gateways in the account.

I mean, it is just extremely time-saving. It’s so efficient! I don’t think it’s an exaggeration or dramatic to say that Dashbird has been a lifesaver for us.

Dashbird provides an easier interface to monitor and debug problems with our Lambdas. Relevant logs are simple to find and view. Dashbird’s support has been good, and they take product suggestions with grace.

Great UI. Easy to navigate through CloudWatch logs. Simple setup.

Dashbird helped us refine the size of our Lambdas, resulting in significantly reduced costs. We have Dashbird alert us in seconds via email when any of our functions behaves abnormally. Their app immediately makes the cause and severity of errors obvious.