Hi. Our Nomad jobs have references to files in templates:
template {
data = file("./service-config.yml")
}
However in terraform, it doens’t want to change the file when the file changes:
resource "terraform_data" "service_config" {
input = file("./service-config.yml")
}
resource "nomad_job" "service" {
jobspec = file("./service.nomad.hcl")
hcl2 { allow_fs = true }
depends_on = [terraform_data.service_config]
}
When changnig service_config, nothing happens.
What can we do so that changing the yaml file triggers a change in the nomad job specification? I do not want to have the job file templated by terraform, the job file should still be usable with raw nomad job run
commands.
Thanks.
After a lot of research, I have tried the following:
- Using replace_triggered_by
resource "terraform_data" "service_config" {
input = file("./service-config.yml")
}
resource "nomad_job" "service" {
jobspec = file("./service.nomad.hcl")
replace_triggered_by = [terraform_data.service_config]
hcl2 { allow_fs = true }
}
^^ This is bad. It cases terraform to STOP the job with one api, and then start it. It completely conflicts with canary updates, as the job is first getting stopped. This is not acceptable. The job needs to be updated with a new version, never stopped.
- Using empty variable
locals {
varmark = <<EOF
variable "mark" {
type = string
default = ""
description = "This is a trick to make terraform track changes of external files of Nomad files"
}
EOF
}
resource "nomad_job" "alloy" {
jobspec = "${file("${path.module}/alloy.nomad.hcl")}${local.varmark}"
hcl2 {
allow_fs = true
vars = { mark = file("${path.module}/alloy_config.alloy") }
}
}
^^ This causes the job to show up in terraform to be changed every single run after run, but actually causes no additional side effects. Nomad does nothing, because the actual job deifintion did not change and variable mark
is not used within the job. Additionally, terraform output shows changes in the file referenced by mark.
However, the job in terraform is changed every single time every single execution, which makes it not possibel to track changes.
- Adding the other job as a comment to the HCL spec.
This also doesn’t work. It looks like the plugin strips out any comments in HCL before posting.
I would be kindly interested in any other ideas to make it work. Would a change_triggered_by
pull request be accepted or is in plans in terraform?