There was a similar question asked on StackOverflow, and a really good solution that worked for me.
You can access the creation timestamp of the stack from the stack properties.
If you still need to specify tags as parameters then you can do it the following way. Currently the JSON syntax supports an extremely limited set of functions. Because of this the possibilities for dynamically modifying your templates are very tiny. The only way I see to introduce this the tag you want is by adding another parameter to the template itself. Depending on the way you initialize the stack, you can script the parameter to be specified dynamically or provide it in the web console.
For example, if you have this in your template:
"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"
}
},
You can later reference it using the Ref keyword in the tags like this:
"Tags" :
[ {
"Key" : "Owner",
"Value" : "my name"
},
{
"Key" : "Name",
"Value" : "instance name"
},
{
"Key" : "DateCreated",
"Value" :
{
"Ref" : "CreationDate"
}
}
],
cfn-create-stack
MyStack --template-file
My.template --parameters
"CreationDate=$(date +'%F %T')"
Hope this helps!
Reference: https://stackoverflow.com/questions/15637947/how-can-i-get-current-date-in-a-cloudformation-script
To find out more about Google Cloud, join Google Cloud training today.
Thanks!