Terraform – FotD – timestamp()

This is part of an ongoing series of posts documenting the built-in interpolation functions in Terraform. For more information, check out the beginning post. In this post I am going to cover the timestamp() function. The example file is on GitHub here.

What is it?

Function name: timestamp()

Returns: Takes no arguments. Returns the current time in UTC using the RFC 3339 standard.

Example:

# Returns something like 2018-09-05T00:00:00Z
output "timestamp_output" {
  value = "${timestamp()}"
}

Example file:

##############################################
# Function: timestamp
##############################################
##############################################
# Variables
##############################################

##############################################
# Resources
##############################################


##############################################
# Outputs
##############################################
output "1_timestamp_basic" {
  value = "${timestamp()}"
}

Run the following from the timestamp folder to get example output for a number of different cases:

#No arguments for function
terraform apply

Why use it?

What time is it? 4:30, it’s not late, nah, it’s just early. The Spin Doctors really said it best. Seriously though, grabbing the current time is great for logging, file naming, and a host of other things. This function also works hand-in-hand with the timeadd function to get the date in the future or the past.

Lessons Learned

Not gonna lie. There wasn’t much to learn. The most important bits of this function are what is in the official documentation. Every time you run terraform apply, the value is going to change. So if a portion of the resources is using that value, then Terraform will see it as a change. It may be necessary to add ignore_changes as a lifecycle property to avoid the diff.

Coming up next is the timeadd() function.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.