Saturday, 7 February 2026

Key Terraform Rule in Execution, files, folders and directories

 

 Key Terraform Rule

Terraform loads and merges ALL .tf files in a directory automatically.

There is:

  • ❌ no “main file”

  • ❌ no execution order by filename

  • ✅ one configuration per directory

So:

terraform apply

applies everything in that folder.


✅ How You SHOULD structure your files

๐Ÿ“ Recommended folder structure

terraform-lab/ ├── provider.tf ├── data.tf ├── instance.tf ├── outputs.tf ├── variables.tf

Terraform reads them all together.

LAB- BREAK YOUR MAIN.TF INTO DIFFERENT COMPONENTS

provider.tf

provider "aws" { region = "us-east-1" }

data.tf

data "aws_vpc" "default" { default = true } data "aws_subnets" "default" { filter { name = "vpc-id" values = [data.aws_vpc.default.id] } }

instance.tf

resource "aws_instance" "web" { ami = "ami-0c02fb55956c7d316" instance_type = "t3.micro" subnet_id = data.aws_subnets.default.ids[0] tags = { Name = "terraform-lab" } }

outputs.tf

output "instance_id" { value = aws_instance.web.id }

▶️ Running Terraform

From the directory:

terraform init terraform plan terraform apply

Terraform automatically:

  • loads all .tf files

  • builds the dependency graph

  • applies in the correct order


❌ Common misconception

“Terraform executes files top to bottom”

Wrong.

Terraform:

  • builds a dependency graph

  • executes based on references

  • ignores file order and filenames


๐Ÿง  KEY TAKEAWYS

Terraform directory = one application
.tf files = chapters in the same book

You don’t run chapters — you run the book.


๐Ÿงช Advanced (Optional): Lab separation strategies

Option A — New folder per lab (recommended for beginners)

labs/ ├── lab1-default-vpc/ ├── lab2-alb/ ├── lab3-asg/

Option B — Same folder, comment/uncomment (not ideal)

Option C — Use variables / count (advanced)


⚠️ One important rule

Terraform only reads files in the current directory.

Subfolders are ignored unless you use modules (advanced topic).


✅ 

  • You don’t “apply a file”

  • You apply a directory

  • Terraform merges all .tf files automatically

  • File naming is for human readability only


๐Ÿง  One-sentence takeaway for students

Terraform applies directories, not files.

No comments:

Post a Comment

Key Terraform Rule in Execution, files, folders and directories

   Key Terraform Rule Terraform loads and merges ALL .tf files in a directory automatically. There is: ❌ no “main file” ❌ no exec...