Terraform: How to get the current working directory

Updated: February 3, 2024 By: Guest Contributor Post a comment

Introduction

Terraform, an open-source infrastructure as code software tool created by HashiCorp, enables users to define and provision a datacenter infrastructure using a high-level configuration language known as HashiCorp Configuration Language (HCL), or optionally JSON. One of the essential skills when working with Terraform is understanding how to interact with the filesystem, particularly getting the current working directory, which can be crucial for relative path configurations, module paths, or when programmatically placing files. This tutorial guides you through multiple ways to get the current working directory in Terraform, starting from basic usage to more advanced scenarios.

Understanding the Basics

To start, it’s essential to familiarize yourself with the basic concept of working directories in Terraform. Terraform operates in a working directory, which is where it reads the configuration files, maintains the state, and writes logs. Knowing the current working directory is critical for referencing other files or directories relatively.

In its simplest form, you can get the current working directory by using the path.cwd function. This builtin function returns the current working directory of the Terraform configuration.

output "current_directory" {
  value = path.cwd
}

This code snippet creates an output named current_directory that will display the current working directory when Terraform apply is executed.

Integrating with Modules

As your Terraform configurations grow, you might start breaking them down into modules for better organization. Knowing the current working directory of the module can be invaluable for referencing files relative to the module’s location.

In a module, path.module can be used to get the directory of the module currently being used.

output "module_directory" {
  value = path.module
}

Similar to the previous example, this will print the module’s current directory path when the output is called.

Advanced Path Management

Moving towards more complex scenarios, you might want to do operations based on the current working directory that involves conditions or interpolations. For instance, constructing dynamic paths based on the environment or other conditions.

locals {
  default_config_path = "${path.cwd}/config/default.tfvars"
  prod_config_path = "${path.cwd}/config/prod.tfvars"
}

output "config_path" {
  value = terraform.workspace == "default" ? local.default_config_path : local.prod_config_path
}

This example demonstrates how to use the ternary operator along with the path.cwd function to choose between different configuration paths based on the current Terraform workspace. It’s a practical approach for managing environments in Terraform.

Debugging with the Current Working Directory

During the development or debugging of Terraform configurations, printing the current working directory can be a helpful way to ensure your paths are set up correctly. You can easily print out the current directory to the Terraform console for debugging purposes.

terraform console
> path.cwd

This method can be used to quickly verify the current working directory without applying or planning any changes.

Conclusion

Understanding and effectively utilizing the current working directory in Terraform can greatly enhance the flexibility and maintainability of your configurations. Whether it’s for basic path references, module management, or advanced path constructions, the techniques outlined in this tutorial provide a solid foundation for navigatiing and manipulating filesystem paths within your Terraform environment. With this knowledge, you can create more dynamic and adaptable configurations suited to a variety of scenarios.