how/state
What lives inside the state file, why serial and lineage are there, and why a password from HCL ends up as plain text in it. The full JSON, layer by layer.
The state file is Terraform's private database about your infrastructure.
Not a transactional Postgres, just JSON, usually terraform.tfstate.
Why you need it:
aws_s3_bucket.demo) with a real object in the cloud
(by id = "linuxlab-3f4a"). Without this, Terraform would not know which bucket it had created.plan does not poke the cloud without need.Press ▶, we will walk through the file layer by layer. The reference is tf-state.
terraform.tfstate sits in the project root next to main.tf. By default it is
plain JSON on disk. After apply, an entry for each resource appears in it.
Let's open it.
recap
The main things about state:
terraform.tfstate.
If state is broken or lost, Terraform loses its connection to reality (see tf-state-import).serial grows on every apply. lineage is a UUID that never changes
(it is generated when the state is first created). These two fields protect against
conflicts with a remote backend.dependencies are written into state, not computed on the fly. If you ran
terraform state rm, you also dropped the dependencies. That is why state rm is dangerous.Next: tf-drift, what happens when state and reality drift apart.