Question

Difficulty: MediumAWS Serverless Application Model (SAM)

A developer is deploying a serverless backend using AWS SAM. The configuration file `template.yaml` contains the following definition:

yaml
Resources:
ProcessDataFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Runtime: python3.12
CodeUri: src/
Events:
GetData:
Type: HttpApi
Properties:
Path: /data
Method: GET

During the deployment process, the CloudFormation stack creation fails with the message `Template format error: Unrecognized resource type: AWS::Serverless::Function`. Additionally, the developer notes that the python handler code currently returns a plain text string `'Success'`, which will cause integration failure when invoked through the API Gateway endpoint.

Which two actions must the developer take to resolve these issues?

  1. Add `Transform: AWS::Serverless-2016-10-31` at the root of the template file.Answer
  2. Modify the python handler to return a dictionary with `statusCode` and `body` keys, where `body` is a JSON-formatted string.Answer
  3. C
    Create a new trust policy for the Lambda execution role allowing the `apigateway.amazonaws.com` service principal to perform `sts:AssumeRole`.
  4. D
    Configure the `Timeout` property of the `AWS::Serverless::Function` resource to 900 seconds to prevent execution timeouts.
  5. E
    Store the response string in the Systems Manager Parameter Store and retrieve it dynamically in the API Gateway integration settings.

Answer

The correct actions are to add the `Transform: AWS::Serverless-2016-10-31` declaration at the root of the template file, and to update the python handler to return a dictionary with `statusCode` and `body` keys.
Adding the Transform declaration allows the AWS CloudFormation service to parse the serverless resources. Returning a dictionary with the status code and JSON body conforms to the Lambda Proxy integration format required by the API Gateway HTTP API configuration.

Step-by-Step Solution

1
Diagnose the CloudFormation parsing failure.
The `Unrecognized resource type: AWS::Serverless::Function` error occurs because CloudFormation does not natively understand the `AWS::Serverless` namespace without the SAM translator. Adding `Transform: AWS::Serverless-2016-10-31` at the template root resolves this.
The Transform declaration instructs CloudFormation to invoke the SAM translator to convert the simplified SAM syntax into standard CloudFormation resources.
2
Diagnose the API Gateway integration failure.
By default, API Gateway event sources declared on SAM Functions use Lambda Proxy Integration, which expects the Lambda function to return a structured JSON response containing `statusCode` and a string `body`.
If the function returns a raw string, API Gateway cannot map it to an HTTP response, resulting in an integration error (HTTP 502).

Key Concept

AWS SAM templates must include the Transform declaration to allow CloudFormation to interpret serverless resources, and Lambda functions integrated with API Gateway HTTP APIs must adhere to the Lambda Proxy Integration response format.
Rate this question