Soru

Zorluk: KolayServerless Development with AWS Lambda

A developer is building a serverless API where an Amazon API Gateway REST API is integrated with an AWS Lambda function using Lambda proxy integration. The Lambda function processes user data and returns a response. During testing, clients receive a 502502 Bad Gateway error. The Lambda execution logs show that the function ran successfully and completed without error. Which two changes should the developer make to the Lambda function's response to resolve this issue? (Select two.)

  1. Format the return value of the Lambda handler as a JSON object containing the statusCode and body fields.Cevap
  2. Ensure that the body field of the returned JSON object is serialized as a string.Cevap
  3. C
    Configure integration response mapping rules in the API Gateway console to map the return value to an HTTP status code.
  4. D
    Return a raw string response directly from the Lambda function handler.
  5. E
    Add permission to the Lambda execution role to allow the apigateway:POST action.

Cevap

The developer should format the return value of the Lambda handler as a JSON object containing the statusCode and body fields, and ensure that the body field of the returned JSON object is serialized as a string.
For an Amazon API Gateway REST API utilizing Lambda proxy integration, the backend Lambda function must return a JSON response with a specific schema. This schema requires a 'statusCode' key (as an integer) and a 'body' key (which must be a string). If the function finishes successfully but returns a malformed structure or a non-string 'body' value, API Gateway cannot construct the HTTP response and returns a 502502 Bad Gateway error.

Adım Adım Çözüm

1
Identify the integration type between Amazon API Gateway and the AWS Lambda function.
The integration type is Lambda proxy integration.
This determines the response structure requirements. Lambda proxy integration bypasses integration response mappings and expects the Lambda function to return a specific JSON payload format directly.
2
Verify the structure of the Lambda response payload.
Ensure the payload is a JSON object containing the statusCode and body keys.
API Gateway requires the statusCode to determine the HTTP response code and the body key to populate the response payload. Without these fields, API Gateway returns a 502502 Bad Gateway error.
3
Serialize the body payload.
Convert the body object into a string (e.g., using JSON.stringify()).
API Gateway requires the body parameter within the returned JSON object to be a string. Returning a raw JSON object or array under the body key will cause the integration to fail.

Anahtar Kavram

API Gateway Lambda Proxy Integration Response Format
Bu soruyu puanla