🌍 Terraform Commands

CommandPurposeKey Notes
terraform validateValidate configuration✔ Syntax & logical checks only❌ No cloud/API calls
terraform fmtFormat code✔ Auto-fixes indentation & style
terraform showShow current state📌 Reads from terraform.tfstate
terraform providersList providers✔ Shows source & versions
terraform outputShow outputs📌 Useful for IDs, ARNs, URLs
terraform apply -refresh-onlyRefresh state only⚠️ Updates state / state file, not .tf
terraform graphDependency graph📌 Visualize resource dependencies

📌 If you run terraform apply directly

StepWhat happens
1️⃣Terraform refreshes state automatically
2️⃣Drift is detected
3️⃣Drifted state compared with .tf
4️⃣Plan generated to match .tf
5️⃣Apply → ❌ manual cloud changes reverted

📌 terraform apply -refresh-only

🔁 Resolution Flow

StepAction
1️⃣Manual change detected in cloud
2️⃣Run terraform apply -refresh-only
3️⃣State file gets updated
4️⃣Update .tf files manually
5️⃣Run terraform plan
6️⃣Apply only if plan is clean

Refresh state → Fix code → Plan → Apply

✔ When to use -refresh-only

  • 📌 You want to inspect & sync drift
  • 📌 You don’t want to make infra changes yet
  • 📌 You plan to fix .tf manually first