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?
- Add the Transform: AWS::Serverless-2016-10-31 declaration at the root of the template file.Cevap
- Format the Lambda function's return value to be a JSON object containing the statusCode and body keys.Cevap
- CModify the template to use a Lambda Custom Integration and configure a default integration response in API Gateway.
- DUpdate the Lambda function's IAM execution role trust policy to allow the apigateway.amazonaws.com service principal to assume the role.
- EIncrease 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
Anahtar Kavram
AWS SAM Template Validation and API Gateway Lambda Proxy Response Schema