Soru

Zorluk: OrtaServerless Development with AWS Lambda

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?

  1. A
    Configure a body mapping template in API Gateway to extract the properties from the Lambda function's plain JSON response.
  2. B
    Enable CORS in the API Gateway method response and define the Access-Control-Allow-Origin header value to match the client's origin.
  3. Modify the Lambda function response to return a JSON object containing a statusCode integer and a stringified JSON payload in the body field.Cevap
  4. D
    Modify 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

1
Analyze the 502 Bad Gateway error message and the successful Lambda execution logs.
Identify that the Lambda function execution succeeded but API Gateway failed to parse the returned payload.
A 502 Bad Gateway error with successful execution logs indicates that the communication between API Gateway and Lambda succeeded, but the response format from Lambda is incompatible with the integration type.
2
Check the integration type configured in API Gateway.
Confirm that Lambda proxy integration is enabled.
Lambda proxy integration mandates a strict response format from the backend, whereas custom integration allows returning raw payloads mapped via templates.
3
Update the Lambda function code to return a JSON object conforming to the proxy integration response contract.
The function now returns an object with a statusCode integer and a stringified payload in the body key.
This format allows API Gateway to successfully parse the response and construct the final HTTP response for the client.

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
Bu soruyu puanla