Terraform Fundamentals · Topic 2 of 2

How Terraform Uses and Manages State

Terraform Associate (004): what the state file stores, why Terraform needs state, refresh and drift, backups, and keeping secrets in state safe.

  • 5 min
In a nutshell
  • State maps resource addresses to real objects and stores their attributes, dependencies and metadata.
  • The local backend is the default and writes terraform.tfstate; remote backends and HCP Terraform share state across a team.
  • terraform plan and terraform apply refresh state by default; -refresh-only updates state without changing infrastructure.

What it is

State is Terraform's record of the infrastructure it manages. It maps each resource address in your configuration, such as aws_instance.web, to the real object it created, such as instance i-0abc123, and stores that object's last known attributes.

By default state is a JSON file named terraform.tfstate in the working directory, written by the local backend. Teams usually move it to a remote backend or HCP Terraform so everyone works from the same copy.

Configurationwhat you wantPlancompareStatewhat Terraform managesReal infrastructurewhat exists

How it works

Why Terraform needs state

PurposeWhat state provides
Mapping to the real worldWhich real object belongs to each resource address
MetadataDependencies and other details, so Terraform can destroy in the right order after a resource leaves the configuration
PerformanceCached attribute values, which large configurations can rely on instead of querying every object
SyncingOne shared record for a team when stored remotely

Without state, Terraform could not tell whether a resource in your configuration already exists or needs to be created. It also could not know what to delete when you remove a block from the configuration.

State during plan and apply

terraform planRefreshread real objectsCompareconfig, state, realityProposed changesterraform applyState updated

By default terraform plan and terraform apply refresh state first: Terraform asks the providers for the current attributes of every managed object. Differences between the refreshed values and the configuration become proposed changes.

After terraform apply makes changes, Terraform writes the new attributes back to state. Commands such as terraform validate and terraform output do not refresh state.

Drift and refresh only mode

Drift is when someone changes a managed object outside Terraform, for example in the cloud console. The next plan detects it during refresh and proposes changes to bring the object back to the configuration.

To accept the outside changes into state without changing any infrastructure, use refresh only mode:

terraform plan -refresh-only
terraform apply -refresh-only

The standalone terraform refresh command is deprecated because it updated state without showing you what would change first.

Where state lives

LocationNotes
Local backend (default)terraform.tfstate in the working directory; the previous version is kept as terraform.tfstate.backup
Remote backend, such as S3, azurerm or gcsShared state, usually with locking and encryption
HCP TerraformStores state per workspace with versions, locking and access control
terraform {
  backend "s3" {
    bucket       = "example-tf-state"
    key          = "network/terraform.tfstate"
    region       = "us-east-1"
    use_lockfile = true
  }
}

State locking stops two runs from writing the same state at once. Backends that support it lock automatically for any operation that could write state.

Secrets in state

State stores resource attributes in plain text, including passwords, keys and values marked sensitive = true. Marking a value sensitive only hides it in CLI output.

Protect state like a secret: use a remote backend with encryption at rest and restricted access, and never commit terraform.tfstate to version control. Ephemeral values, such as ephemeral variables and ephemeral resources, are not stored in state or plan files.

Working with state safely

Do not edit the state file by hand. Use the CLI instead: terraform state list and terraform state show inspect it, terraform state mv and terraform state rm change it, and terraform show prints it in readable form.

How the exam asks about this

  • "True or false: a database password passed in as a variable ends up in plain text in the state file." True, if it is stored as a resource attribute; sensitive only hides it in output.
  • "An engineer changed a tag in the console and wants state to reflect it without touching infrastructure." Run terraform apply -refresh-only.
  • "Which file does the local backend write by default?" terraform.tfstate in the working directory, with terraform.tfstate.backup holding the previous version.

What to remember for the exam

  • State maps resource addresses to real objects and stores their attributes, dependencies and metadata.
  • The local backend is the default and writes terraform.tfstate; remote backends and HCP Terraform share state across a team.
  • terraform plan and terraform apply refresh state by default; -refresh-only updates state without changing infrastructure.
  • State can hold secrets in plain text; protect it and keep it out of version control.
  • State locking prevents concurrent writes to the same state.
  • Inspect and change state with terraform state subcommands, never by editing the JSON.
Watch out

sensitive = true does not encrypt anything. It redacts the value in plan and apply output, but the value is still stored in plain text in state.

Common mistakes

Thinking Terraform reads the cloud instead of state. Terraform refreshes real objects, but it only knows which objects it manages because state lists them. A resource missing from state is treated as new.

Committing state to Git. State can contain secrets and is easily overwritten by two people at once. Use a remote backend with locking instead.

Using terraform refresh as the answer. It still exists but is deprecated. The current answer is terraform plan -refresh-only to review, or terraform apply -refresh-only to accept.

Check yourself

  • Why does Terraform need a state file at all?

    State maps each resource in the configuration to the real object it manages and records metadata such as dependencies, so Terraform knows what exists, what to change and what to destroy.

  • What does terraform apply -refresh-only do?

    It reads the current attributes of managed objects and, after you approve, writes them to state without creating, changing or destroying any infrastructure.

  • Does marking an output or variable sensitive = true keep it out of the state file?

    No. It only hides the value in CLI output; the value is still stored in plain text in state, so state itself must be protected.

Topic 2 of 2 in Terraform Fundamentals

Test yourself on Terraform Fundamentals

The full HashiCorp Terraform Associate 004 question bank has 600 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by HashiCorp. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.