Lesson 2 of 2 Terraform State Fundamentals

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.

Why state committed to Git is a security finding. Marking the database password variable sensitive only redacts it in output; the state files still hold it, so anyone with access to the repository or a clone can read it. The fix is to rotate the password, move state to an encrypted, access-controlled remote backend, ignore and purge the files, and use a write-only argument in future.
Marking a value sensitive does not keep it out of state.

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.

Select all that apply

Protecting state

Exam skills 2.4.3 · 4.8.1

Identify the facts and practices that matter because state contains secrets.

Log in to try this activity Interactive activities are free with an account.

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.

Local state file
Shared with the teamNo
LockingSame machine only
Encryption and access controlDepends on the laptop
Version historyOne backup file
Choose when Personal experiments and learning
Remote backend (S3, HCP Terraform and others)
Shared with the teamYes
LockingAcross all users and pipelines
Encryption and access controlProvided by the service
Version historyVersioning in the service, such as S3 versioning or HCP state versions
Choose when Any shared or production infrastructure

Remote backends are configured with a backend block, and HCP Terraform with a cloud block, as described in the module on backends.

Sort into groups

Local or remote state?

Exam skills 2.4.3 · 6.1.1

Decide whether each property belongs to local state or to a remote backend.

Log in to try this activity Interactive activities are free with an account.
Worked example

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.

Check yourself

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.