How does Terraform determine which file contains the information?
sobes.tech AI
Answer from AI
Terraform automatically loads and processes all files with extensions .tf and .tfvars in the directory from which it is run (the working directory). It does not require explicit specification of files.
- Files with
.tfcontain main configuration blocks: resources, providers, variables, outputs, modules, etc. - Files with
.tfvarscontain value definitions for variables declared in.tffiles. Files namedterraform.tfvarsor environment-specific files with.tfvarsextension (e.g.,dev.tfvars) can also be used for automatic loading.
Terraform combines information from all these files to build the final dependency graph and execution plan.
Example directory structure:
/project
├── main.tf # Main resources
├── variables.tf # Variable declarations
├── outputs.tf # Output declarations
├── terraform.tfvars # Default environment variable values
└── modules/
└── vpc/
├── main.tf
├── variables.tf
└── outputs.tf
In this example, when running from /project, Terraform will load main.tf, variables.tf, and outputs.tf. Values from terraform.tfvars will be applied to variables declared in variables.tf. The /modules/vpc module will be loaded if it is called in one of the top-level files.
It is important to note that the order of declarations within a file or between files does not matter for Terraform. It builds a dependency graph and performs operations in parallel if possible, based on the relationships between resources and other configuration elements.