Soru

Zorluk: OrtaAWS Serverless Application Model (SAM)

A developer uses the AWS Serverless Application Model (SAM) to deploy a serverless application. The template defines an AWS::Serverless::Function resource with an Api event source, as shown in the following snippet:

yaml
Resources:
ProcessOrderFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs18.x
CodeUri: ./src
Events:
CreateOrder:
Type: Api
Properties:
Path: /orders
Method: post

The application deploys successfully. However, when clients send a POST request to /orders, the API Gateway returns a 502 Bad Gateway status code, and the Lambda function execution logs show that the function ran and completed successfully without errors.

Which of the following describes the cause of this issue and the correct resolution?

  1. A
    The Lambda function execution exceeded its default timeout limit because SAM functions default to a timeout of 3 seconds. To resolve this, increase the Timeout property under the resource Properties in the SAM template.
  2. B
    The SAM template is missing the Transform declaration at the root level, causing API Gateway to fail to map the incoming request to the Lambda function. To resolve this, add 'Transform: AWS::Serverless-2016-10-31' to the template.
  3. The default integration type for the SAM Api event is a Lambda proxy integration. The Lambda function returned a plain text string instead of a structured JSON response containing the statusCode and body fields, which API Gateway requires. To resolve this, modify the Lambda function return value to match the expected JSON structure.Cevap
  4. D
    The IAM execution role assigned to the Lambda function has an invalid trust policy that does not trust the API Gateway service principal. To resolve this, update the trust policy to allow 'apigateway.amazonaws.com' to assume the role.

Cevap

The default integration type for the SAM Api event is a Lambda proxy integration. The Lambda function returned a plain text string instead of a structured JSON response containing the statusCode and body fields, which API Gateway requires. To resolve this, modify the Lambda function return value to match the expected JSON structure.
The default integration type configured by AWS SAM when using the Api event source is the Lambda proxy integration. Under this model, API Gateway passes the raw request directly to the Lambda function, and expects the Lambda function to return a response matching a specific JSON format (specifically containing 'statusCode' and 'body' fields). If the function returns a raw string or an unsupported format, API Gateway cannot map the response, resulting in a 502 Bad Gateway error. Modifying the Lambda function to return the correct JSON structure resolves the issue.

Adım Adım Çözüm

1
Analyze the error symptoms and deployment state.
The application deployed successfully, but requests result in a 502 Bad Gateway error, and Lambda logs show successful execution with no runtime exceptions.
This rules out deployment-time issues like missing transforms, and rules out internal Lambda execution errors or timeouts.
2
Identify the integration type defined by the AWS SAM Api event source.
By default, defining an Api event source under AWS::Serverless::Function sets up an Amazon API Gateway REST API with Lambda Proxy Integration.
Understanding the defaults of AWS SAM configurations helps pinpoint the expectations of the API Gateway integration.
3
Verify response formatting requirements for Lambda Proxy Integration.
API Gateway Proxy Integration expects the Lambda function output to be a JSON object with at least a 'statusCode' and a 'body' property.
Returning a plain string instead of the structured JSON payload causes API Gateway to fail parsing, leading to a 502 Bad Gateway response.

Anahtar Kavram

AWS SAM defaults to configuring API Gateway Lambda Proxy Integrations for Api events, which requires backend Lambda functions to return a specific JSON response format containing 'statusCode' and 'body'.
Bu soruyu puanla