2013-03-26 42 views
7

我在我的CFN腳本中使用標籤標註我的資源:如何在CloudFormation腳本中獲取當前日期?

"Tags" : [ { "Key" : "Owner",  "Value" : "my name" }, 
      { "Key" : "Name",  "Value" : "instance name" } 
      { "Key" : "DateCreated", "Value" : <something goes here> } 
     ], 

我想創建一個標籤與當前日期按照上面的例子。可能嗎?

+0

您不需要此值的標籤;您可以從describe-instance命令獲取啓動日期:http://docs.aws.amazon.com/AWSEC2/latest/CommandLineReference/ApiReference-cmd-DescribeInstances.html – Guy 2013-03-26 17:42:09

回答

5

@Guy的建議是正確的,您可以從堆棧屬性中訪問堆棧的創建時間戳。

如果您仍然需要指定標籤作爲參數,那麼您可以按照以下方法進行操作。目前JSON語法支持極其有限的set of functions。正因爲如此,動態修改模板的可能性非常小。我看到介紹這個標籤的唯一方法就是向模板本身添加另一個參數。根據初始化堆棧的方式,您可以動態指定要指定的參數,或者在Web控制檯中提供參數。

舉例來說,如果你有這樣的模板:

"Parameters" : { 
    "CreationDate" : { 
     "Description" : "Date", 
     "Type" : "String", 
     "Default" : "2013-03-20 21:15:00", 
     "AllowedPattern" : "^\\d{4}(-\\d{2}){2} (\\d{2}:){2}\\d{2}$", 
     "ConstraintDescription" : "Date and time of creation" 
    } 
    }, 

可以使用ref關鍵字後面引用它的標籤是這樣的:

"Tags" : [ { "Key" : "Owner",  "Value" : "my name" }, 
      { "Key" : "Name",  "Value" : "instance name" }, 
      { "Key" : "DateCreated", "Value" : { "Ref" : "CreationDate" } } 
      ], 

這是不平凡的自動如果您從AWS控制檯創建堆棧,則指定當前時間,但如果使用CLI工具,則可以這樣調用cfn-create-stack:

cfn-create-stack MyStack --template-file My.template --parameters "CreationDate=$(date +'%F %T')" 

希望這有助於!

+1

這不僅僅是實例 - 這種方法也適用於卷,AMI,您可以標記的任何其他內容 - 謝謝。 – chris 2013-03-27 14:57:19

相關問題