3

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?

MaxiPalle
  • 410
  • 1
  • 6
  • 15
  • Can you provide some examples of what the max_start_date should be for different dates? – Helder Sepulveda Aug 31 '21 at 14:02
  • Terraform doesn't have much support for date calculations. You are probably better off calculating that outside of Terraform and passing it in as a variable. Also it's not clear at all what you mean by "the first day of the month within the next 12 months". – Mark B Aug 31 '21 at 14:02
  • @MarkB there is a lot we can do with the provided functions... like for example the first day of next month can be done: `timeadd(formatdate("YYYY-MM-01'T'00:00:00Z", timestamp()), "744h")` ... we just need clarification on the question – Helder Sepulveda Aug 31 '21 at 14:20
  • the 8640 Hours is 360 Days (a year?!?). maybe the ask is for the first day of the month a year from today – Helder Sepulveda Aug 31 '21 at 14:25

2 Answers2

6

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

Helder Sepulveda
  • 15,500
  • 4
  • 29
  • 56
0

You could use the time_rotating resource, set rotation_years=1, then import it as existing on the first day of the current month.

TeamDman
  • 649
  • 6
  • 26