DEV Community

Nandan K
Nandan K

Posted on

Terraform File Structure

Day 6 of #30daysofawsterraform challenge

Today I come across file structure in Terraform.

  1. Why file structure is important in Terraform?
    Separate file for versions, providers, resources, variables and outputs ensures the infrastructure is maintainable, collaborative, error-free, and production-ready.

  2. Basic form of file structure:

    terraform-project/

    ├── main.tf → Primary configuration (providers + resources)
    ├── variables.tf → Input variables
    ├── outputs.tf → Output values
    ├── provider.tf → (optional) provider & version constraints
    ├── terraform.tfstate → (auto-generated) state file ⚠ NEVER commit to Git
    ├── .gitignore → Ignore secrets and state files
    └── README.md → Documentation

    In the enterprise level, as the environments like Dev, Staging, Production grows the complexity of file structure also increases. But Terraform manages the complex file structure in most robust way.

    Eg:
    terraform-project/

    ├── environments/
    │ ├── dev/
    │ │ ├── backend.tf → Remote state config (S3, etc.)
    │ │ ├── terraform.tfvars → Non-secret variable values
    │ │ └── main.tf
    │ │
    │ └── prod/
    │ ├── backend.tf
    │ ├── terraform.tfvars
    │ └── main.tf

    ├── modules/
    │ ├── vpc/
    │ ├── ec2/
    │ ├── s3/
    │ └── rds/

    ├── main.tf → Calls modules (if you keep global module calls)
    ├── variables.tf
    ├── outputs.tf
    ├── versions.tf → Core + provider version constraints (best practice)
    ├── locals.tf → Local values (computed vars)
    ├── data.tf → Data sources
    └── .gitignore

Devops #Terraform #AWS

Thanks to Piyush sachdeva The CloudOps Community

Top comments (0)