A developer is troubleshooting a Vue.js single-page application that is receiving a 502 Bad Gateway error when calling a POST endpoint on an Amazon API Gateway REST API. The API uses a Lambda proxy integration with a backend AWS Lambda function. In the Amazon CloudWatch logs, the developer confirms that the Lambda function executed successfully and returned the following JSON object:
{
"status": "success",
"data": {
"orderId": "78910"
}
}
Which of the following describes the root cause and the correct resolution for this error?
- AThe integration is missing a mapping template in API Gateway. The developer must configure an Integration Response mapping template to transform the custom JSON keys from the Lambda output into a standard client HTTP response.
- BThe API Gateway resource lacks CORS configuration. The developer must configure the CORS preflight response headers directly on the Amazon S3 bucket hosting the Vue.js application to allow cross-origin requests.
- The Lambda proxy integration requires the function's output to be a JSON object containing an integer 'statusCode' and a stringified JSON 'body'. The developer must modify the Lambda function's response payload to return this format.Answer
- DThe Lambda function returned an object structure. The developer must modify the Lambda function to return the response payload as a raw, unformatted JSON string directly from the handler so API Gateway can forward it.
Answer
The Lambda proxy integration requires the function's output to be a JSON object containing an integer 'statusCode' and a stringified JSON 'body'. The developer must modify the Lambda function's response payload to return this format.
In a Lambda proxy integration, API Gateway expects the backend Lambda function to return a specific JSON response format containing an integer 'statusCode' and a string 'body'. If the backend Lambda function returns a custom JSON object instead of this format, API Gateway fails to parse the response and returns a 502 Bad Gateway error to the client. Modifying the Lambda function to return the correct structure resolves this integration issue.
Step-by-Step Solution
Key Concept
API Gateway Lambda Proxy Integration Response Formatting
Estimated Time:1m 30s