

# Amazon SAM template anatomy
<a name="sam-specification-template-anatomy"></a>

An Amazon SAM template file closely follows the format of an Amazon CloudFormation template file, which is described in [Template anatomy](https://docs.amazonaws.cn/AWSCloudFormation/latest/UserGuide/template-anatomy.html) 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](https://docs.amazonaws.cn/AWSCloudFormation/latest/UserGuide/transform-section-structure.html) 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`, `AWS::Serverless::CapacityProvider`, `AWS::Serverless::HttpApi`, `AWS::Serverless::SimpleTable`, and `AWS::Serverless::StateMachine` 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](sam-specification-template-anatomy-globals.md).
+ **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](https://docs.amazonaws.cn/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html) in the *Amazon CloudFormation User Guide*. For more information about Amazon SAM resources, see [Amazon SAM resources and properties](sam-specification-resources-and-properties.md).

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

## YAML
<a name="template-anatomy-outline.yaml"></a>

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
<a name="template-anatomy-sections"></a>

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)](https://docs.amazonaws.cn/AWSCloudFormation/latest/UserGuide/transform-section-structure.html)**  
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](https://docs.amazonaws.cn/AWSCloudFormation/latest/UserGuide/transform-section-structure.html) in the *Amazon CloudFormation User Guide*.

**[Globals (optional)](sam-specification-template-anatomy-globals.md)**  
Properties that are common to all your serverless functions, APIs, and simple tables. All the `AWS::Serverless::Function`, `AWS::Serverless::Api`, `AWS::Serverless::CapacityProvider`, `AWS::Serverless::HttpApi`, `AWS::Serverless::SimpleTable`, and `AWS::Serverless::StateMachine` 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)](https://docs.amazonaws.cn/AWSCloudFormation/latest/UserGuide/template-description-structure.html)**  
A text string that describes the template.  
This section corresponds directly with the `Description` section of Amazon CloudFormation templates.

**[Metadata (optional)](https://docs.amazonaws.cn/AWSCloudFormation/latest/UserGuide/metadata-section-structure.html)**  
Objects that provide additional information about the template.  
This section corresponds directly with the `Metadata` section of Amazon CloudFormation templates.

**[Parameters (optional)](https://docs.amazonaws.cn/AWSCloudFormation/latest/UserGuide/parameters-section-structure.html)**  
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 precedence over entries in the Amazon SAM template file. For more information about the `sam deploy` command, see [sam deploy](sam-cli-command-reference-sam-deploy.md) in the Amazon SAM CLI command reference. For more information about the configuration file, see [Amazon SAM CLI configuration file](serverless-sam-cli-config.md).

**[Mappings (optional)](https://docs.amazonaws.cn/AWSCloudFormation/latest/UserGuide/mappings-section-structure.html)**  
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 [https://docs.amazonaws.cn/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-findinmap.html](https://docs.amazonaws.cn/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-findinmap.html) intrinsic function in the `Resources` and `Outputs` sections.  
This section corresponds directly with the `Mappings` section of Amazon CloudFormation templates.

**[Conditions (optional)](https://docs.amazonaws.cn/AWSCloudFormation/latest/UserGuide/conditions-section-structure.html)**  
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)](https://docs.amazonaws.cn/AWSCloudFormation/latest/UserGuide/resources-section-structure.html)**  
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)](https://docs.amazonaws.cn/AWSCloudFormation/latest/UserGuide/outputs-section-structure.html)**  
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
<a name="template-anatomy-next-steps"></a>

To download and deploy a sample serverless application that contains an Amazon SAM template file, see [Getting started with Amazon SAM](serverless-getting-started.md) and follow the instructions in [Tutorial: Deploy a Hello World application with Amazon SAM](serverless-getting-started-hello-world.md).