for_each over tuple in Terraform

for_each over tuples in terraform

Terraform does not allow to iterate through a tuple with for_each, but you can create one in the fly.

locals {
  cron_jobs = [
    {
      name    = "every-15-minutes"
      command = "rake send:sms:notification"
      cron    = "*/15 * * * * *"
    },
    {
      name    = "every-30-minutes"
      command = "rake send:sqs:notification"
      cron    = "*/30 * * * * *"
    }
  ]
}

resource "nomad_job" "cron_jobs" {
  for_each = { for cj in local.cron_jobs : cj.name => cj }

  jobspec = templatefile("cron_job.tpl", {
    name    = each.key
    command = each.value.command
    cron    = each.value.cron
  })
}