DEV Community

Cover image for Day 6/30: Organising Terraform – File Structure Best Practices πŸ“‚βœ¨
GOKULPRASATH N
GOKULPRASATH N

Posted on

Day 6/30: Organising Terraform – File Structure Best Practices πŸ“‚βœ¨

Day 6/30: Organizing Terraform – File Structure Best Practices πŸ“‚βœ¨
Today marks Day 6 of my #30daysofAWSTerraform challenge! πŸš€ As the codebase grows, keeping everything in a single main.tf file becomes unmanageable. Today, I learned how to modularize and organize Terraform files for better readability, maintainability, and scalability.
I refactored my monolithic code into a standard professional structure used by DevOps teams worldwide.

βœ… Tasks Completed:

  1. Refactoring: Split the single main.tf into logical files (providers.tf, variables.tf, outputs.tf).
  2. Backend Isolation: Created a dedicated backend.tf to manage the Remote State configuration separately.
  3. Variable Management: Moved variable definitions to variables.tf and actual values to terraform.tfvars.
  4. Security Best Practices: Created a .gitignore file to prevent sensitive files like terraform.tfvars, .terraform/, and *.tfstate from being pushed to GitHub.

πŸ“ Notes:
Standard File Structure:

  1. main.tf β†’ Core resource definitions (EC2, VPC, S3).
  2. variables.tf β†’ Declaration of input variables.
  3. outputs.tf β†’ Definition of output values (e.g., IPs, IDs).
  4. providers.tf β†’ Provider configuration and version constraints.
  5. terraform.tfvars β†’ Environment-specific values (GitIgnored!). Why split? It makes collaboration easier, code reviews faster, and helps avoid merge conflicts in large teams.

πŸ”— Resources:
Video I watched: https://www.youtube.com/watch?v=QMsJholPkDY
Mentor: Piyush Sachdeva

I am excited to dive into Type Constraints tomorrow to make my variables even more robust!

AWS #Terraform #CloudEngineering #DevOps #InfrastructureAsCode #techtutorialswithpiyush #TheCloudOpsCommunity #30daysofAWSTerraform

Top comments (0)