How to calculate with dates in Terraform

Viewed 1499

Aaccording to Terraform documentation I am trying to calculate a max. start date in Terraform, which has to be the first day of the month within the next 12 monthes.

I found this post which let me start to implement it like the following example:

  locals {
    current_time           = timestamp()
    today                  = formatdate("YYYY-MM-DD", local.current_time)
    max_start_date         = formatdate("YYYY-MM-DD", timeadd(local.today, "8640h")) # max. 360 days 
    ...

But now I'm lost. I need to create the maximum start date which has to be the first day of the month within a 12 month time range ...

Any Ideas how to solve this?

1 Answers

There is a lot we can do with the provided functions:

for example if I wanted to get the first day of the current month I could do:
formatdate("YYYY-MM-01", timestamp())
output of that > "2021-08-01"

Now if we want to add to that we need to format it in a way that will work for timeadd:
timeadd(formatdate("YYYY-MM-01'T'00:00:00Z", timestamp()), "24h")
output of that > "2021-08-02T00:00:00Z"


From your code:
formatdate("YYYY-MM-DD", timeadd(local.today, "8640h")) # max. 360 days
if all you just need is the first day, just do "YYYY-MM-01"
also keep in mind that 360 days is not a full year:
https://www.google.com/search?q=hours+in+a+year

Related