Soru

Zorluk: OrtaTroubleshooting API Gateway Errors and CORS

A React Single Page Application (SPA) hosted on `https://portal.dev-ops-metrics.net` attempts to retrieve project status reports by sending an HTTP `GET` request to an Amazon API Gateway REST API. The API uses a Lambda proxy integration. Although the Lambda function executes successfully and returns a payload, the client application receives an HTTP `502 Bad Gateway` error with a response body of `{"message": "Internal server error"}`. The API Gateway CloudWatch execution logs display: `Execution failed due to configuration error: Malformed Lambda proxy response`. Which modification to the Lambda function's return payload will resolve this error?

  1. A
    Return a JSON object containing the `status` key with a numeric value and the `payload` key containing the raw data structure.
  2. B
    Return the raw data structure directly from the handler function, as API Gateway automatically wraps it in a standard HTTP response.
  3. Return a JSON object containing the `statusCode` key with an integer value and the `body` key containing a stringified JSON representation of the data.Cevap
  4. D
    Return a JSON object containing the `statusCode` key and a `headers` key containing the `Access-Control-Allow-Origin` header set to `*`, omitting the `body` key.

Cevap

The Lambda function must return a JSON object containing the `statusCode` key with an integer value and the `body` key containing a stringified JSON representation of the data.
In an Amazon API Gateway REST API with Lambda Proxy integration, the backend Lambda function is responsible for constructing the complete HTTP response. The response returned by the Lambda function must be a JSON object (or dictionary) with specific keys, including `statusCode` (which must be an integer or string representing one) and `body` (which must be a string, often a stringified JSON object). Returning a response in this exact format allows API Gateway to successfully parse the result and return a valid HTTP response to the client application.

Adım Adım Çözüm

1
Analyze the CloudWatch execution log error: `Execution failed due to configuration error: Malformed Lambda proxy response`.
Identify that the integration type is Lambda Proxy, which expects a specific JSON format from the backend Lambda function.
API Gateway requires the backend response to match a strict schema in proxy integrations to automatically construct the HTTP response.
2
Review the output structure required by API Gateway Lambda Proxy integration.
The output must contain the keys `statusCode` (an integer or stringified integer) and `body` (a stringified representation of the response data).
If these specific keys are missing or formatted incorrectly, API Gateway cannot parse the response and throws an HTTP 502 error.
3
Modify the Lambda function response return statement to conform to the required JSON schema.
Construct a response dictionary containing `statusCode` and a serialized JSON string in `body`, then return it.
This satisfies the API Gateway proxy schema, allowing it to correctly construct and return an HTTP 200 response to the client.

Anahtar Kavram

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