A developer is building a serverless web application using Amazon API Gateway and an AWS Lambda function with a Lambda proxy integration. During testing, client HTTP requests receive an HTTP 502 Bad Gateway error. The CloudWatch logs show that the Lambda function completed its execution successfully and returned the application data as a plain JSON string. How can the developer resolve this issue?
- AConfigure a body mapping template in API Gateway to extract the properties from the Lambda function's plain JSON response.
- BEnable CORS in the API Gateway method response and define the Access-Control-Allow-Origin header value to match the client's origin.
- Modify the Lambda function response to return a JSON object containing a statusCode integer and a stringified JSON payload in the body field.Cevap
- DModify the Lambda function's IAM execution role to trust the API Gateway service principal to assume the role.
Cevap
Modify the Lambda function response to return a JSON object containing a statusCode integer and a stringified JSON payload in the body field.
The correct solution is to modify the Lambda function's response. In Amazon API Gateway Lambda proxy integration, API Gateway expects the backend Lambda function to return a response in a specific JSON format. This format must include the statusCode integer field and the response payload as a stringified JSON within the body field. If the Lambda function returns a plain JSON string directly, API Gateway cannot map it to an HTTP response and fails with a 502 Bad Gateway error.
Adım Adım Çözüm
Anahtar Kavram
API Gateway Lambda Proxy Integration Response Format
Alternatif Yöntem
If modifying the Lambda function code is not possible or desired, the developer can change the API Gateway integration type from Lambda proxy integration to Lambda custom integration. This allows API Gateway to accept the plain JSON payload and map it to an HTTP response using Integration Response mapping templates.
Tahmini Süre:1m 30s