An e-commerce application uses a REST API in Amazon API Gateway to retrieve product inventory. The developer decides to transition the integration type of the GET method from a Lambda custom integration to a Lambda proxy integration. Following this change, clients receive an HTTP 502 Bad Gateway error. The Amazon CloudWatch logs for the integrated AWS Lambda function confirm that the function executes successfully and returns the raw inventory data. Which modification should the developer make to resolve this error?
- AConfigure a Velocity Template Language (VTL) mapping template in the API Gateway Integration Response to format the response payload.
- Format the Lambda function's output as a JSON object containing a `statusCode` integer, a `headers` object, and a stringified JSON `body`.Cevap
- CConfigure a custom Lambda authorizer in API Gateway to intercept the execution result and map it to the correct Method Response.
- DDefine the required response structure in the Method Response settings of the API Gateway console to automatically parse the raw Lambda return value.
Cevap
Format the Lambda function's output as a JSON object containing a `statusCode` integer, a `headers` object, and a stringified JSON `body`.
In a Lambda proxy integration, API Gateway does not map the response automatically or apply integration response templates. The Lambda function itself must return a JSON object with specific keys: `statusCode` (integer), `headers` (object), and `body` (string). If the Lambda function returns raw data or any other structure, API Gateway fails to parse it and returns a 502 Bad Gateway error to the client. Formatting the function's output with these keys resolves the error.
Adım Adım Çözüm
Anahtar Kavram
Lambda Proxy Integration Response Format
Tahmini Süre:1m 30s