This lesson explores the structure and naming conventions of the Terraform configuration directory for better project organization and configuration management.
In this lesson, we explore the structure and naming conventions of the Terraform configuration directory to optimize project organization and enhance configuration management.Previously, we worked with a single configuration file named local.tf located in the terraform-local-file directory. Below is an example demonstrating how to list the files in this directory along with the contents of local.tf:
Copy
Ask AI
[terraform-local-file]$ ls /root/terraform-local-filelocal.tf
Terraform supports splitting configuration across multiple files. For instance, you can create an additional file named cat.tf that introduces a new resource using the same local_file provider:
Copy
Ask AI
[terraform-local-file]$ ls /root/terraform-local-filelocal.tf # Note: cat.tf is now also present in the directory.
Copy
Ask AI
resource "local_file" "cat" { filename = "/root/cat.txt" content = "My favorite pet is Mr. Whiskers"}
When you apply your configuration, Terraform automatically processes every file in the directory with the .tf extension. This ensures that both pets.txt and cat.txt will be created as specified.
It is also common practice to consolidate resource definitions into one configuration file, often named main.tf. To further organize your configuration, you can use additional files such as:
File
Purpose
variables.tf
To define input variables
outputs.tf
To specify outputs for the configuration
providers.tf
To configure provider settings
These topics will be discussed in detail later in the lesson.Now, let’s continue with practical exercises to explore working with providers.