In Terraform 0.12 and later, one can use a for expression combined with a fileset function and one of the hashing functions to calculate a combined checksum for files in a directory:
> sha1(join("", [for f in fileset(path.cwd, "*"): filesha1(f)]))
"77e0b2785eb7405ea5b3b610c33c3aa2dccb90ea"
The code above will calculate a sha1
checksum for each file in the current directory that matches the name pattern, join the checksums in a string and, finally, calculate a checksum for the resulting string. So the null_resource
example would look like this with the expression above as a trigger:
resource "null_resource" "deploy_files" {
triggers = {
dir_sha1 = sha1(join("", [for f in fileset("my-dir", "*"): filesha1(f)]))
}
provisioner "file" { ... }
provisioner "remote-exec: { ... }
}
Note that fileset("my-dir", "*")
does not take into account files in sub-directories of my-dir
. If you want to include checksums of those use the name pattern **
instead of *
.