How to Fix a 502 Bad Gateway Error from API Gateway and Lambda?

A developer is building an application integrating an Amazon API Gateway with an AWS Lambda function. When calling the API, the developer receives the following error: Wed Nov 08 01:13:00 UTC 2017 : Method completed with status: 502 What should the developer do to resolve the error?

  1. Change the HTTP endpoint of the API to an HTTPS endpoint.
  2. Change the format of the payload sent to the API Gateway.
  3. Change the format of the Lambda function response to the API call. Source Reference Answer
  4. Change the authorization header in the API call to access the Lambda function.

Community Votes

C
100%

100% of anonymous learners picked answer C. Votes are pick records left by other test-takers — they are not the verified answer.

Community Insight

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.

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.

Community Discussion (4 comments)

Saudis 👍 1 Selected: C
When 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.
aws_god 👍 1 Selected: C
In 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 Selected: C
Correct response format should be { "statusCode": 200, "headers": { "Content-Type": "application/json" }, "body": "{\"message\": \"Success\"}" }
YUICH 👍 1
the answer is C

Comments & Corrections

No comments yet — spotted an error or have a note? Share it below.

Log in to comment, report an error, or add a note about this question.

Submitted for moderation before publishing. Keep it helpful and respectful.

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

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.

Related Analysis

Practice All DVA-C02 Questions

Access 100 questions with complete answers and detailed explanations.

View Full DVA-C02 Practice Test →

← Back to DVA-C02 Study Guide