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 ` 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?
- 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
- BThe 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.
- CThe Lambda function is timing out because its execution context is reused across requests, causing the default -second runtime limit to be exceeded when processing the API payload.
- DThe 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.