Inside the state file

Intermediate · 6 min

Open terraform.tfstate and read it: serial, lineage, resource addresses, attributes and the secrets that sit there in plain text.

Before thisInit, validate, plan, apply and destroy in practiceReading a plan and the dependency graph

Spacelift · 15 min · from 0:14

Inside the state file

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by SpaceliftReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • State is a JSON document. Its top level holds a format version, the Terraform version, a serial that rises on every write, a lineage ID that never changes, outputs and resources.
  • Each resource instance is stored under its address, such as `aws_subnet.private["a"]`, with the real object's ID and every attribute the provider returned.
  • `sensitive` only hides a value in CLI output. The value itself is written to state in plain text, along with anything a provider returns, such as a generated password.
  • Read state with `terraform state pull`, `state list` and `state show`. Do not edit the file by hand, because the file's bookkeeping has to stay consistent and the commands do that for you.

The file you were told not to touch

The devops lesson on Terraform says state is Terraform’s memory, that it can hold secrets, and that you must not edit it. That is enough to get through a first week. It stops being enough the day a plan wants to destroy a database and you have to work out why, or a teammate asks whether the backend bucket needs the same access controls as a password vault. Both questions get answered by opening the file and reading it.

What the file looks like

I can’t run Terraform here, so I did not produce this file with terraform apply. I wrote a small state file by hand in the version 4 format that current Terraform and OpenTofu use, with an S3 bucket, a generated password and two subnets created with for_each, and parsed it with a script. The shape is real. The values are made up.

$ python3 inspect_state.py terraform.tfstate
{'version': 4, 'terraform_version': '1.17.2', 'serial': 7, 'lineage': '5d2b0f3e-9a1c-4c1e-8f67-0c1d2e3a4b5c'}
aws_s3_bucket.notes_uploads -> notes-uploads-example-1234 | sensitive_attributes: []
random_password.db -> none | sensitive_attributes: [[{'type': 'get_attr', 'value': 'result'}]]
aws_subnet.private["a"] -> subnet-0a1 | sensitive_attributes: []
aws_subnet.private["b"] -> subnet-0b2 | sensitive_attributes: []

The top level has five parts that matter.

  • version is the format of the file itself. It has been 4 for years.
  • serial goes up by one every time state is written. If you hold a copy with serial 7 and the backend holds serial 9, Terraform knows yours is stale.
  • lineage is a random ID generated when the state was first created. It never changes. If you point a configuration at a state with a different lineage, Terraform stops, because that is a different set of infrastructure.
  • outputs are the root module’s output values.
  • resources is the list that does the work.

Each entry in resources has a mode (managed for a resource, data for a data source), a type, a name and the provider that owns it. Under it, instances holds one object per real thing. A resource with count or for_each has several instances, each with an index_key. That key is how aws_subnet.private["a"] becomes an address, and it is why changing a key looks like destroying one object and creating another. The lesson on count and for_each goes into the consequences.

Inside an instance, attributes is everything the provider reported about the object, as of the last refresh. schema_version tells Terraform whether the provider needs to upgrade that data before use. private is an opaque blob some providers use for their own bookkeeping.

Why every attribute is stored

A plan needs three things: your configuration, the real object, and a record of what Terraform did last time. The record is what lets Terraform tell “this attribute changed in your code” from “this attribute was changed by someone else”. It also holds values nobody typed: the ID AWS assigned, an ARN, the address of a database. Other resources read those through references, and on the next run they must come from somewhere.

The cost is the other half of the file. Providers return what they return, and anything a resource has is in attributes.

What sensitive actually does

Look at the last two lines of my script’s output.

outputs marked sensitive still stored as: Zk3$uPq9!vTn2LwX
random_password.db result in file: Zk3$uPq9!vTn2LwX

The output is flagged "sensitive": true, and the instance lists result under sensitive_attributes. Those markers tell the CLI to print (sensitive value) instead of the text. The string is still in the JSON, unencrypted. So the state file has to be protected like the credential it contains: a private bucket, narrow IAM access, encryption at rest, and no copy in Git. The lesson on sensitive values and secrets in state covers the options, and OpenTofu’s client-side state encryption is covered in a later lesson.

Reading it properly

Do not open the file in an editor and change it. terraform state pull prints the current state from whatever backend you use, so pipe it to jq when you want to look. terraform state list prints every address, and terraform state show ADDRESS prints one resource’s attributes. The next lessons cover how a backend stores the file and the commands that change it safely.

Why not edit it? A typo in a JSON file that every plan depends on is expensive, and a hand edit has to keep the serial, the schema versions and the attributes consistent with each other. The commands do the same work and keep that bookkeeping straight.

About the video

The embed is Spacelift’s “Managing Terraform State Files”, published August 2023, from 0:14 to 5:54. In that span it explains what the state file is, runs an init and apply with local state, and says why the file needs protecting. The rest of the video covers an S3 backend, DynamoDB locking and a Spacelift pitch, which I left out because the next two lessons cover backends and locking on current versions. The video is vendor material and predates ephemeral values and S3-native locking. English was judged from the title, description and the channel’s English auto-caption track, not by ear.

Where this fits

State is a map from addresses in your code to real objects. Everything in this group, from backends to moved blocks, is about keeping that map correct and shared. Next, where the file should live: remote backends on S3, GCS and Azure.

Check yourself

  1. 01What are `serial` and `lineage` for?

    The serial counts writes, so Terraform can tell an older copy of state from a newer one. The lineage is a random ID created with the state, so Terraform can refuse to mix up two unrelated states.

  2. 02A variable is marked `sensitive = true`. Is its value in the state file?

    Yes, if it ended up in any resource attribute or output. The flag redacts the value in plan output and CLI display only. The file still holds it in plain text.

  3. 03Why does the state store every attribute and not just the resource ID?

    Terraform compares your configuration with the last known attributes to decide what changed, and it needs values like computed IDs to feed other resources. Re-reading everything from the cloud on each plan would be slower and could not tell which objects belong to you.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark