Soru

Zorluk: ZorAPI Development and Integration with Amazon API Gateway

A developer is migrating a backend AWS Lambda function from a Lambda custom (non-proxy) integration to a Lambda proxy integration on an Amazon API Gateway REST API. Under the custom integration, the Lambda function received a pre-mapped JSON payload containing query parameters and headers, and it returned a simple JSON object:

`{ "status": "success", "data": { "userId": 101 } }`

After configuring the API Gateway to use Lambda Proxy Integration, clients receive a 502 Bad Gateway error on all API requests. Additionally, the Lambda function execution logs show errors indicating that the incoming event format is unexpected.

Which of the following modifications must the developer make to resolve these errors?

  1. Modify the Lambda function to parse the incoming request body from the event.body property, and update the function's return statement to return a JSON object with statusCode and a stringified JSON body.Cevap
  2. B
    Define an Integration Request mapping template in API Gateway to map the client request to the Lambda input event, and define an Integration Response to map the Lambda return payload to a Method Response.
  3. C
    Configure a Lambda authorizer for the API Gateway method to authenticate the client request and inject the required request context before invoking the backend Lambda function.
  4. D
    Add Access-Control-Allow-Origin headers to the API Gateway Method Response configuration and configure API Gateway to automatically convert the Lambda output string into a 502 status code.

Cevap

Modify the Lambda function to parse the incoming request body from the event.body property, and update the function's return statement to return a JSON object with statusCode and a stringified JSON body.
The correct option is to modify the Lambda function to parse the incoming request body from the event.body property and return a JSON object with statusCode and a stringified JSON body. This is because Lambda proxy integration passes the entire HTTP request wrapper where the request body is stringified, and requires the response to conform strictly to a response format containing statusCode and body fields.

Adım Adım Çözüm

1
Understand the difference between Lambda custom integration and Lambda proxy integration input events.
In custom integrations, API Gateway maps parameters and payload before invoking Lambda. In proxy integrations, API Gateway passes the raw request inside an event object, where the body is a stringified JSON in the event.body property.
This explains why the Lambda function logged unexpected event format errors after the integration type was changed to proxy.
2
Understand the difference in response expectations for Lambda proxy integration.
Lambda proxy integration requires the backend function to return a specific JSON response containing at least statusCode (an integer) and body (a string representing the response payload).
If the backend returns a custom JSON object or a raw string instead of this expected format, API Gateway cannot parse it and yields a 502 Bad Gateway error.
3
Implement code changes in the Lambda function to parse the input and format the output correctly.
The function parses event.body for incoming parameters and returns an object such as { statusCode: 200, body: JSON.stringify({ status: 'success', data: { userId: 101 } }) }.
This matches the input and output requirements for Lambda proxy integration and resolves both the execution logs error and the 502 Bad Gateway response.

Anahtar Kavram

The primary difference in payload structure and response contract between API Gateway Lambda Proxy and Lambda Custom (non-proxy) integrations.
Tahmini Süre:2m 0s
Bu soruyu puanla