Terraform symlink trigger might not act as you expected

ukitdog
1 min readJan 3, 2025

--

Background

My company needs to inject some shared libraries into the subdirectory. To avoid copy-and-paste issues, we are using simple symlinks to link the shared libraries to each subdirectory, ensuring they share the same version without needing to touch the git sub-module.

Expected

Each time we update the source code or shared libraries, Terraform should monitor the changes and apply them to remote.

Actual

symlink directory inside the subdirectory will always be the same.

Terraform state will only amended if the source code has been updated.

Resolution

We need to watch the original directory of the symlink and the source code.

Here is the sample code


resource "null_resource" "run"{

triggers = {
source_dir_sha = sha1(join("", [for f in fileset("dir/source_code", "**"): filesha1("source_code/${f}")]))
symlink_dir_sha = sha1(join("", [for f in fileset("dir/shared_libs", "**"): filesha1("shared_libs/${f}")]))
}

}

Why

We always thought Terraform would pick up the symlink like the real file.

However, it just considers the symlink as a link, any change from destination will not be watched by Terraform

Bonus

Always use `openssl sha1` to check the shasum in MacOS and Linux, which will return the same result. While `shasum` might not return the same result in difference OS.

Reference

  1. https://stackoverflow.com/a/57931341/2876087
  2. https://www.geeksforgeeks.org/tar-command-linux-examples/
  3. How to installing sha1sum in MAC OS?
  4. https://formulae.brew.sh/formula/md5sha1sum

--

--

ukitdog
ukitdog

No responses yet