Terraform: How to convert timestamp to date and vice versa

Updated: February 3, 2024 By: Guest Contributor Post a comment

In the world of automation and infrastructure as code, Terraform stands out as a powerful tool for managing infrastructure through code templates. One common challenge when scripting with Terraform is handling dates and timestamps—a necessity for generating logs, scheduling tasks, and more. This tutorial explores how to convert a timestamp to a date and vice versa in Terraform, with progressively complex examples to guide you from the basics to more advanced applications.

Understanding Timestamps and Dates in Terraform

Before diving into the conversions, it’s vital to grasp what timestamps and dates represent in Terraform and why conversions between them might be needed. A timestamp is a sequence of characters denoting the time a certain event occurred, typically in UNIX time format (i.e., the number of seconds since January 1, 1970). Meanwhile, a date refers to a human-readable representation of a day, month, and year.

Basic Conversion: Timestamp to Date

To start, let’s convert a UNIX timestamp to a readable date format in Terraform. Terraform provides built-in functions to perform this task efficiently.

timestamp()

The timestamp() function returns the current UNIX timestamp. To convert this to a readable date, you can use the formatdate() function.

formatdate("YYYY-MM-DD", timestamp())

Output :

"2023-04-01"

This simple example demonstrates how to convert the current timestamp to a specific date format. The formatdate() function is very flexible and allows you to format the date in various ways.

Advanced Conversion: Date to Timestamp

Moving to a slightly more complex use case, what if you need to convert a specific date back into a UNIX timestamp? Terraform provides a function called timeadd() that can be helpful in such cases, though it’s slightly less straightforward than formatdate().

timeadd("1970-01-01T00:00:00Z", "43830h")

This example effectively converts 43830 hours since the UNIX epoch (January 1, 1970) back into a timestamp. The timeadd() function adds the specified duration to a base time. In this case, converting the date back to a timestamp requires some calculation since you must know the exact duration between the date and the UNIX epoch start.

Intermediate Use Cases: Scheduling and Logs

Now that we’ve covered the basics of converting between timestamps and dates, let’s look at more practical applications. For instance, managing schedules or creating logs with dynamic timestamps.

resource "aws_cloudwatch_log_group" "example" {
  name = formatdate("YYYY-MM-DD", timestamp())
}

This example uses Terraform to create an AWS CloudWatch log group with a name based on the current date. The formatdate() function dynamically generates the date, ensuring log names are organized by creation date.

Advanced Use Cases: Time-based Resource Naming

The ability to manipulate dates and timestamps in Terraform can be extremely powerful, especially for naming resources in a way that incorporates date or time information. This enables better organization and tracking of resources.

resource "aws_instance" "example" {
  tags = {
    Name = "Instance-${formatdate("YYYY-MM-DD-HH-MM-SS", timestamp())}"
  }
}

This more complex example demonstrates attaching a precise timestamp to an AWS instance name. It’s particularly useful for resources that are frequently created and destroyed, as it helps differentiate between instances based on the exact time they were provisioned.

Conclusion

Through this tutorial, we’ve explored how to convert timestamps to dates and vice versa in Terraform, starting from basic examples and moving towards more advanced use cases. Efficiently handling these conversions not only aids in scripting and automation tasks but also enhances infrastructure management by providing clearer logs, better scheduling, and improved resource tracking.