Amazon SAM template anatomy - Amazon Serverless Application Model
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Amazon SAM template anatomy

An Amazon SAM template file closely follows the format of an Amazon CloudFormation template file, which is described in Template anatomy in the Amazon CloudFormation User Guide. The primary differences between Amazon SAM template files and Amazon CloudFormation template files are the following:

  • Transform declaration. The declaration Transform: AWS::Serverless-2016-10-31 is required for Amazon SAM template files. This declaration identifies an Amazon CloudFormation template file as an Amazon SAM template file. For more information about transforms, see Transform in the Amazon CloudFormation User Guide.

  • Globals section. The Globals section is unique to Amazon SAM. It defines properties that are common to all your serverless functions and APIs. All the AWS::Serverless::Function, AWS::Serverless::Api, and AWS::Serverless::SimpleTable resources inherit the properties that are defined in the Globals section. For more information about this section, see Globals section of the Amazon SAM template.

  • Resources section. In Amazon SAM templates the Resources section can contain a combination of Amazon CloudFormation resources and Amazon SAM resources. For more information about Amazon CloudFormation resources, see Amazon resource and property types reference in the Amazon CloudFormation User Guide. For more information about Amazon SAM resources, see Amazon SAM resources and properties.

All other sections of an Amazon SAM template file correspond to the Amazon CloudFormation template file section of the same name.

YAML

The following example shows a YAML-formatted template fragment.

Transform: AWS::Serverless-2016-10-31 Globals: set of globals Description: String Metadata: template metadata Parameters: set of parameters Mappings: set of mappings Conditions: set of conditions Resources: set of resources Outputs: set of outputs

Template sections

Amazon SAM templates can include several major sections. Only the Transform and Resources sections are required.

You can include template sections in any order. However, if using language extensions, you should add AWS::LanguageExtensions before the serverless transform (that is, before AWS::Serverless-2016-10-31) as shown in the following example:

Transform: - AWS::LanguageExtensions - AWS::Serverless-2016-10-31

As you build your template, it can be helpful to use the logical order that's shown in the following list. This is because the values in one section might refer to values from a previous section.

Transform (required)

For Amazon SAM templates, you must include this section with a value of AWS::Serverless-2016-10-31.

Additional transforms are optional. For more information about transforms, see Transform in the Amazon CloudFormation User Guide.

Globals (optional)

Properties that are common to all your serverless functions, APIs, and simple tables. All the AWS::Serverless::Function, AWS::Serverless::Api, and AWS::Serverless::SimpleTable resources inherit the properties that are defined in the Globals section.

This section is unique to Amazon SAM. There isn't a corresponding section in Amazon CloudFormation templates.

Description (optional)

A text string that describes the template.

This section corresponds directly with the Description section of Amazon CloudFormation templates.

Metadata (optional)

Objects that provide additional information about the template.

This section corresponds directly with the Metadata section of Amazon CloudFormation templates.

Parameters (optional)

Values to pass to your template at runtime (when you create or update a stack). You can refer to parameters from the Resources and Outputs sections of the template. Objects that are declared in the Parameters section cause the sam deploy --guided command to present additional prompts to the user.

Values that are passed in using the --parameter-overrides parameter of the sam deploy command—and entries in the configuration file—take precendence over entries in the Amazon SAM template file. For more information about the sam deploy command, see sam deploy in the Amazon SAM CLI command reference. For more information about the configuration file, see Amazon SAM CLI configuration file.

Mappings (optional)

A mapping of keys and associated values that you can use to specify conditional parameter values, similar to a lookup table. You can match a key to a corresponding value by using the Fn::FindInMap intrinsic function in the Resources and Outputs sections.

This section corresponds directly with the Mappings section of Amazon CloudFormation templates.

Conditions (optional)

Conditions that control whether certain resources are created or whether certain resource properties are assigned a value during stack creation or update. For example, you could conditionally create a resource that depends on whether the stack is for a production or test environment.

This section corresponds directly with the Conditions section of Amazon CloudFormation templates.

Resources (required)

The stack resources and their properties, such as an Amazon Elastic Compute Cloud (Amazon EC2) instance or an Amazon Simple Storage Service (Amazon S3) bucket. You can refer to resources in the Resources and Outputs sections of the template.

This section is similar to the Resources section of Amazon CloudFormation templates. In Amazon SAM templates, this section can contain Amazon SAM resources in addition to Amazon CloudFormation resources.

Outputs (optional)

The values that are returned whenever you view your stack's properties. For example, you can declare an output for an S3 bucket name, and then call the aws cloudformation describe-stacks Amazon Command Line Interface (Amazon CLI) command to view the name.

This section corresponds directly with the Outputs section of Amazon CloudFormation templates.

Next steps

To download and deploy a sample serverless application that contains an Amazon SAM template file, see Getting started with Amazon SAM and follow the instructions in Tutorial: Deploy a Hello World application.