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

验证模板

要检查模板文件的语法错误,可以使用 aws cloudformation validate-template 命令。

注意

aws cloudformation validate-template 命令仅用于检查模板的语法。它不能确保您为资源指定的属性值对该资源有效,也无法确定创建堆栈时将存在的资源数量。

要检查操作有效性,需要尝试创建堆栈。Amazon CloudFormation 堆栈没有沙盒或测试区域,因此您需要为测试期间创建的资源付费。

在验证期间,Amazon CloudFormation 首先检查模板是否是有效的 JSON。如果不是,CloudFormation 会检查模板是否是有效的 YAML。如果两种检查都失败,CloudFormation 会返回模板验证错误。您可以使用 --template-body 参数在本地验证模板,也可以使用 --template-url 参数进行远程验证。

有效模板

以下示例传递 --template-url 参数,以在远程位置验证模板:

aws cloudformation validate-template --template-url https://s3.amazonaws.com/cloudformation-templates-us-east-1/S3_Bucket.template

以下是上一条命令的输出。

{
    "Description": "Amazon CloudFormation Sample Template S3_Bucket: Sample template showing how to create a publicly accessible S3 bucket. **WARNING** This template creates an S3 bucket.
You will be billed for the Amazon resources used if you create a stack from this template.",
    "Parameters": [],
    "Capabilities": []
}

预期结果是无错误消息,并且列出所有参数的相关信息。

验证错误

此部分在运行 aws cloudformation validate-template 命令时会产生验证错误。

以下示例传递 --template-body 参数,以在本地验证模板。

aws cloudformation validate-template --template-body file:///home/local/test/sampletemplate.json

以下是上一条命令的输出。

{
    "ResponseMetadata": {
        "RequestId": "4ae33ec0-1988-11e3-818b-e15a6df955cd"
    },
    "Errors": [
        {
            "Message": "Template format error: JSON not well-formed. (line 11, column 8)",
            "Code": "ValidationError",
            "Type": "Sender"
        }
    ],
    "Capabilities": [],
    "Parameters": []
}
A client error (ValidationError) occurred: Template format error: JSON not well-formed. (line 11, column 8)

预期结果是一条错误消息,其中列出有关错误的信息。