Re-triggering shell script in terraform

Terraform doesn’t have a single primitive for hashing a whole directory, but if you are using Terraform v0.12.8 or later then you can construct such a thing using the new fileset function in conjunction with the filesha256 function:

  triggers = {
    file_hashes = jsonencode({
      for fn in fileset("${path.module}/helm-files", "**") :
      fn => filesha256("${path.module}/helm-files/${fn}")
    })
  }

I used jsonencode here because triggers values are required to be strings, and so JSON encoding was a convenient way to flatten that constructed map into a single string. It doesn’t really matter which encoding you use there, but using JSON may help terraform plan's change output to show you specifically what changed inside that object, because it’s often able to detect when a string contains JSON and show a structural changeset instead of a string-oriented one.

2 Likes