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
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.
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.
Four quick questions, then a straight answer.
From your answers, this is quietly costing you traffic and nobody has the hours to chase it. Scaplet finds every affected page, drafts the fixes and tracks how AI answers mention you.
A data platform shipped 46 fixes without a ticket.
This looks handled. Re-check after a redesign, a migration or a big content push; that is when it breaks.
Your site is small and you have the time. Work through the steps on this page, starting with the pages that get the most traffic.
client --> API Gateway or ALB --> Lambda function
^ |
'---- return value ----' not a valid response object -> 502headers and multiValueHeaders can be unspecified. An ALB says the response must include the Base64 encoding status, status code and headers, and that you can omit the body. The safest habit is to return every field every time.isBase64Encodedtrue for binarytruestatusCodestatusDescription200 OKheadersmultiValueHeadersbodyexport const handler = async (event) => {
const body = JSON.stringify({ ok: true, path: event.path ?? '/' });
return {
isBase64Encoded: false,
statusCode: 200,
statusDescription: '200 OK',
headers: { 'Content-Type': 'application/json' },
body,
};
};The ALB also ignores hop-by-hop headers such as Connection and Transfer-Encoding, and you can omit Content-Length because the load balancer computes it. For binary bodies, Base64-encode the content and set isBase64Encoded to true. For API Gateway that also needs */* configured as a Binary Media Type.
curl -sS -o /dev/null -w "%{http_code}\n" https://httpbin.org/status/502
# observed: 502{"isBase64Encoded":false,"statusCode":200,"statusDescription":"200 OK","headers":{"Content-Type":"application/json"},"body":"{\"ok\":true,\"path\":\"/health\"}"}
"hello"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.
statusCodeFunctionError in the invoke result, or throttlingisBase64Encoded is false, or */* is not a Binary Media Type on API Gateway3 questions people ask next, answered from the sources on this page.
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.
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.
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.
Run the free audit, then approve the fixes. Every change can be undone.