0
0
Terraformcloud~10 mins

Why state should not be edited manually in Terraform - Visual Breakdown

Choose your learning style9 modes available
Process Flow - Why state should not be edited manually
Terraform State File
Terraform Commands
Read/Write State
Infrastructure Changes
State Reflects Reality
State File Modified
Terraform Detects Mismatch
Errors or Unexpected Behavior
Infrastructure Drift or Failure
Terraform uses a state file to track infrastructure. Manual edits can cause mismatches leading to errors or wrong changes.
Execution Sample
Terraform
# Terraform state file snippet
{
  "resources": [
    {"type": "aws_instance", "id": "i-123456"}
  ]
}

# Manual edit changes id to "i-654321"
Shows a state file with a resource ID changed manually, causing mismatch.
Process Table
StepActionState File ContentTerraform BehaviorResult
1Terraform reads state file{"resources": [{"type": "aws_instance", "id": "i-123456"}]}Matches actual infrastructurePlan and apply work correctly
2Manual edit changes id to i-654321{"resources": [{"type": "aws_instance", "id": "i-654321"}]}State no longer matches real resourceTerraform plans to replace or errors
3Terraform plan runs{"resources": [{"type": "aws_instance", "id": "i-654321"}]}Detects resource missing or changedShows unexpected changes or errors
4Terraform apply runs{"resources": [{"type": "aws_instance", "id": "i-654321"}]}Attempts to fix mismatchMay destroy and recreate resource or fail
5Manual fix or restore state{"resources": [{"type": "aws_instance", "id": "i-123456"}]}State matches reality againTerraform works as expected
💡 Manual edits cause state and real infrastructure to mismatch, leading to errors or unwanted changes.
Status Tracker
VariableStartAfter Manual EditAfter Terraform PlanAfter Terraform ApplyFinal
resource_idi-123456i-654321i-654321 (mismatch detected)i-654321 (may be replaced)i-123456 (restored)
Key Moments - 3 Insights
Why does Terraform show errors after manual state edits?
Because the state file no longer matches the real infrastructure, Terraform detects a mismatch during plan or apply (see execution_table step 3).
Can manual state edits cause resource destruction?
Yes, if Terraform thinks a resource changed or is missing due to state edits, it may plan to destroy and recreate it (see execution_table step 4).
How to fix problems caused by manual state edits?
Restore the state file to match real infrastructure or use Terraform commands like 'terraform state' to safely modify state (see execution_table step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the resource_id in the state file after manual edit?
Ai-123456
Bi-000000
Ci-654321
DNo resource_id
💡 Hint
Check execution_table row 2 under 'State File Content'
At which step does Terraform detect the mismatch caused by manual edit?
AStep 2
BStep 3
CStep 1
DStep 5
💡 Hint
Look at execution_table row 3 under 'Terraform Behavior'
If the manual edit is undone and state restored, what happens to Terraform behavior?
ATerraform works as expected
BTerraform continues to error
CTerraform plans to destroy resources
DTerraform ignores the state file
💡 Hint
See execution_table row 5 under 'Result'
Concept Snapshot
Terraform keeps a state file to track infrastructure.
Manual edits to this file cause mismatches.
Mismatches lead to errors or unwanted changes.
Always use Terraform commands to change state safely.
Restoring state fixes these problems.
Full Transcript
Terraform uses a state file to remember what resources it manages. When you run Terraform commands, it reads this file to know what exists. If you manually change the state file, Terraform will see differences between the file and the real infrastructure. This causes errors or plans to destroy and recreate resources unexpectedly. The safe way to update state is using Terraform commands, not manual edits. If manual edits cause problems, restore the original state file to fix the mismatch.