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" : "Brand",
   "Value" : "brand name"
},
{
   "Key" : "Product",
   "Value" : "product name"
}
{
   "Key" : "Product sold",
   "Value" : <sold product data>
}
],

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,520 points
8,814 views

1 answer to this question.

0 votes

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!

answered Aug 29, 2018 by Priyaj
• 58,100 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,170 points

edited Jun 16, 2023 by Khan Sarfaraz 1,214 views
0 votes
1 answer
0 votes
1 answer
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
• 58,100 points
3,004 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP