

# CloudFormation 模板格式
<a name="template-formats"></a>

可使用 JSON 或 YAML 格式创作 CloudFormation 模板。这两种格式的目的相同，但在可读性和复杂性方面各有明显的优势。
+ **JSON** – JSON 是一种轻量级的数据交换格式，非常便于机器解析和生成。但对于人类来说，读写可能会变得很麻烦，配置复杂时尤其如此。在 JSON 中，模板使用嵌套的大括号 `{}` 和方括号 `[]` 来定义资源、参数和其他组件。其语法要求显式声明每个元素，因此可能会使模板变得冗长，不过可以确保严格遵守结构化的格式。
+ **YAML** – 与 JSON 相比，YAML 的设计更便于人类阅读且不那么冗长。YAML 使用缩进而不是大括号和方括号来表示嵌套，因此更便于直观地显示资源和参数的层次结构。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.amazonaws.cn/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)。