State Contents and Protection
The state file is a JSON document that can contain secrets. This lesson describes what it holds and how to protect and share it.
Learning outcomes
After completing this lesson, you will be able to:
- Describe the main contents of the state file, including lineage and serial.
- Explain why state is sensitive and how to protect it.
- Distinguish local from remote state.
What the state file contains
By default the local backend writes state to terraform.tfstate in the root module directory and keeps the previous version in terraform.tfstate.backup. The file is JSON. The following excerpt shows its main parts.
{
"version": 4,
"terraform_version": "1.12.2",
"serial": 7,
"lineage": "3f1c6a2e-8d4b-4b8e-9a51-6c2f1e0d9b77",
"outputs": { "bucket_name": { "value": "acme-logs-witty-fox", "type": "string" } },
"resources": [
{
"mode": "managed",
"type": "aws_s3_bucket",
"name": "logs",
"provider": "provider[\"registry.terraform.io/hashicorp/aws\"]",
"instances": [ { "attributes": { "bucket": "acme-logs-witty-fox", "arn": "arn:aws:s3:::acme-logs-witty-fox" } } ]
}
]
}- lineage
- A unique ID created with the state; states with different lineages are unrelated.
- serial
- A number that increases every time the state is written, so newer and older versions can be told apart.
- mode
- managed for resources, data for data sources.
- instances
- One entry per instance, with every attribute the provider returned.
Terraform uses lineage and serial to refuse dangerous operations, such as pushing an older state over a newer one. The format is internal: never edit state by hand. Use terraform state commands and moved, removed and import blocks to change it.
Secrets in state
Every attribute a provider returns is stored, including database passwords, private keys, access tokens and connection strings, in plain text. Marking a variable or output as sensitive only stops Terraform from printing the value; the value is still in state. HashiCorp's own sample exam question makes this point: usernames and passwords referenced in configuration, even as variables, end up in plain text in state.
The following diagram shows the risk of state in version control and the steps that remove it.

State is sensitive
Treat state as sensitive data. Do not commit terraform.tfstate to version control, store it in a backend that encrypts it at rest and in transit, and restrict who can read it. Use ephemeral values and write-only arguments for secrets that should never be stored.
Protecting state
Local and remote state
Local state is convenient for learning, but it has serious limits for real work. The following comparison shows why teams use a remote backend.
Remote backends are configured with a backend block, and HCP Terraform with a cloud block, as described in the module on backends.
Local or remote state?
Reviewing a repository
During a review, a security engineer finds terraform.tfstate and terraform.tfstate.backup committed in a Git repository. The configuration creates an RDS database whose password variable is marked sensitive. What is the risk and the fix?
The decisive fact is that sensitive marking does not remove values from state.
Anyone who can read the repository, or any of its clones, can read the database password from the state files. The team should rotate the password, move state to a remote backend with encryption and access control, add *.tfstate and *.tfstate.* to .gitignore, and remove the files from the repository history. For the future, the password can be passed through a write-only argument so that it is never stored.
What do the lineage and serial values in a state file identify?
Show answer
Lineage identifies the state as a whole; serial increases with each write, so Terraform can tell newer versions from older ones.
Exam Tips
Remember for the exam
- State is JSON with lineage, serial, outputs and resource instances.
- All attributes, including secrets, are stored in plain text.
- Never commit state; use an encrypted, access-controlled remote backend.
- Never edit state by hand.