OpenTofu State Management
Intermediatev1.0.0
Manage OpenTofu state effectively — remote backends, state locking, import existing resources, move and remove state entries, and disaster recovery for infrastructure state files.
Content
Overview
State is OpenTofu's source of truth — it maps real infrastructure to your configuration. Master state operations to import existing resources, refactor safely, and recover from state issues.
Why This Matters
- -Infrastructure tracking — state records what OpenTofu manages
- -Team collaboration — remote state with locking prevents conflicts
- -Refactoring — move resources between modules without recreation
- -Recovery — restore from state backup after incidents
How It Works
Step 1: Remote Backend Configuration
Step 2: State Inspection
Step 3: Import Existing Resources
Step 4: Move & Remove
Step 5: Backup & Recovery
Best Practices
- -Always use remote backend with locking for team projects
- -Use moved blocks over state mv (declarative, version-controlled)
- -Backup state before major refactoring operations
- -Run tofu plan after imports to verify alignment
- -Never edit state JSON manually (use state commands)
Common Mistakes
- -Local state for team projects (conflicts, no locking)
- -Manual state JSON editing (corruption risk)
- -Forgetting to plan after import (config may not match)
- -Using state rm when you mean to destroy (resource orphaned)
- -Not backing up state before state mv operations
FAQ
Discussion
Loading comments...