A developer is migrating a REST API to Amazon API Gateway with an AWS Lambda backend. The developer configures a new resource with a GET method using Lambda Proxy Integration. The Lambda function executes successfully and returns the following JSON response:
{
"status": "success",
"data": {
"items": ["item1", "item2"]
}
}
However, when testing the API endpoint via a client, the client receives an HTTP Bad Gateway status code. The CloudWatch execution logs for API Gateway show: "Malformed Lambda proxy response" and "Execution failed due to configuration error". What is the root cause of this error, and how should the developer resolve it?
- The Lambda function must return a JSON response with specific keys, including `statusCode` and a stringified `body`. The developer should modify the Lambda function to return a structured payload containing these fields.Cevap
- BThe developer must configure an Integration Response mapping template in API Gateway to map the custom JSON properties returned by the Lambda function to the HTTP Method Response.
- CThe developer must configure a Lambda Authorizer to validate the format of the payload returned by the Lambda function before it is passed to the Method Response.
- DThe Lambda function returned a JSON object, but Lambda Proxy Integration requires the response body to be base64-encoded. The developer should modify the function to set `isBase64Encoded` to `true` and base64-encode the JSON payload.
Cevap
The Lambda function must return a JSON response with specific keys, including `statusCode` and a stringified `body`. The developer should modify the Lambda function to return a structured payload containing these fields.
In Lambda Proxy Integration, API Gateway does not map or modify the response from the backend. The backend Lambda function is directly responsible for generating the complete HTTP response. It must return a JSON object with a specific structure: an integer `statusCode`, and a stringified JSON `body`. The custom JSON object returned by the function does not match this structure, which triggers a Bad Gateway error in API Gateway.
Adım Adım Çözüm
Anahtar Kavram
Lambda Proxy Integration Response Format
Tahmini Süre:2m 0s