验证模板
要检查您的模板文件是否存在语法错误,您可以使用 aws cloudformation validate-template 命令。
aws cloudformation validate-template 的设计用途为仅检查您的模板的语法。它不能保证您已对一项资源指定的属性值对于该资源有效,也不能决定创建堆栈时存在的资源数量。
要检查操作有效性,您需要尝试创建堆栈。没有用于 Amazon CloudFormation 堆栈的沙盒或测试区,因此您需要在测试期间为创建的资源支付费用。
在验证期间,Amazon CloudFormation 首先检查模板是否是有效的 JSON。如果不是,CloudFormation 会检查模板是否是有效的 YAML。如果两种检查都失败,CloudFormation 会返回模板验证错误。您可以使用 --template-body 参数在本地验证模板,也可以使用 --template-url 参数进行远程验证。以下示例验证在远程位置的模板:
PROMPT>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": [] }
预期结果是无错误消息,并且列出所有参数的相关信息。
以下示例显示了本地模板文件出现的错误。
PROMPT> 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)