What State Is
Your code says resource "aws_vpc" "main". AWS knows a VPC called vpc-0a1b2c3d. Nothing in either says the two belong together. State is the record that does: for every resource address in the code, the id of the real object and the attributes it had after the last apply.
Without state the tool couldn't tell "create a new VPC" from "this VPC already exists", couldn't delete what you removed from the code, and would have to query every object in your account to plan. With it, plan reads state, asks the provider to refresh each recorded object, and diffs code against that.
Looking inside
The local backend keeps state in terraform.tfstate, plain JSON:
mkdir s && cd s
cat > main.tf <<'HCL'
resource "local_file" "motd" {
filename = "motd.txt"
content = "hello\n"
}
resource "terraform_data" "build" {
input = { version = "1.4.0" }
}
output "motd_path" {
value = local_file.motd.filename
}
HCL
tofu init >/dev/null
tofu apply -auto-approve >/dev/null
jq '{version, terraform_version, serial, lineage, outputs: (.outputs | keys)}' terraform.tfstate
jq -c '.resources[] | {mode, type, name, provider, id: .instances[0].attributes.id}' terraform.tfstate
echo "--- apply a change: the serial goes up, the old state becomes the backup"
sed -i 's/1.4.0/1.5.0/' main.tf
tofu apply -auto-approve >/dev/null
jq .serial terraform.tfstate terraform.tfstate.backup{
"version": 4,
"terraform_version": "1.13.2",
"serial": 1,
"lineage": "8fcc1e51-1171-38ef-2699-dc2befeb00a0",
"outputs": [
"motd_path"
]
}
{"mode":"managed","type":"local_file","name":"motd","provider":"provider[\"registry.opentofu.org/hashicorp/local\"]","id":"f572d396fae9206628714fb2ce00f72e94f2258f"}
{"mode":"managed","type":"terraform_data","name":"build","provider":"provider[\"terraform.io/builtin/terraform\"]","id":"e3fc18b8-4d83-ad59-21a2-cb94132dc2ac"}
--- apply a change: the serial goes up, the old state becomes the backup
2
1
| Field | Meaning |
|---|---|
version |
the state format version (4 for years now) |
terraform_version |
the CLI version that last wrote it. OpenTofu writes its own version here |
serial |
increases on every write; used to detect stale plans and conflicting writes |
lineage |
a UUID set when the state is first created; two states with different lineages are unrelated, and the CLI refuses to overwrite one with the other |
resources[] |
one entry per resource block, with instances[] (several for count/for_each) holding all attributes |
outputs |
root module outputs, as last applied |
tofu show prints the same information in HCL style, which is easier to read than the JSON.
Why state is sensitive
State contains every attribute of every resource: generated passwords (random_password.result), private keys (tls_private_key.private_key_pem), database connection strings, sometimes secrets read through data sources. sensitive only hides them in the CLI output (see Variables):
mkdir k && cd k
cat > main.tf <<'HCL'
resource "tls_private_key" "deploy" {
algorithm = "ED25519"
}
HCL
tofu init >/dev/null
tofu apply -auto-approve >/dev/null
jq -r '.resources[0].instances[0].attributes.private_key_pem' terraform.tfstate | head -1
jq -r '.resources[0].instances[0].sensitive_attributes | length' terraform.tfstate-----BEGIN PRIVATE KEY-----
3
The private key is right there in the file. The state even lists which attributes are sensitive (three of them here: the key in its PEM, OpenSSH and PKCS#8 forms), but stores their values in clear text anyway. So:
- Never commit state to Git, never paste it into a ticket.
- Keep it in a backend with encryption at rest, versioning and tight access control (Remote Backends); with OpenTofu, encrypt it client-side as well (State Encryption).
- Prefer designs that keep secrets out of state: managed passwords (
manage_master_user_passwordfor RDS), ephemeral values, secrets fetched by the application at run time.
One state per blast radius
Everything in one state is planned, locked and applied together. A single state for the whole company means slow plans, everyone waiting for the lock, and a typo in a DNS record able to touch the production database. Split states along lines that change independently and have different owners:
| Typical split | Changes |
|---|---|
network (VPC, subnets, NAT) |
rarely |
data (RDS, ElastiCache, S3) |
rarely; most dangerous |
cluster (EKS, node groups) |
sometimes |
app-* (per service: queues, IAM roles, DNS) |
often |
And one state per environment: dev, staging and prod never share a state (Environments).
Test yourself
What is the main job of the state file?
- Store the provider binaries
- Map each resource address in the code to the real object's id and last known attributes
- Cache the plan
- Store variable defaults
A tls_private_key resource is marked sensitive. Is the key in the state file?
- No, sensitive values are skipped
- Yes, in plain text
- Only a hash
- Only with -json
What does the lineage field protect against?
- Two people applying at once
- Overwriting a state with an unrelated one
- Provider upgrades
- Drift