Terraform: How to add a duration to a date

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

Introduction

In Terraform, managing infrastructure often involves dealing with dates and durations, especially when configuring resource lifecycles or setting up expiration dates for tokens and certificates. This tutorial will guide you through the process of adding a duration to a date in Terraform, from basic to advanced levels, with practical code examples and outputs.

Built-In Time Functions in Terraform

Terraform offers a suite of built-in time-based functions that you can use to manipulate dates and durations. Before we dive into adding durations to dates, it’s important to familiarize yourself with these functions:

  • timestamp(): Returns the current UTC timestamp.
  • formatdate(): Formats a timestamp according to a custom layout.
  • timeadd(): Adds a duration to a timestamp.

Basic Example: Adding Hours to the Current Date

Let’s start with a simple example where we add a few hours to the current date and time.

resource "null_resource" "add_hours" {
  triggers = {
    new_date = timeadd(timestamp(), "4h")
  }
}
output "new_date" {
  value = null_resource.add_hours.triggers.new_date
}

This code snippet uses the timeadd() function to add 4 hours to the current timestamp provided by timestamp(), and assigns the result to a trigger in a null_resource. The output then prints the new date and time.

Adding Days to a Specific Date

Moving on to a more complex scenario, let’s add a duration of days to a specific start date.

variable "start_date" {
  description = "The start date in YYYY-MM-DD format"
  type        = string
  default     = "2023-01-01"
}

resource "null_resource" "add_days" {
  triggers = {
    new_date = timeadd(var.start_date, "10d")
  }
}

output "new_date_with_days" {
  value = null_resource.add_days.triggers.new_date
}

In this example, we introduce the concept of variables and how to use them in Terraform. We declared a variable start_date with a default value, and then used the timeadd() function to add 10 days to this date. Again, the result is used as a trigger for a null_resource, and the output displays the new date.

Using timeadd for Complex Durations

Adding a duration to a date can involve not just days or hours, but a combination of different units of time. Terraform’s timeadd() function supports this level of complexity.

resource "null_resource" "complex_duration" {
  triggers = {
    new_date = timeadd("2023-01-01T00:00:00Z", "1w3d5h")
  }
}

output "new_date_complex" {
  value = null_resource.complex_duration.triggers.new_date
}

This code example demonstrates how to add a complex duration—1 week, 3 days, and 5 hours—to a specific timestamp. This functionality becomes especially useful when managing expiration dates for resources with specific operational windows or constraints.

Advanced Scenario: Handling Time Zones

Working with time zones in Terraform can introduce an additional layer of complexity. However, dealing with UTC dates and conversions can be managed effectively with careful planning.

variable "local_time_zone" {
  description = "Your local time zone"
  type        = string
  default     = "America/New_York"
}

resource "null_resource" "time_zone_conversion" {
  triggers = {
    new_date = formatdate("YYYY-MM-DD'T'HH:MM:SS'Z'", timeadd(formatdate("YYYY-MM-DD'T'HH:MM:SS", timestamp(), var.local_time_zone), "12h"))
  }
}

output "new_date_time_zone" {
  value = null_resource.time_zone_conversion.triggers.new_date
}

This advanced example showcases how to add a duration to the current date and time, considering a specific time zone, and then converting the result back to UTC. It involves using both the timeadd() and formatdate() functions to handle the conversion and addition operations.

Conclusion

Manipulating dates and durations in Terraform is a powerful technique that can help manage infrastructure with precision. Through a variety of examples, we’ve explored adding durations to dates, from simple hours to complex combinations of weeks, days, and hours. Understanding and applying these time functions can significantly enhance your Terraform scripting abilities and enable more dynamic infrastructure management.