Another probably valuable Terraform nugget to share. Suppose that you want to get the current working directory name only for some reason. There are few helpful built-in references and functions that could help.
You can find three TF filesystem values:
path.module: Directory path of the module where
path.root: Directory path of the root module.
path.cwd: Current working directory.
Presuming I have a module under
/home/devcoops/repos/terraform/aws/modules/network and I need to extract network folder name only. This could be accomplished by:
Arguably you can get the same results as:
path.module is the popular choice and in most cases the right choice, as it’s making the module being agnostic. On top of that, I rarely see
path.cwd being used anywhere before.
If you want to learn more about the differences and use cases, visit Terraform’s official Filesystem and Workspace Info. Tried everything and nothing works? Let me know in the comment section below. On a side note, follow our official channel on Telegram.