Sling Academy
Home/DevOps/Terraform: How to add a duration to a date

Terraform: How to add a duration to a date

Last updated: February 03, 2024

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.

Next Article: Terraform: How to compare two strings case-insensitively

Previous Article: Terraform: How to compare 2 dates

Series: Terraform Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide