criticalAdvancedSymptom-first#state#corrupt#json#backup

Corrupted local state — JSON parse error in state file

Error: Error loading state: SyntaxError: JSON input

Likely source:data-lossconfiguration

Focused incident mode — strips away everything but this runbook.

Symptoms

  • Error: Error loading state: SyntaxError: JSON input
  • terraform plan fails with state parse error
  • Local state file is corrupted or truncated

Never manually edit terraform.tfstate — use terraform state commands. Always use remote backends for team environments.

Evidence to grab

Collect these artifacts before you change anything — they prove (or rule out) each cause.

  • Parse error output

    terraform plan 2>&1 | head -10
  • State file integrity check

    python3 -c 'import json; json.load(open("terraform.tfstate"))' 2>&1

Diagnostic flow

Run the steps in order, marking each claim confirmed or refuted. The evidence you gather feeds the ranked causes below.

Guided decision tree is ready.

Steps with commands, expected output, and pass/fail branches.

Ranked root causes

Evidence checklist

  • State file is truncated or has partial JSON
  • Process was killed during terraform apply

Confirm before fixing

python3 -c 'import json; json.load(open("terraform.tfstate"))' 2>&1
READ

Validate the state file JSON

Fix

cp terraform.tfstate.backup terraform.tfstate && terraform plan
CHANGE

Restore from the most recent backup

Resolution runbook

Apply from top to bottom after the cause is confirmed. Review every command before running it — risk tags are shown on each.

1
python3 -c 'import json; json.load(open("terraform.tfstate"))' 2>&1
READ

Validate the state file JSON

2
ls -la terraform.tfstate* backup/
READ

Find available backups

3
cp terraform.tfstate.backup terraform.tfstate
CHANGE

Restore from backup

4
terraform state pull > terraform.tfstate.restored
READ

Pull remote state as fallback recovery

5
terraform plan
READ

Verify state is restored and functional

Prevention

  • Use remote backends (S3, GCS, Terraform Cloud) instead of local state.
  • Enable state file versioning on the remote backend.
  • Use CI pipeline locks to prevent concurrent applies.
  • Keep local state backups in .gitignore but monitor state file health.

Commands marked READ CHANGE DESTRUCTIVE — never run DESTRUCTIVE commands against production data you cannot restore. Copy → verify → execute.