How to Fix a 502 Bad Gateway Error from API Gateway and Lambda?
Short answer
C Change the format of the Lambda function response to the API call.
Question
Key Takeaways
The exam tests whether you know that 502 in API Gateway + Lambda points to a malformed Lambda response, not to the request payload, endpoint protocol, or authorization.
A 502 Bad Gateway error from Amazon API Gateway behind a Lambda proxy integration means Lambda returned a malformed response that API Gateway cannot parse. The community unanimously (100%) agrees the fix is reformatting the Lambda function's response to the required JSON structure with statusCode, headers, and a stringified body.
Common mistake: Choosing B (change the payload sent to API Gateway) because candidates assume the incoming request is malformed; in reality, a 502 is generated when Lambda's response does not match the proxy integration format.
Expert Analysis
Why the Answer Is Correct
With Lambda proxy integration, API Gateway expects Lambda to return a JSON object containing "statusCode", "headers", and a stringified "body". If Lambda returns anything else — a plain string, a non-stringified body, or missing keys — API Gateway cannot map it and returns a 502 Bad Gateway to the caller. Comment [1] correctly states that a 502 means API Gateway was unable to get a valid response from Lambda, and comment [3] shows the exact required response shape. Reformatting the Lambda response (option C) directly resolves the error.
Why the Other Options Are Wrong
Option A is irrelevant because API Gateway integrations with AWS services already use HTTPS endpoints, and the protocol of the endpoint is not what triggers a 502. Option B addresses the request payload sent to API Gateway, but a 502 is a response-side failure, not a request-side one. Option D would produce 401/403 authorization errors, not a 502, since the log shows the method completed and the failure occurred when processing Lambda's output.
Community Comment Notes
All four comments vote C, giving this question a 100% consensus. Comment [2] is the most valuable because it links the official AWS Knowledge Center article on malformed 502 errors and advises reviewing the Lambda response format in the logs. Comment [3] reinforces this by providing the canonical response structure: { "statusCode": 200, "headers": {...}, "body": "{\"message\": \"Success\"}" }.
Official Reference
- https://repost.aws/knowledge-center/malformed-502-api-gateway
- https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
These links open the official vendor documentation in a new tab.
Exam Strategy
Whenever a DVA-C02 question pairs a 502 error with API Gateway and Lambda, immediately think 'malformed Lambda proxy response' and look for the option about fixing the Lambda response format. Eliminate request-side distractors like payloads or authorization headers, since auth failures return 4xx codes and 502 specifically indicates Lambda's output could not be parsed.
Community Votes
100% of anonymous learners picked answer C. Votes are pick records left by other test-takers — they are not the verified answer.
Discussion 4 contributions
Compiled from public community threads and reader submissions, lightly edited for clarity. Handles and vote tallies are shown as recorded, and comments reflect the views of individual contributors.
From public community threads 4
-
aws_god 👍 1 Answered CIn the logs, review the format of your Lambda function's response to your API. If the response isn't in the required JSON format, then reformat it. https://repost.aws/knowledge-center/malformed-502-api-gateway
-
albert_kuo 👍 1 Answered CCorrect response format should be { "statusCode": 200, "headers": { "Content-Type": "application/json" }, "body": "{\"message\": \"Success\"}" }
-
Saudis 👍 1 Answered CWhen you see a 502 Bad Gateway error in Amazon API Gateway, it means that the API Gateway was unable to get a valid response from Lambda.
-
YUICH 👍 1the answer is C
Reader comments & corrections
No reader corrections yet — spotted an error or have a note? Share it below.
Related Analysis
Practice All DVA-C02 Questions
Access 100 questions with complete answers and detailed explanations.
View Full DVA-C02 Practice Test →