Question

Difficulty: EasyAWS Serverless Application Model (SAM)

A developer is attempting to deploy a serverless application using a template file named template.yaml. The file contains the following code:

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

When deploying this template directly via AWS CloudFormation, the deployment fails with the error message: `Template format error: Unrecognized resource type: AWS::Serverless::Function`.

Which of the following additions to the template will resolve this error?

  1. Add `Transform: AWS::Serverless-2016-10-31` at the root level of the template.Answer
  2. B
    Add an IAM trust policy that explicitly allows the `apigateway.amazonaws.com` service to assume the execution role of the Lambda function.
  3. C
    Specify the `Integration: AWS_PROXY` property under the `Events` section of the serverless function.
  4. D
    Configure a `Timeout` property under `Globals` to match the execution environment's maximum execution duration.

Answer

To resolve the unrecognized resource type error, the `Transform: AWS::Serverless-2016-10-31` declaration must be added at the root level of the template. This declaration tells AWS CloudFormation to process the template using the AWS SAM translator, which converts serverless-specific resources like `AWS::Serverless::Function` into standard AWS CloudFormation resources.
The template contains an `AWS::Serverless::Function` resource, which is a custom resource type extension provided by the AWS Serverless Application Model (SAM). AWS CloudFormation does not natively recognize this resource type. To enable CloudFormation to parse and translate this template into standard resources, the `Transform: AWS::Serverless-2016-10-31` line must be included at the root level of the template. Adding this declaration resolves the parsing error.

Step-by-Step Solution

1
Analyze the error message returned during template deployment.
The error indicates that the resource type `AWS::Serverless::Function` is unrecognized by AWS CloudFormation.
This tells us that the parser is treating the template as standard CloudFormation and does not know how to translate SAM resources.
2
Check the root level of the template for the required transform declaration.
The template only has a `Resources` block and is missing the `Transform` declaration.
AWS CloudFormation requires a macro to process the SAM template format into standard CloudFormation resources.
3
Add the transform statement to the template.
Inserting `Transform: AWS::Serverless-2016-10-31` at the root allows CloudFormation to parse the SAM resources.
This macro transforms the serverless resource declarations into their underlying AWS CloudFormation resources (like `AWS::Lambda::Function` and `AWS::IAM::Role`) during deployment.

Key Concept

AWS SAM templates extend AWS CloudFormation. To deploy SAM resources using CloudFormation, the template must include the `Transform: AWS::Serverless-2016-10-31` declaration. This triggers the CloudFormation transform macro to parse and compile SAM-specific resources into standard CloudFormation resources.
Rate this question