

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# AWS SAM 模板解剖学
<a name="sam-specification-template-anatomy"></a>

 AWS SAM 模板文件严格遵循 CloudFormation 模板文件的格式，该格式在《*AWS CloudFormation 用户指南》*的[模板剖析](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-anatomy.html)中进行了描述。 AWS SAM 模板文件和 CloudFormation 模板文件之间的主要区别如下：
+ **转换声明。** AWS SAM 模板文件需要声明 `Transform: AWS::Serverless-2016-10-31`。此声明将 CloudFormation 模板文件标识为 AWS SAM 模板文件。有关转换的更多信息，请参阅*《AWS CloudFormation 用户指南》*中的[转换](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/transform-section-structure.html)。
+ **全局变量部分。**该`Globals`部分是独一无二的 AWS SAM。它定义了所有无服务器函数通用的属性，以及。 APIs所有`AWS::Serverless::Function`、、`AWS::Serverless::Api``AWS::Serverless::CapacityProvider`、`AWS::Serverless::HttpApi``AWS::Serverless::SimpleTable`、和`AWS::Serverless::StateMachine`资源都继承本`Globals`节中定义的属性。有关该部分的更多信息，请参阅 [模板的 “全局” 部分 AWS SAM](sam-specification-template-anatomy-globals.md)。
+ **资源部分。**在 AWS SAM 模板中，该`Resources`部分可以包含 CloudFormation 资源和 AWS SAM 资源的组合。有关 CloudFormation 资源的更多信息，请参阅*AWS CloudFormation 用户指南*中的[AWS 资源和属性类型参考](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html)。有关 AWS SAM 资源的更多信息，请参阅[AWS SAM 资源和财产](sam-specification-resources-and-properties.md)。

 AWS SAM 模板文件的所有其他部分对应于同名的 CloudFormation 模板文件部分。

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

以下示例显示 YAML 格式的模板片段。

```
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
```

## 模板部分
<a name="template-anatomy-sections"></a>

AWS SAM 模板可以包括几个主要部分。仅 `Transform` 和 `Resources` 部分为必需。

您可以按任意顺序包含模板部分。但是，如果使用语言扩展，则应在无服务器转换*之前*（即 `AWS::Serverless-2016-10-31` 之前）添加 `AWS::LanguageExtensions`，如下例所示：

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

在您构建模板时，使用以下列表中显示的逻辑顺序可能会很有用。这是因为某一部分中的值可能引用前一部分中的值。

**[转换（必需）](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/transform-section-structure.html)**  
对于 AWS SAM 模板，必须包含此部分，其值为`AWS::Serverless-2016-10-31`。  
其它变换是可选的。有关转换的更多信息，请参阅*《AWS CloudFormation 用户指南》*中的[转换](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/transform-section-structure.html)。

**[全局变量（可选）](sam-specification-template-anatomy-globals.md)**  
所有无服务器函数和简单表都通用的属性。 APIs所有`AWS::Serverless::Function`、、`AWS::Serverless::Api``AWS::Serverless::CapacityProvider`、`AWS::Serverless::HttpApi``AWS::Serverless::SimpleTable`、和`AWS::Serverless::StateMachine`资源都继承本`Globals`节中定义的属性。  
本节是独一无二的 AWS SAM。 CloudFormation 模板中没有相应的部分。

**[Description](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-description-structure.html)（可选）**  
一个描述模板的文本字符串。  
本节与 CloudFormation 模板`Description`部分直接对应。

**[元数据（可选）](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/metadata-section-structure.html)**  
提供有关模板的其他信息的对象。  
本节与 CloudFormation 模板`Metadata`部分直接对应。

**[Parameters（可选）](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/parameters-section-structure.html)**  
要在运行时 (创建或更新堆栈时) 传递到模板的值。您可引用模板的 `Resources` 和 `Outputs` 部分中的参数。在 `Parameters` 部分中声明的对象会导致 **sam deploy --guided** 命令向用户显示附加提示。  
使用 `sam deploy` 命令的 `--parameter-overrides` 参数传入的值以及配置文件中的条目优先于 AWS SAM 模板文件中的条目。有关 `sam deploy` 命令的更多信息，请参阅 AWS SAM CLI 命令参考中的 [sam deploy](sam-cli-command-reference-sam-deploy.md)。有关配置文件的更多信息，请参阅[AWS SAMCLI 配置文件](serverless-sam-cli-config.md)。

**[Mappings（可选）](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/mappings-section-structure.html)**  
可用来指定条件参数值的密钥和关键值的映射，与查找表类似。可以通过使用 `Resources` 和 `Outputs` 部分中的 [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-findinmap.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-findinmap.html) 内置函数将键与相应的值匹配。  
本节与 CloudFormation 模板`Mappings`部分直接对应。

**[条件（可选）](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/conditions-section-structure.html)**  
用于控制是否创建某些资源或者是否在堆栈创建或更新过程中为某些资源属性分配值的条件。例如，您可以根据堆栈是用于生产环境还是用于测试环境来按照条件创建资源。  
本节与 CloudFormation 模板`Conditions`部分直接对应。

**[Resources（必需）](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/resources-section-structure.html)**  
堆栈资源及其属性，例如亚马逊弹性计算云 (Amazon EC2) 实例或亚马逊简单存储服务 (Amazon S3) Service 存储桶。您可引用模板的 `Resources` 和 `Outputs` 部分中的资源。  
该部分与 CloudFormation 模板的 `Resources` 部分类似。在 AWS SAM 模板中，除 AWS SAM 资源外，此部分还可以包含 CloudFormation 资源。

**[Outputs（可选）](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/outputs-section-structure.html)**  
在您查看堆栈的属性时返回的值。例如，您可以为 S3 存储桶名称声明输出，然后调用 `aws cloudformation describe-stacks` AWS Command Line Interface (AWS CLI) 命令查看该名称。  
该部分与 CloudFormation 模板的 `Outputs` 部分直接对应。

## 后续步骤
<a name="template-anatomy-next-steps"></a>

要下载和部署包含 AWS SAM 模板文件的示例无服务器应用程序，请参阅[入门 AWS SAM](serverless-getting-started.md)并按照中的[教程：使用以下命令部署 Hello World 应用程序 AWS SAM](serverless-getting-started-hello-world.md)说明进行操作。