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/Folder | Purpose |
|---|---|
.terraform/ (directory) | Stores downloaded provider plugins and module caches. This folder contains the binaries Terraform uses to interact with cloud providers. |
.terraform.lock.hcl | Lock 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/Folder | Purpose |
|---|---|
(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 directories | The .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/Folder | Purpose |
|---|---|
terraform.tfstate | The most important file. It records the current state of your infrastructure — what Terraform created and the metadata for each resource. |
terraform.tfstate.backup | A 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/Folder | Purpose |
|---|---|
terraform.tfstate | Updated to reflect that resources have been deleted. |
terraform.tfstate.backup | Backup 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/Folder | Purpose |
|---|---|
terraform.tfstate | Updated with new metadata or configuration values fetched from Azure. |
terraform.tfstate.backup | Backup created before the refresh. |
🧠 Summary Table
| Command | Creates / Updates | Description |
|---|---|---|
terraform init | .terraform/, .terraform.lock.hcl | Sets up provider plugins and dependency locks |
terraform plan | (optional) tfplan | Prepares execution plan preview |
terraform apply | terraform.tfstate, terraform.tfstate.backup | Deploys and records infrastructure state |
terraform destroy | Updates terraform.tfstate and backup | Removes infrastructure and updates state |
terraform refresh | Updates terraform.tfstate and backup | Syncs local state with actual cloud state |
📘 Best Practices
- ✅ Commit
.tf,.tfvars, and.terraform.lock.hclfiles to version control - 🚫 Don’t commit
.terraform/or.tfstatefiles - 🗂️ Use remote backends for shared or production environments
- 🔐 Secure your state file — it can contain sensitive information (like connection strings or secrets)