Soru

Zorluk: OrtaTroubleshooting API Gateway Errors and CORS

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?

  1. A
    The 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.
  2. B
    The 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.
  3. 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.Cevap
  4. D
    The 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.

Cevap

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.

Adım Adım Çözüm

1
Analyze the error message and execution logs.
The client receives a 502 Bad Gateway error, but CloudWatch logs confirm that the Lambda function completed execution successfully. This points to a communication or parsing error between API Gateway and Lambda.
Checking both client-side errors and backend logs helps distinguish between a Lambda function crash and an integration parsing failure.
2
Identify the integration type mapping requirements.
The API utilizes a Lambda proxy integration. Unlike custom integrations, proxy integrations do not support Integration Response mapping templates in API Gateway.
Understanding the difference between Lambda custom and Lambda proxy integrations dictates where the payload formatting must be performed.
3
Modify the Lambda function handler output format.
The developer updates the Lambda function to return an object structured with 'statusCode' (integer) and 'body' (stringified JSON).
API Gateway requires this specific contract to successfully construct the HTTP response for the client.

Anahtar Kavram

API Gateway Lambda Proxy Integration Response Formatting
Tahmini Süre:1m 30s
Bu soruyu puanla