Free auditStart free
GuideError · CDN and hosting errors

AWS Lambda 502 Bad Gateway

A Lambda 502 is usually the function's return value breaking the proxy contract, a response over 1 MB, a timeout or an error. See the exact shapes and test them.

4 min
read
5
sources
Oct 31, 2026
updated
Scaplet Editorial Team
author
In one line

A 502 in front of a Lambda function means the proxy could not use what the function returned. API Gateway returns 502 when the output is not in its JSON format. An ALB lists three Lambda causes: a response body over 1 MB, a timeout, and a function error or throttling. Return the documented shape and keep it small.

30-second check

Where does your site stand?

Four quick questions, then a straight answer.

1/4Have you checked your own site for this?
Summarize this page withChatGPTClaudePerplexityGoogleAI Mode
01What each caller documents as a Lambda 502

A 502 here is about the return value

Lambda itself does not speak HTTP. A proxy in front of it translates the function's return value into a response, and a 502 means the translation failed. Which proxy you use changes the rules, so start there. The general AWS 502 causes for load balancers and CloudFront live on the AWS 502 page; this page covers only what is specific to Lambda.
Caller

API Gateway, Lambda proxy integration

502 trigger in the AWS docs
The function output is in a different format from the one API Gateway requires
Caller

Application Load Balancer, Lambda target

502 trigger in the AWS docs
The response body exceeds 1 MB
Caller

Application Load Balancer, Lambda target

502 trigger in the AWS docs
The function did not respond before its configured timeout was reached
Caller

Application Load Balancer, Lambda target

502 trigger in the AWS docs
The function returned an error, or the Lambda service throttled it
Where the translation happens
client --> API Gateway or ALB --> Lambda function
                 ^                      |
                 '---- return value ----'   not a valid response object -> 502
03Fix it on your stack

Check it in 2 minutes

Call the function directly, bypassing the proxy, and read what it returns. If the direct result is not a response object, the proxy has nothing valid to translate. The first command is a public test endpoint for the status shape; the second runs a handler on your machine; the third needs your AWS account.
Public test endpoint: read a 502 status
curl -sS -o /dev/null -w "%{http_code}\n" https://httpbin.org/status/502
# observed: 502
Local run of a good and a broken handler under Node (run on 2026-11-01)
{"isBase64Encoded":false,"statusCode":200,"statusDescription":"200 OK","headers":{"Content-Type":"application/json"},"body":"{\"ok\":true,\"path\":\"/health\"}"}
"hello"
Your account: invoke the function directly (AWS CLI v2; needs your function)
aws lambda invoke response.json \
    --function-name my-function \
    --cli-binary-format raw-in-base64-out \
    --payload '{ "path": "/health" }'
cat response.json
# A response object, like the first line above, is correct.
# A bare string like "hello" has no statusCode and is not the documented shape.

The first line is the object a proxy can use. The second is what a handler returns when it forgets to wrap the value: a bare string. Based on the AWS docs, that is the case API Gateway rejects with 502. The aws lambda invoke form is taken from the AWS CLI reference, and AWS notes --cli-binary-format is required on CLI version 2. If the invoke result contains a FunctionError, the function itself failed, which is the other documented route to a 502 on an ALB.

04Symptom, cause and fix

Fixes by symptom

Symptom

Direct invoke returns a string, array or object with no statusCode

Cause
The handler returns data, not a response
Fix
Wrap the value as in the handler above
Symptom

Works for small requests, 502 for large ones through an ALB

Cause
Maximum response JSON from the function is 1 MB
Fix
Return a link to the data, for example an object in S3, instead of the data itself
Symptom

502 after a fixed delay

Cause
The function ran to its configured timeout
Fix
Shorten the work or raise the timeout. See Lambda timeout
Symptom

502 with FunctionError in the invoke result, or throttling

Cause
The function returned an error or was throttled
Fix
Fix the exception from the logs, or raise concurrency
Symptom

Binary file arrives corrupted

Cause
isBase64Encoded is false, or */* is not a Binary Media Type on API Gateway
Fix
Encode the body, set the flag, and configure the media type
A response object is right when
  • The function sits behind API Gateway Lambda proxy or an ALB
  • You want to control status and headers
  • The body is under 1 MB
Use another route when
  • The payload is large or binary and changes rarely
  • You need streaming or long-running work
  • A caller other than a proxy invokes the function and wants plain data
05Explained

What a Lambda 502 means for SEO

When a function renders pages, a bad return value makes each of those URLs answer 502 at once. Search engines read that as a server fault: Google says its crawlers slow down on 5xx, and URLs it already indexed are kept only until the errors have lasted long enough. A format bug shows up the moment you deploy it, so put the direct invoke from the section above into your release checks. See 5xx server errors and API Gateway 504.
Questions

Asked a lot.

3 questions people ask next, answered from the sources on this page.

01Why does API Gateway return 502 for my Lambda function?

AWS documents one cause for the proxy integration: the function output is in a different format from the one API Gateway requires. Return an object with statusCode, and optionally headers, multiValueHeaders, body and isBase64Encoded.

02What is the Lambda response size limit behind an ALB?

1 MB. The ALB docs give the maximum size of the response JSON the function can send as 1 MB, and list a response body over 1 MB as a 502 cause.

03Can the response body be empty?

Behind an ALB, yes: AWS says the body can be omitted, while the Base64 encoding status, status code and headers must be present. Behind API Gateway, return the documented format and include body when you have content.

Sources · 5

Every fact, linked and dated.

Official docs first. The date is when we last opened each page.
  • amazon.com
  • google.com
  1. 01AWS: Lambda functions as targets for Application Load Balancersdocs.aws.amazon.com · checked 2026-11-01
  2. 02AWS: Set up Lambda proxy integrations in API Gatewaydocs.aws.amazon.com · checked 2026-11-01
  3. 03AWS: Troubleshoot your Application Load Balancersdocs.aws.amazon.com · checked 2026-11-01
  4. 04AWS CLI reference: lambda invokedocs.aws.amazon.com · checked 2026-11-01
  5. 05How HTTP status codes affect Google's crawlersdevelopers.google.com · checked 2026-11-01
Free · 2 minutes

Tools point. Agencies advise. This one ships.

Run the free audit, then approve the fixes. Every change can be undone.

Free, no card60 secondsNobody will call you
  • 46 fixes shipped without a ticket Brightpipe
  • 0→7 ChatGPT citations in six weeks Paylane