Unix timestamp in Terraform?

Hi

Is there any way to output the current time as a Unix timestamp (seconds since 1970-01-01, e.g. 1569594755) in a Terraform file? I’ve looked through all the documentation for the ‘formatdate’ function and while you can do pretty much every other format you could think of, it doesn’t seem to be possible to get the standard unix timestamp of seconds since the epoch. Doesn’t seem to be anything about it in either of the other two time functions (timeadd & timestamp) either, as far as I can see.

Am I missing something?

Many thanks
Rich.

6 Likes

I have the same question. Supporting timestamp epoch would be really useful in querying situations where one wants to report an expired resource requiring rotation, for instance. All existing timestamp functions (timeadd, timestamp and formatdate) return a timestamp string and there’s no type conversion function either.

2 Likes

Would love to revive this discussion. As we get into trying to more advanced terraform use-cases (rotating AMI IDs on a 3 month schedule is what I’m currently working on), small improvements like this one can make a big difference.

Any thoughts from core team members?

I’m using this resource:

locals {
  labels = merge(var.labels, {
    created          = time_static.activation_date.unix
    created_readable = formatdate("YYYY-MM-DD_hh-mm-ss", time_static.activation_date.rfc3339)
  })
}

resource "time_static" "activation_date" {}
1 Like

Ok, thanks @crazy-matt, this looks really useful and I’m pretty sure it’ll do what I wanted. Is this a relatively new provider? I’m pretty sure it wasn’t around when I first asked the question! :slight_smile: Interesting that they’re moving this sort of stuff to a provider though. Feels like it ought to be a core function. But so long as it works I guess.