Soru

Zorluk: OrtaAWS Serverless Application Model (SAM)

A developer uses AWS SAM to build and deploy a serverless microservice. The SAM template defines a Lambda function triggered by an API Gateway event using the following template definition:

yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Resources:
SubmitFeedbackFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/
Handler: app.handler
Runtime: nodejs18.x
Events:
PostFeedback:
Type: Api
Properties:
Path: /feedback
Method: post

The Lambda function's handler code is implemented as follows:

javascript
exports.handler = async (event) => {
const feedbackText = event.feedback;
return {
message: `Feedback received: ${feedbackText}`
};
};

When clients send a POST request with the JSON payload `{"feedback": "Great service!"}`, the response from the API is `502502 Bad Gateway` and the Lambda logs show that `feedbackText` is undefined.

Which explanation best identifies the root cause of this failure, and how should it be resolved?

  1. The API event source in AWS SAM defaults to API Gateway Lambda Proxy Integration, which passes the request payload as a serialized string in the body property of the event object. The handler must parse the body property to access the feedback value and must return a structured JSON response containing statusCode and body fields.Cevap
  2. B
    The AWS SAM template is missing the required global transform declaration at the resource level, preventing API Gateway from matching the route to the target Lambda function.
  3. C
    The Lambda function is timing out because its execution context is reused across requests, causing the default 33-second runtime limit to be exceeded when processing the API payload.
  4. D
    The auto-generated IAM role for the function is missing a trust policy allowing the API Gateway service principal to assume the role, preventing API Gateway from invoking the function.

Cevap

The API event source in AWS SAM defaults to API Gateway Lambda Proxy Integration, which passes the request payload as a serialized string in the body property of the event object. The handler must parse the body property to access the feedback value and must return a structured JSON response containing statusCode and body fields.
The API event source in AWS SAM defaults to API Gateway Lambda Proxy Integration. In a proxy integration, the request payload is passed directly as a serialized JSON string under the body property of the event parameter. The handler code must parse this string (e.g., using JSON.parse(event.body)) to access nested properties. Additionally, the Lambda function must return a structured JSON response containing the statusCode integer and a stringified body to satisfy the API Gateway proxy format. Returning a custom object without these properties causes API Gateway to fail to parse the backend response, returning a 502502 Bad Gateway error.

Adım Adım Çözüm

1
Analyze the event source definition in the AWS SAM template.
The event named PostFeedback uses the Api type, which translates to an Amazon API Gateway REST API configured with Lambda Proxy Integration by default.
Determining the integration type is essential to understand the structure of the incoming event object and the required response format.
2
Examine the Lambda handler code and the log output.
The handler attempts to access event.feedback directly, but under Lambda Proxy Integration, the payload is serialized as a string in event.body. Therefore, event.feedback is undefined.
This explains why the variable evaluates to undefined during execution.
3
Analyze the 502502 Bad Gateway error returned to clients.
In a proxy integration, API Gateway expects the Lambda function to return a structured JSON response containing key-value pairs for statusCode and body. Returning a plain object without these keys causes a parsing failure in API Gateway, resulting in a 502502 error.
This identifies the structural requirements of the return payload to resolve the gateway integration error.

Anahtar Kavram

AWS SAM API Event Source and Lambda Proxy Integration Response Requirements
Bu soruyu puanla