Terraform function for git top directory

if I am inside a Terraform source code directory and I want to evaluate the git top directory (parent directory which contains the .git directory), then what would be the Terraform way of doing it?

The reason I ask is because, my TF modules are also in the same source tree, and the sub-directories referring to the modules are at varying depths inside the source treee.

I want to fixup the “git top dir” instead of using varying number of ../../../.. from varying locations.

Moving the modules to a separate repo could be one of the solution, but any other “path expansion” way?

Shantanu Gadgil

Hi @shantanugadgil,

If you’re talking about references from one module to another inside a particular repository then the answer is no: the only supported way to declare that one module depends on another within the same repository is to use a relative path starting with either ../ or ./, and there is no way to dynamically generate that path.

If you are using a monorepo you could use symlinks. For example in one project we put our modules into a top level directory and all of our Terraform then uses symlinks to that when pulling in modules (e.g. source="./terraform-modules/somemodule").
If you move the code elsewhere in the tree the source doesn’t need to change but you need to remove and add the symlink again.

1 Like