Day 6 - Organizing Terraform Files the Right Way

On Day 6, I learned something simple but powerful: Terraform code should not just work, it should also be easy to read.

As projects grow, putting everything into a single main.tf file becomes messy. It may work for a small demo, but once more resources are added, it quickly becomes difficult to maintain. That is where file structure becomes important.

Terraform reads all .tf files in the current directory as a single configuration. This means file names do not control the logic of the deployment. Instead, file names are there to help us organize the code better. Dependencies are handled through references between resources, not because one file comes before another.

For this exercise, I split the configuration into multiple files:

  • backend.tf for remote state configuration
  • provider.tf for the AWS provider
  • variables.tf for inputs
  • locals.tf for reusable values
  • vpc.tf for networking resources
  • storage.tf for S3 resources
  • outputs.tf for output values
  • terraform.tfvars for actual variable values

This structure made the code much easier to understand. Instead of scrolling through one long file, I could go directly to the section I wanted to review or change.

One of the biggest takeaways for me was that Terraform organization is really about maintainability. A clean structure helps with troubleshooting, onboarding, collaboration, and future scaling. Even in a learning project, building good habits early matters.

I also noticed that some values should be handled carefully. For example, using dynamic timestamps in tags can create unnecessary changes in Terraform plans. That was a helpful reminder that even small design choices can affect how stable the code feels over time.

This day helped me move from “writing Terraform that runs” to “writing Terraform that is structured like real infrastructure code.”

Commands I used

terraform init
terraform fmt -recursive
terraform validate
terraform plan
terraform apply

What I learned today

  • Terraform merges all .tf files in a folder
  • File names are for organization, not logic
  • Resources should be grouped by purpose
  • Consistent structure makes code easier to manage
  • Clean file organization is a best practice from the beginning

Implementation Screenshots

  • Folder structure in VS Code


  • terraform fmt -recursive
  • terraform validate
  • terraform plan
  • Successful terraform apply
  • Resources created in AWS Console

  • terraform destroy

Video Reference


Jay

Comments

Popular posts from this blog

ASM Integrity check failed with PRCT-1225 and PRCT-1011 errors while creating database using DBCA on Exadata 3 node RAC

Lock Tables in MariaDB

Life is beautiful