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

相关问题