# Terraform: "Invalid resource instance data in state" means state and schema disagree

## What you are seeing

```
Error: Invalid resource instance data in state

aws_instance.web: data in state didn't match the schema ...
```

The state file contains attributes the current provider schema does not recognize, or is missing ones it requires. Typical causes: provider upgraded/downgraded between writes, state hand-edited, or state written by a different tool (OpenTofu vs Terraform) with schema differences.

## First response

1. Check what changed: provider version in the lock file vs what wrote the state. `terraform state pull | head` shows the state's terraform_version and provider versions.
2. If a provider upgrade caused it, the provider's upgrade guide usually covers state migration. Upgrading forward through the versions (not skipping majors) lets each version migrate the state.
3. If the state was hand-edited, `terraform state pull > backup.json`, fix the JSON to match the schema (or restore the backup and redo the edit properly with `terraform state mv`/`rm`), then `terraform state push` the fixed file. Validate with `terraform plan` afterwards.

## Rules for agents

1. Always `terraform state pull` to a backup before any manual state surgery. State push overwrites; there is no undo without the backup.
2. Never downgrade a provider to "fix" state written by a newer provider. Newer state schemas are not backward compatible; downgrading makes it worse.
3. `terraform refresh` (or plan with refresh) can sometimes reconcile minor mismatches by re-reading real objects. Try it before surgery.
4. If the state is genuinely corrupt beyond repair, the recovery path is: backup, remove the broken entries (`terraform state rm`), re-import the real objects. Slow but safe.
