

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 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 範本檔案的主要差異如下：
+ **轉換宣告。**範本檔案`Transform: AWS::Serverless-2016-10-31`需要 AWS SAM 宣告。此宣告會將 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::SimpleTable`、 `AWS::Serverless::HttpApi`和 `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::LanguageExtensions`*之前*新增 （也就是在 之前`AWS::Serverless-2016-10-31`)，如下列範例所示：

```
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::SimpleTable`、 `AWS::Serverless::HttpApi`和 `AWS::Serverless::StateMachine` 資源都會繼承 `Globals`區段中定義的屬性。  
本節對 是唯一的 AWS SAM。 CloudFormation 範本中沒有對應的區段。

**[描述 (選用)](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-description-structure.html)**  
說明範本的文字字串。  
本節直接對應至 CloudFormation 範本的 `Description`區段。

**[Metadata (選用)](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`命令的詳細資訊，請參閱 命令參考[sam deploy](sam-cli-command-reference-sam-deploy.md)中的 AWS SAM CLI 。如需組態檔案的詳細資訊，請參閱 [AWS SAM CLI 組態檔案](serverless-sam-cli-config.md)。

**[Mappings (選用)](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/mappings-section-structure.html)**  
可用來指定條件式參數值之索引鍵與相關聯值的映射，與查詢表格類似。您可以使用 [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) `Resources`和 `Outputs`區段中的內部 函數，將索引鍵與對應的值進行比對。  
本節直接對應至 CloudFormation 範本的 `Mappings`區段。

**[Conditions (選用)](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 Elastic Compute Cloud (Amazon EC2) 執行個體或 Amazon Simple Storage Service (Amazon S3) 儲存貯體。您可以參照範本之 `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)。