Soru

Zorluk: OrtaAWS Serverless Application Model (SAM)

A developer is using AWS SAM to build and deploy a serverless application. The application consists of an Amazon API Gateway HTTP API that triggers an AWS Lambda function. During the initial deployment of the template using the AWS SAM CLI, the deployment fails with an error stating that the resource type 'AWS::Serverless::Function' is unrecognized. After addressing the deployment failure, the developer tests the API endpoint but receives a 502 Bad Gateway error, even though Amazon CloudWatch Logs show that the Lambda function executed successfully and returned the correct data. Which two actions must the developer take to resolve these issues?

  1. Add the Transform: AWS::Serverless-2016-10-31 declaration at the root of the template file.Cevap
  2. Format the Lambda function's return value to be a JSON object containing the statusCode and body keys.Cevap
  3. C
    Modify the template to use a Lambda Custom Integration and configure a default integration response in API Gateway.
  4. D
    Update the Lambda function's IAM execution role trust policy to allow the apigateway.amazonaws.com service principal to assume the role.
  5. E
    Increase the Timeout property of the function under the Globals section of the template.

Cevap

The developer must add the Transform: AWS::Serverless-2016-10-31 declaration at the root of the template file and format the Lambda function's return value to be a JSON object containing the statusCode and body keys.
The deployment error is caused by the missing Transform header, which instructs CloudFormation to run the AWS SAM translator macro. The runtime 502 error is caused by the Lambda function returning a response format that API Gateway cannot parse for its proxy integration, requiring the output to be structured as a JSON object with statusCode and body.

Adım Adım Çözüm

1
Diagnose the CloudFormation deployment failure.
Identify that the error 'AWS::Serverless::Function is unrecognized' indicates that CloudFormation does not know how to parse the SAM-specific resource.
AWS CloudFormation requires the Transform header to invoke the SAM translation service.
2
Add the Transform declaration.
Add 'Transform: AWS::Serverless-2016-10-31' at the root of the template.
This enables successful compilation and deployment of the SAM resources.
3
Diagnose the 502 Bad Gateway runtime error.
A 502 Bad Gateway error when the Lambda logs indicate success points to a response parsing failure by API Gateway.
In Lambda Proxy integrations, API Gateway expects a specific schema from the Lambda response, containing the status code and body.
4
Format the Lambda response.
Ensure the function returns a JSON response matching the proxy integration structure.
This allows API Gateway to successfully parse the response and return it to the client.

Anahtar Kavram

AWS SAM Template Validation and API Gateway Lambda Proxy Response Schema
Bu soruyu puanla