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?
- Add `Transform: AWS::Serverless-2016-10-31` at the root level of the template.Cevap
- BAdd an IAM trust policy that explicitly allows the `apigateway.amazonaws.com` service to assume the execution role of the Lambda function.
- CSpecify the `Integration: AWS_PROXY` property under the `Events` section of the serverless function.
- DConfigure a `Timeout` property under `Globals` to match the execution environment's maximum execution duration.
Cevap
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.
Adım Adım Çözüm
Anahtar Kavram
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.