How to manipulate date and time with Terraform

How to manipulate date and time with Terraform

Photo by drz on Unsplash


The other day I came across a question regarding how to manipulate date and time with Terraform. I never needed to do it before, but it is a good question, actually.

To manipulate it, you have to use the built-in function formatdate along with timestamp and timeadd, so you have the ability to generate the current date and also add or remove time from it.

Final code: github.com/chgasparoto/youtube-cleber-gaspa..

Let's start creating a timestamp from now. I'll be using Terraform version 0.15.0, but it works to the previous version as well.

terraform {
  required_version = "~> 0.15"
}

locals {
  now = timestamp()
}

output "locals" {
  value = {
    "now" = local.now, # 2021-04-20T04:12:00Z
  }
}

One important detail is that the returned timestamp above is in UTC (00:00), with no timezone set.

We have our timestamp from the current moment, and we can manipulate it as we want using the formatdate function. Let's see how it works to format a date to the Brazilian standards.

locals {
  now = timestamp()

  date_br = formatdate("DD/MM/YYYY", local.now)
}

output "locals" {
  value = {
    "now"     = local.now, # 2021-04-20T04:12:00Z,
    "date_br" = local.date_br, # 21/04/2021,
  }
}

The formatdate basically works as most libraries out there in terms of what it accepts as its argument to get the desirable format. You can find the full table here. The Brazilian official time is -3 hours compared to the UTC, and I'm based in the Netherlands, which is +2 hours. Let's take a look at how we can make this adjustment in our time.

locals {
  now = timestamp()

  brasilia_tz  = timeadd(local.now, "-3h") # Brazil's official time
  amsterdam_tz = timeadd(local.now, "2h") # Netherlands official time

  date_br = formatdate("DD/MM/YYYY hh:mm", local.brasilia_tz)
  date_nl = formatdate("D-MM-YYYY hh.mm", local.amsterdam_tz)

  date_utc = formatdate("YYYY-MM-DD", local.now)
}

output "locals" {
  value = {
    "now"          = local.now # 2021-04-20T04:12:00Z,
    "brasilia_tz"  = local.brasilia_tz # 2021-04-20T01:12:00Z,
    "amsterdam_tz" = local.amsterdam_tz # 2021-04-20T06:12:00Z,
    "date_br"      = local.date_br # 21/04/2021 01:12,
    "date_nl"      = local.date_nl # 21-04-2021 06.12,
    "date_utc"     = local.date_utc # 2021-04-20,
  }
}

With that in mind, you can use it to concatenate and generate names and/or properties based on your current date and time. Example of a bucket name:

terraform {
  required_version = "~> 0.15"
}

resource "random_pet" "bucket" {}

locals {
  now = timestamp()

  brasilia_tz  = timeadd(local.now, "-3h")
  amsterdam_tz = timeadd(local.now, "2h")

  date_br = formatdate("DD/MM/YYYY", local.brasilia_tz)
  date_nl = formatdate("D-MM-YYYY", local.amsterdam_tz)

  date_utc = formatdate("YYYY-MM-DD", local.now)
}

output "bucket_name" {
  value = "${random_pet.bucket.id}-${local.date_utc}"
}

output "locals" {
  value = {
    "now"          = local.now,
    "brasilia_tz"  = local.brasilia_tz,
    "amsterdam_tz" = local.amsterdam_tz,
    "date_br"      = local.date_br,
    "date_nl"      = local.date_nl,
    "date_utc"     = local.date_utc
  }
}

output "pet" {
  value = random_pet.bucket.id
}

That's it, friends. I hope you have liked it. You can find me on Twitter and also on Youtube (Only Portuguese for now).