

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

# CloudFormation 範本格式
<a name="template-formats"></a>

您可用 JSON 或 YAML 格式編寫 CloudFormation 範本。這兩種格式用途相同，但在可讀性和複雜度方面各有獨特優勢。
+ **JSON** – JSON 是一種輕量級資料交換格式，易於機器解析和生成。但對於人類而言，撰寫和閱讀可能較為繁瑣，尤其是在處理複雜組態時。在 JSON 中，範本透過巢狀的大括號 `{}` 和方括號 `[]` 來建構，以定義資源、參數及其他元件。其語法要求每個元素都必須明確宣告，這可能使範本較為冗長，但能確保嚴格遵循結構化格式。
+ **YAML** – YAML 設計初衷是比 JSON 更易於人類閱讀，且更精簡。它使用縮排而非括號來表示巢狀關係，能更直觀地呈現資源和參數的階層結構。YAML 因其清晰性和易用性而備受青睞，尤其在處理較複雜的範本時。但 YAML 依賴縮排，若間距不一致可能導致錯誤，因此需仔細留意以確保正確性。

## 範本結構
<a name="template-structure"></a>

CloudFormation 範本分為多個區段，每個區段用於存放特定類型的資訊。部分區段必須按特定順序宣告，其他區段則無順序要求。不過，在您建置範本時，使用下列範例所示的邏輯順排可能有幫助，因為某個區段中的值可能會參照先前區段中的值。

建立範本時，請勿重複宣告主要區段 (例如 `Resources` 區段)。雖然 CloudFormation 可能會接受範本，但在處理範本時會有未定義的行為，並且可能不正確地佈建資源或傳回莫名錯誤。

### JSON
<a name="template-structure.json"></a>

下列範例顯示包含所有可用區段的 JSON 格式範本結構。

```
{
  "AWSTemplateFormatVersion" : "{{version date}}",

  "Description" : "{{JSON string}}",

  "Metadata" : {
    {{template metadata}}
  },

  "Parameters" : {
    {{set of parameters}}
  },
  
  "Rules" : {
    {{set of rules}}
  },

  "Mappings" : {
    {{set of mappings}}
  },

  "Conditions" : {
    {{set of conditions}}
  },

  "Transform" : {
    {{set of transforms}}
  },

  "Resources" : {
    {{set of resources}}
  },
  
  "Outputs" : {
    {{set of outputs}}
  }
}
```

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

下列範例顯示包含所有可用區段的 YAML 格式範本結構。

```
---
AWSTemplateFormatVersion: {{version date}}

Description:
  {{String}}

Metadata:
  {{template metadata}}

Parameters:
  {{set of parameters}}

Rules:
  {{set of rules}}

Mappings:
  {{set of mappings}}

Conditions:
  {{set of conditions}}

Transform:
  {{set of transforms}}

Resources:
  {{set of resources}}

Outputs:
  {{set of outputs}}
```

## 說明
<a name="template-comments"></a>

JSON 格式的範本不支援註解。JSON 本身並未設計註解語法，因此您無法在 JSON 結構中直接新增註解。但如果您需要加入說明性備註或文件，可考慮新增中繼資料。如需詳細資訊，請參閱 [Metadata 屬性](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-attribute-metadata.html)。

在 YAML 格式的範本中，您可以使用 `#` 符號加入行內註解。

以下範例示範有內嵌評論的 YAML 範本。

```
AWSTemplateFormatVersion: 2010-09-09
Description: A sample CloudFormation template with YAML comments.
# Resources section
Resources:
  MyEC2Instance: 
    Type: AWS::EC2::Instance
    Properties: 
      # Linux AMI
      ImageId: ami-1234567890abcdef0 
      InstanceType: t2.micro
      KeyName: MyKey
      BlockDeviceMappings:
        - DeviceName: /dev/sdm
          Ebs:
            VolumeType: io1
            Iops: 200
            DeleteOnTermination: false
            VolumeSize: 20
```

## 規格
<a name="template-formats.supported-specifications"></a>

CloudFormation 支援以下 JSON 和 YAML 規格：

JSON  
CloudFormation 遵循 ECMA-404 JSON 標準。如需 JSON 格式的詳細資訊，請參閱 [http://www.json.org](http://www.json.org)。

YAML  
CloudFormation 支援 YAML 1.1 版規格，但有幾個例外狀況。CloudFormation 不支援以下功能：  
+ `binary`、`omap`、`pairs`、`set` 和 `timestamp` 標籤
+ 別名
+ 雜湊合併
如需 YAML 的詳細資訊，請參閱 [https://yaml.org/](https://yaml.org/)。

## 進一步了解
<a name="template-formats.learnmore"></a>

對於您在範本中指定的每個資源，都需使用 JSON 或 YAML 的特定語法規則定義其屬性和值。如需每種格式之範本語法的詳細資訊，請參閱[CloudFormation 範本區段](template-anatomy.md)。