驗證範本 - AWS CloudFormation

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

驗證範本

使用 aws cloudformation validate-template 命令檢查您的範本檔案語法錯誤。

注意

aws cloudformation validate-template 命令旨在僅檢查範本的語法。它不能確保您為資源指定的屬性值對該資源有效。它也不能判斷建立堆疊時將存在的資源數量。

您需要嘗試建立堆疊才能檢查操作有效性。 AWS CloudFormation 堆疊沒有沙盒或測試區,因此將針對您在測試期間建立的資源收費。

在驗證期間, AWS 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": "AWS 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 AWS 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)

預期結果是出現錯誤訊息,並列出有關錯誤的資訊。