Skip to content
DevOpsintermediate

What is Terraform state?

Updated Nov 30, 20263 min readBased on official documentation
What is Terraform state?

In short

Terraform state is a file that records which real resources Terraform manages and how they map to your configuration. Terraform reads it before each run to decide what to create, change or delete. By default it is a local JSON file named terraform.tfstate.

Terraform is a tool that creates and manages infrastructure, such as servers, networks and databases, from text files you write. To do its job, Terraform needs to remember what it has already built. That memory is called state.

How it works

When Terraform creates a resource in response to your configuration, it records the identity of that real object against the resource instance it came from. Later runs use this record to find the same object again. If you change the configuration, Terraform compares your new code with the state and with the real infrastructure, then plans the smallest set of changes needed.

Before most operations, Terraform refreshes the state so it matches what actually exists in your cloud account or data center. This keeps the plan accurate. The state also stores metadata and helps Terraform stay fast when you manage a large number of resources, because it does not have to rediscover everything from scratch.

By default, Terraform keeps this data in a local file called terraform.tfstate. Before each change, it also keeps a backup of the previous version in terraform.tfstate.backup. The file is plain JSON. You should not edit it by hand. Instead, use the terraform state command, which offers basic operations such as listing and moving entries from the command line.

Why local state is not enough for teams

A local file needs no setup, which makes it fine for learning and small experiments. The drawbacks show up quickly in real work. Only the person who holds the file can run Terraform safely against the infrastructure, and losing the file means Terraform no longer knows what it created. Two people applying changes at the same time can also overwrite each other.

The official guidance is to store state in HCP Terraform or in a remote backend. A remote backend keeps the state in shared storage, so the whole team uses the same copy. Good backends also support state locking, which blocks a second run from changing the state while the first one is still working.

You can learn more about running infrastructure in a controlled way in our guide to GitOps, which covers keeping changes reviewed and tracked.

Keep state out of version control

It can be tempting to commit the state file to Git next to your code. Avoid this. Git does not provide state locking, and it is hard to control who can read the file. The state can contain sensitive values, so a leak can expose secrets. Use a storage option that supports locking and access control instead.

Importing and forgetting resources

Terraform expects a one-to-one link between each resource instance in your configuration and one real object. Usually Terraform keeps this link true by itself, because it creates the object and records it. Problems start when you change the state by hand. The terraform import command adds an existing object to the state. The terraform state rm command removes a binding so Terraform forgets the object. After either action, you must make sure the one-to-one rule still holds, for example by deleting an object you asked Terraform to forget, or by importing it to a different resource.

Reading state from other tools

If your automation needs the state data, avoid parsing the state file directly. The state format can change between Terraform versions, so your scripts would need constant updates. Instead, use the JSON output options of terraform output and terraform show. A common pattern is to run them right after a successful apply and save the result as a build artifact. Other software can then read that output without running Terraform itself.

Common mistakes

Beginners often delete the state file to fix a confusing error. This makes Terraform lose track of real resources, so it may try to create duplicates. Another mistake is sharing a local state file between machines. Move to a remote backend early, and treat state as important data that needs backups, access control and locking.

FAQ

Can I delete terraform.tfstate and start over?

You can, but Terraform will then lose track of the resources it created. On the next apply it may try to create them again, which can produce duplicates or conflicts. Use terraform import to bring existing objects back under management if needed.

Should I commit the state file to Git?

No. Git does not support state locking, and the state file can contain secrets. Store state in HCP Terraform or a remote backend that supports locking and access control.

Is state the same as my configuration files?

No. Configuration files describe what you want. State records which real objects Terraform has created for those configuration items.

Can I edit the state file by hand?

The official documentation says not to. Use the terraform state command for basic changes, since the format may change between versions.

Sources

This page is based on the official documentation below. Read it for full detail.

Terraform state — official documentation ↗

Terraform state in the news

blozhikDeveloper news from across the web, explained. Every story links to its original sources.