🌍 Terraform Commands
| Command | Purpose | Key Notes |
|---|---|---|
terraform validate | Validate configuration | ✔ Syntax & logical checks only❌ No cloud/API calls |
terraform fmt | Format code | ✔ Auto-fixes indentation & style |
terraform show | Show current state | 📌 Reads from terraform.tfstate |
terraform providers | List providers | ✔ Shows source & versions |
terraform output | Show outputs | 📌 Useful for IDs, ARNs, URLs |
terraform apply -refresh-only | Refresh state only | ⚠️ Updates state / state file, not .tf |
terraform graph | Dependency graph | 📌 Visualize resource dependencies |
📌 If you run terraform apply directly
| Step | What 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
| Step | Action |
|---|---|
| 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
.tfmanually first