模板格式 - Amazon CloudFormation
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

模板格式

可使用 JSON 或 YAML 格式创作 CloudFormation 模板。这两种格式都有相同的 CloudFormation 特性和功能。

JSON

以下示例演示了 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

以下示例演示了 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

在决定要使用哪种格式时,选择最方便使用的格式。另外,考虑到 YAML 本身提供了一些不适用于 JSON 的功能,例如注释。

以下示例介绍一个包含内联注释的 YAML 模板。

AWSTemplateFormatVersion: "2010-09-09" Description: A sample template Resources: MyEC2Instance: #An inline comment Type: "AWS::EC2::Instance" Properties: ImageId: "ami-0ff8a91507f77f867" #Another comment -- This is a Linux AMI InstanceType: t2.micro KeyName: testkey BlockDeviceMappings: - DeviceName: /dev/sdm Ebs: VolumeType: io1 Iops: 200 DeleteOnTermination: false VolumeSize: 20

规范

CloudFormation 支持以下 JSON 和 YAML 规范:

JSON

CloudFormation 遵循 ECMA-404 JSON 标准。有关 JSON 格式的更多信息,请访问 http://www.json.org

YAML

CloudFormation 支持 YAML 版本 1.1 规范,但有一些例外。CloudFormation 不支持以下功能:

  • binaryomappairssettimestamp 标签

  • 别名

  • 哈希合并

有关 YAML 的更多信息,请参阅 https://yaml.org/

了解更多信息

对于您在模板中指定的每个资源,可以使用 JSON 或 YAML 的特定语法规则定义其属性和值。有关适用于每种格式的模板语法的更多信息,请参阅模板部分