Soru

Zorluk: KolayAPI Development and Integration with Amazon API Gateway

A developer has configured a Lambda proxy integration in Amazon API Gateway. When testing the API, they receive a 502 (Bad Gateway) error. The Lambda function execution logs in Amazon CloudWatch show that the function ran successfully and returned the intended result. Which of the following is the most likely cause of this error?

  1. The Lambda function is returning a raw string or a custom JSON object instead of a JSON response containing the required "statusCode" and "body" keys.Cevap
  2. B
    The integration request requires mapping templates in API Gateway to transform the incoming request payload before invoking the Lambda function.
  3. C
    The developer configured a custom Lambda authorizer instead of a built-in Amazon Cognito User Pool authorizer, causing the execution to abort prior to integration.
  4. D
    The API Gateway integration must be changed to a Lambda custom integration because Lambda proxy integrations do not support dynamic routing or HTTP method execution.

Cevap

The Lambda function is returning a raw string or a custom JSON object instead of a JSON response containing the required "statusCode" and "body" keys.
In a Lambda proxy integration, API Gateway expects the backend Lambda function to return a response structured as a specific JSON object. This object must contain the 'statusCode' (as an integer or string) and the 'body' (as a stringified JSON payload). Since the Lambda function executed successfully but API Gateway returned a 502 Bad Gateway error, the most likely cause is that the function did not format its response payload to include these required keys, causing API Gateway to fail parsing the response.

Adım Adım Çözüm

1
Analyze the error code (502 Bad Gateway) and CloudWatch execution logs.
The Lambda function executed successfully without error, but API Gateway returned a 502 error to the client.
This indicates that the communication between API Gateway and Lambda succeeded, but API Gateway failed to parse the output format returned by the Lambda function.
2
Verify the integration type configured in API Gateway.
The integration is configured as a Lambda proxy integration.
Lambda proxy integration imposes strict requirements on the structure of the JSON returned by the backend Lambda function.
3
Verify the response structure of the Lambda function.
The function must return a JSON response containing 'statusCode' and 'body'.
API Gateway requires these fields to construct the HTTP response. A mismatch or missing fields will result in a 502 Bad Gateway error.

Anahtar Kavram

Understanding the response format requirements for API Gateway Lambda Proxy integrations.
Tahmini Süre:1m 0s
Bu soruyu puanla