Understanding Terraform State: Remote Backends Explained
Terraform state remote backends solve a plain problem: one person runs Terraform on a laptop, and the rest of the team has no reliable way to know what infrastructure actually exists. Terraform state is the record Terraform keeps of the resources it manages, their settings, and how they map to your configuration files. When that record lives only on one machine, collaboration breaks. A remote backend moves the record to shared storage that every team member and every automation job can reach.
If you are new to the tool, start with a first Terraform module before going remote.
What Terraform state actually stores
The state file is a JSON document. It lists each managed resource, the values Terraform last applied, and dependencies between resources. Terraform reads it before every plan so it can tell what already exists, what changed in your configuration, and what needs to be created, updated, or destroyed.
State is not a backup of your cloud account. It only covers resources Terraform manages. Resources created by hand in the cloud console, or by another tool, do not appear in it unless you import them. That gap is one reason teams run into drift, where real infrastructure no longer matches the configuration.
State files can also hold sensitive values, such as passwords or keys that a resource returned when it was created. Treat the file as sensitive data from day one, even in a small project. Anyone who can read it may learn details about your infrastructure that should stay private.
How terraform state remote backends solve team conflicts
With local state, two engineers can run Terraform against the same infrastructure from two laptops, each with a different copy of the record. Both plans look correct on their own machines. The second apply can overwrite or destroy work from the first, and neither person sees the conflict coming.
A remote backend gives the whole team one shared copy. Every plan and apply reads from the same place, so the second engineer sees the result of the first engineer’s apply. Automation jobs get the same benefit: a pipeline that runs Terraform reads the same state your laptop does, instead of a stale copy bundled into a build artifact.
Shared state also makes handover practical. A new team member can clone the configuration, point Terraform at the backend, and run a plan without asking a colleague to send a state file over chat. That single change removes a whole class of setup errors.
How a remote backend works
A backend has two jobs: store the state file somewhere durable, and control who can read or write it. Terraform supports several backends, and the right choice usually follows the cloud you already run on.
| Backend | Typical storage | Locking method | Good fit for |
|---|---|---|---|
| S3 | Amazon S3 bucket | DynamoDB table, or native S3 locking in newer setups | Teams already on AWS |
| GCS | Google Cloud Storage bucket | Built-in locking | Teams already on Google Cloud |
| azurerm | Azure Blob Storage | Blob lease | Teams already on Azure |
| Terraform Cloud / HCP Terraform | Hosted service | Built-in run queue | Teams that want hosted runs and a web interface |
| HTTP | A custom HTTP service | Depends on the service | Teams with an existing state service |
The terraform remote state pattern stays the same across these options: Terraform reads state from the backend at the start of a run, and writes the updated state back at the end. Your configuration declares the backend in a terraform block, and the backend settings themselves are not stored in state. They live in your configuration or in a separate backend configuration file.
If you are still deciding between tools, a Terraform vs Pulumi comparison helps clarify the trade-offs, and Terraform alternatives such as OpenTofu share the same remote-state idea with different backends and licensing.
Terraform state locking and why it matters
Locking stops two applies from running at the same time. When Terraform starts an apply, it takes a lock on the state. A second run that starts while the first is still going waits or fails with a clear error, instead of writing over the first run halfway through.
Locking also protects plans in some setups, but the main value is on applies and destroys, where a partial write can leave state and real infrastructure out of sync. If a run crashes or a lock is left behind after a failed job, Terraform lets you release the lock by force, but only do that after you confirm no other run is active. Forcing a lock release while another apply runs can corrupt state.
Some backends lock automatically, and some need an extra resource, such as a DynamoDB table for an S3 backend. Check the backend documentation for your provider before you rely on locking in production, because a backend without locking gives you shared storage without the safety net.
Setting up a terraform S3 backend
An S3 backend is a common first remote backend because most AWS teams already have an account and a bucket is cheap to run. The steps below describe the shape of the setup; check the official Terraform and AWS documentation for the current syntax and options.
- Create a dedicated S3 bucket for state, in the same region as most of your infrastructure, and turn on versioning so you can recover an earlier state file if a bad apply overwrites it.
- Turn on server-side encryption for the bucket, and block public access at the bucket level.
- Create a DynamoDB table for locking if your setup still needs one, with a partition key named LockID.
- Add a backend block to your Terraform configuration naming the bucket, key, and region, then run terraform init and confirm it asks to migrate your existing local state.
- Run a plan and confirm it reads the remote state, then run an apply from a second machine or a pipeline job to confirm both see the same result.
Pass backend settings through a separate file or init flags if they hold account-specific details, so the main configuration stays portable.
Security: encryption, access, and secrets
Three controls cover most remote-state risk. First, encryption at rest and in transit: the backend should encrypt the state file, and Terraform should only reach it over HTTPS. Second, narrow access: only the engineers and automation roles that run Terraform need read and write access to the state bucket or workspace. Read-only roles for reviewers are enough for most people. Third, versioning and backups: a versioned bucket lets you restore a previous state file after a bad apply, which is often faster than rebuilding state from scratch.
Secrets deserve separate handling. State files can contain sensitive values, so do not store them in Git, do not attach them to tickets, and do not paste them into chat. Use a dedicated secrets store for credentials and pass references into Terraform, rather than hardcoding values in configuration. A comparison of secrets managers compared can help you pick one that fits your cloud and budget.
Common mistakes with remote state
- Committing a state file to Git. Once it is in version history, removing it from the latest commit does not remove it from history, and any secrets in it stay exposed until you rotate them.
- Editing a state file by hand to fix a problem. Manual edits can break the mapping between configuration and real resources. Use import, refresh, and state move commands instead, and back up the file first.
- Sharing one state file across unrelated environments. Production and staging in the same state means one bad apply can touch both. Use separate workspaces or separate backend keys per environment.
- Skipping locking in a team setup. Shared storage without locking only moves the conflict problem, it does not remove it.
- Forgetting backups. Versioning on the bucket is the cheapest recovery option you have, and it only helps if you turn it on before you need it.
Migrating from local to remote state
Migration is a normal step, not a rebuild. Back up your local state file first, then add the backend block and run terraform init. Terraform detects the local state and asks whether to copy it to the backend. Confirm, then run a plan. A plan with no changes tells you the migration kept the mapping intact. A plan that wants to recreate resources tells you something went wrong, and you should stop and compare the backed-up state before you apply anything.
After migration, delete the local state file from the working directory or move it to a safe archive, so nobody accidentally runs Terraform against the old copy. Tell the team the backend location and the lock procedure on the same day, so the first remote apply does not surprise anyone.
Set up a remote backend early, lock it down, turn on versioning, and keep environments separate. Teams that do that spend less time untangling state problems.