terraform,

Get current working directory name in Terraform

Aug 04, 2022 · 1 min read · Post a comment

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.

Prerequisites

  • Terraform

Solution

You can find three TF filesystem values:

  • path.module: Directory path of the module where path.module is referenced.
  • 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:

basename(abspath(path.module))

Arguably you can get the same results as:

basename(abspath(path.root))
basename(path.cwd)

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.root and path.cwd being used anywhere before.

Conclusion

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.