Specifying a date in the future

Hi @bingerk!

This seems like one of those situations where it can be helpful for a provider to offer a little extra abstraction on top of what the underlying API offers to work better within Terraform’s model. Specifically, to take as an argument a rule for deciding the start_time rather than a literal start_time, which the resource logic could then manage to ensure that it only changes if something else has changed to warrant it. (Assuming I understood correctly the point of this.)

It’s a tricky thing to solve with the Terraform language alone because the Terraform language is designed primarily for describing “stable” values that only vary when their explicit inputs change. Functions like timestamp that bring in external state are there for pragmatic reasons but don’t tend to compose well with other language features.

The best idea I can think of for this is to do some conditional logic based on the current wallclock time so that it does a different calculation depending on whether the current wallclock time is before or after the scheduled run time. Something like this, perhaps:

locals {
  current_time           = timestamp()
  start_wallclock_time   = "7.55"
  current_wallclock_time = formatdate("h.mm", local.current_time)
  schedule_tomorrow      = (local.current_wallclock_time >= local.start_wallclock_time)
  today                  = formatdate("YYYY-MM-DD", local.current_time)
  tomorrow               = formatdate("YYYY-MM-DD", timeadd(local.current_time, "24h"))
  start_time             = "${local.schedule_tomorrow ? local.tomorrow : local.today}T07:55:00Z"
}

With the above, local.start_time should then be a value suitable for the start_time argument you want to populate. I tested this after 07:55 UTC, so I’ve only been able to test the “tomorrow” behavior myself here, but hopefully they both work!

1 Like