Terraform: How to compare 2 dates

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

Overview

In the world of infrastructure as code, Terraform stands out for its simple syntax and powerful functions, providing you the ability to efficiently manage your infrastructure. One lesser-discussed but highly useful feature is Terraform’s ability to compare dates. This functionality can be invaluable for a variety of tasks, from managing resource lifecycles to implementing compliance checks. This tutorial will guide you through comparing dates in Terraform step by step, with code examples ranging from basic to advanced.

Understanding Terraform’s Date Functionality

Before diving into comparisons, it’s important to understand how Terraform handles dates. Terraform uses the RFC 3339 format for dates, which resembles ‘YYYY-MM-DDTHH:MM:SSZ’. You can manipulate and compare these dates using built-in functions.

Getting the Current Date and Time

timestamp()

This function returns the current UTC timestamp in RFC 3339 format.

Basic Comparison: Checking Which Date is Newer

To compare two dates in Terraform, you can use the timeadd function combined with conditional checks. Here’s a straightforward example to determine if one date is newer than another:

locals {
  current_time = timestamp()
  future_time = timeadd(timestamp(), "24h")
}

output "is_future_time_newer" {
  value = local.current_time < local.future_time
}

This example will always return true because future_time is 24 hours ahead of current_time.

Comparing Date Differences

More complex comparisons might involve determining the exact difference between two dates. While Terraform does not provide a direct method to do this, you can leverage the timeadd function creatively. Here’s how:

locals {
  past_date = "2023-01-01T00:00:00Z"
  comparison_date = "2023-03-01T00:00:00Z"
  difference_in_days = floor((timeadd(local.comparison_date, "-0s") - timeadd(local.past_date, "-0s")) / 86400)
}

output "difference_in_days" {
  value = local.difference_in_days
}

This code calculates the difference between two dates in days. Since Terraform handles date arithmetic in seconds, you need to divide the result by 86400 (the number of seconds in a day).

Advanced Example: Age of Infrastructure

For more advanced scenarios, such as determining the age of a particular piece of infrastructure, you can use the Terraform aws_instance resource in conjunction with date comparison techniques. For example:

resource "aws_instance" "my_instance" {
  # Your instance configuration here
}

locals {
  instance_creation_date = aws_instance.my_instance.creation_date
  current_date = timestamp()
  age_in_days = floor((timeadd(local.current_date, "-0s") - timeadd(local.instance_creation_date, "-0s")) / 86400)
}

output "instance_age" {
  value = format("The instance is %d days old.", local.age_in_days)
}

This code snippet demonstrates how to calculate the age of an AWS instance in days by comparing its creation date with the current date.

Conclusion

Comparing dates in Terraform, although it doesn’t support direct date difference functions, can be achieved through clever use of the functions it does provide. Whether you’re determining the newer of two dates, calculating differences, or assessing the age of your infrastructure, the flexibility of Terraform’s function syntax makes these tasks possible. With the basics covered in this tutorial, you can now incorporate date comparisons into your Terraform configurations to add more dynamic and intelligent behavior to your infrastructure management tasks.