Home Infrastructure as Code (IaC) Understanding Files and Directories Created by Terraform Commands

Understanding Files and Directories Created by Terraform Commands

298
0

When you work with Terraform, your project directory typically contains files you’ve created (like main.tf) plus several files and directories that Terraform creates or updates automatically as part of its workflow.

Let’s look at what happens step by step.


⚙️ 1️⃣ terraform init

✅ Purpose

terraform init initializes your working directory — it downloads required providers, sets up local state files, and prepares Terraform to run.

📁 Files and Directories Created/Updated

File/FolderPurpose
.terraform/ (directory)Stores downloaded provider plugins and module caches. This folder contains the binaries Terraform uses to interact with cloud providers.
.terraform.lock.hclLock file that records the exact provider versions used, ensuring consistent builds across environments. Terraform will reuse these versions next time you run init.

Example directory structure after terraform init:

terraform-azure-vm/
├── main.tf
├── .terraform/
│   ├── providers/
│   │   └── registry.terraform.io/
│   │       └── hashicorp/
│   │           └── azurerm/...
├── .terraform.lock.hcl

💡 Tip: You should commit .terraform.lock.hcl to version control, but never commit .terraform/ — it’s environment-specific.


🧮 2️⃣ terraform plan

✅ Purpose

terraform plan creates an execution plan — a preview of what Terraform will do when you apply the configuration.

📁 Files and Directories Created/Updated

File/FolderPurpose
(optional) tfplan (or any filename you provide)If you run terraform plan -out=tfplan, Terraform saves the plan to this binary file for later use in terraform apply tfplan.
No new directoriesThe .terraform/ directory remains unchanged.

Example usage:

terraform plan -out=tfplan

💡 Tip: Saving a plan file allows you to review and apply exactly the same plan later — great for CI/CD pipelines and audits.


🧱 3️⃣ terraform apply

✅ Purpose

terraform apply executes the plan — it creates, updates, or deletes resources in your cloud provider as per your configuration.

📁 Files and Directories Created/Updated

File/FolderPurpose
terraform.tfstateThe most important file. It records the current state of your infrastructure — what Terraform created and the metadata for each resource.
terraform.tfstate.backupA backup of the previous state file, automatically created each time Terraform updates the state.

Example structure after terraform apply:

terraform-azure-vm/
├── main.tf
├── .terraform/
├── .terraform.lock.hcl
├── terraform.tfstate
├── terraform.tfstate.backup

💡 Tip:

  • Never edit the state file manually.
  • If you’re working in a team, use remote state storage (like Azure Blob, S3, or Terraform Cloud) to avoid conflicts.

🧹 4️⃣ terraform destroy

✅ Purpose

terraform destroy removes all resources created by the configuration.

📁 Files and Directories Created/Updated

File/FolderPurpose
terraform.tfstateUpdated to reflect that resources have been deleted.
terraform.tfstate.backupBackup created before destroying resources.

After a full destroy, your Azure resources are gone, but these local state files remain so Terraform still knows what used to exist.


🔁 5️⃣ (Optional) terraform refresh

✅ Purpose

terraform refresh updates the state file with the latest data from Azure without changing resources.

📁 Files and Directories Created/Updated

File/FolderPurpose
terraform.tfstateUpdated with new metadata or configuration values fetched from Azure.
terraform.tfstate.backupBackup created before the refresh.

🧠 Summary Table

CommandCreates / UpdatesDescription
terraform init.terraform/, .terraform.lock.hclSets up provider plugins and dependency locks
terraform plan(optional) tfplanPrepares execution plan preview
terraform applyterraform.tfstate, terraform.tfstate.backupDeploys and records infrastructure state
terraform destroyUpdates terraform.tfstate and backupRemoves infrastructure and updates state
terraform refreshUpdates terraform.tfstate and backupSyncs local state with actual cloud state

📘 Best Practices

  • ✅ Commit .tf, .tfvars, and .terraform.lock.hcl files to version control
  • 🚫 Don’t commit .terraform/ or .tfstate files
  • 🗂️ Use remote backends for shared or production environments
  • 🔐 Secure your state file — it can contain sensitive information (like connection strings or secrets)
Previous articleUnderstanding the Terraform Code for Resource Group, Virtual Network, and Subnet Creation in Azure
Next articleSetting Up Visual Studio Code for Terraform
Heartin Kanikathottu
As a seasoned Cloud and Security Architect, I’ve led transformative initiatives in key roles, including Vice President at Morgan Stanley, Principal Architect at Societe Generale, and Tech Lead & Cloud Security Architect at VMware, among others. I’m also an internationally published author with multiple books available on platforms like Amazon and O'Reilly. Notably, one of my books was recognized as the 8th best cloud computing book of all time in 2020, reflecting the impact of my contributions to the field. With over 15 professional certifications from providers such as Microsoft (Azure), Amazon (AWS), Oracle (Java), Pivotal (Spring), and IBM, I bring a wealth of expertise to my work. Academically, I hold dual Master’s degrees in Cloud Computing and Data Analytics. I’m passionate about sharing knowledge and mentoring others, which is why I actively speak at global technical forums such as Tech Opportunities Fest at Platform Calgary, Google's Kubernetes Meetup, Java User Group, Elasticsearch Meetup, and the Agile India Conference.

LEAVE A REPLY

Please enter your comment!
Please enter your name here