How can I get current date in a CloudFormation script?

0 votes

I am tagging my resources using Tags in my cfn script:

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

I would like to create a tag with the current date as per the example above. Is it possible?

Aug 29, 2018 in AWS by bug_seeker
• 15,350 points
884 views

1 answer to this question.

0 votes

The advice by @Guy is correct, 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"
   }
}
],

It is not trivial to automatically assign the current time if you create the stack from the AWS console, but if you use the CLI tools you can call cfn-create-stack like this:

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

Hope this helps!

answered Aug 29, 2018 by Priyaj
• 56,520 points

Related Questions In AWS

0 votes
1 answer

How do I disable detailed monitoring for instances in an auto scaling group in a CloudFormation template?

The property you want is InstanceMonitoring, not ...READ MORE

answered Aug 31, 2018 in AWS by Archana
• 4,090 points
46 views
+1 vote
2 answers

How do I get my AWS Glue client in JAVA?

Hey, you've been using a correct code ...READ MORE

answered Apr 17, 2018 in AWS by Cloud gunner
• 4,260 points
892 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How can I disable a user's password in AWS using boto3

delete_login_profile is the one you should use if ...READ MORE

answered Sep 27, 2018 in AWS by Priyaj
• 56,520 points
379 views