Backends and state locking
Terraform 004 · Lesson 14 of 18 · 1:39:16
YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗
Learning with these videos? Subscribe for new courses and exam-style questions.
Subscribe on YouTube (opens in a new tab)In this lesson
Prepare for the HashiCorp Terraform Associate (004) exam. In this lesson: the local backend, the backend block and partial configuration, state locking and force-unlock, the S3 backend with its lock file, remote state, and CLI workspaces. Includes three original practice questions, with Bluefin Logistics, a fictional parcel delivery company, as the example.
Study notes
The text of this lesson, to read at your own pace. Information as of November 2026.
One shared state
Two Bluefin engineers ran terraform apply at the same time, from two laptops, each with its own copy of the state. Repairing the result took a whole day. This lesson covers where Terraform keeps its state, how a remote backend shares it, and how locking stops two runs from colliding.
The local backend
Without any backend configuration, Terraform uses the local backend by default. It writes the state to a file named terraform dot T F state in the root module directory, and keeps the previous version in a backup file next to it. It locks the state through the operating system, which protects only against two runs on the same machine. That suits one person learning, but not a team, because each engineer would work with a different copy of the state.
The backend block
A backend block, inside the terraform block, stores the state remotely, for example in an S3 bucket, an Azure storage account, or Google Cloud Storage. A configuration has at most one backend block, and the block cannot use variables or locals, because Terraform reads it before anything else. Values that change per environment go in a partial configuration: you leave them out of the block, and pass them to terraform init with the backend config option, as a file or as key and value pairs. Credentials belong in environment variables, because values written in the block end up in the dot terraform directory and in plans. And when you change the backend, terraform init copies the state with the migrate state option, while the reconfigure option ignores the old state.
State locking
State locking is automatic: when the backend supports it, Terraform locks the state for every operation that could write it, and releases the lock at the end. If another run holds the lock, Terraform stops with an error, unless the lock timeout option lets it wait for a while. With S3, setting use lock file to true stores a lock file next to the state in the bucket, which replaces the older lock table in DynamoDB, now deprecated. You can turn locking off with the lock equals false option, but that is rarely a good idea. And if a crash leaves your own lock behind, terraform force-unlock with the lock ID removes it, but never on a lock that another run still holds.
Remote state and workspaces
To use the outputs of another configuration, the terraform remote state data source reads the root module outputs from its state. CLI workspaces let one configuration and one backend keep several states, for example test and production, with commands like terraform workspace new and terraform workspace select. With the local backend, their states go in a directory named terraform dot T F state dot D. But workspaces share the same backend and credentials, so they are not a strong isolation: separate configurations, or separate accounts, are safer for production.
Recap
Let's recap. By default, the local backend writes terraform dot T F state in the root module directory, which suits one person, not a team. A single backend block stores the state remotely, takes no variables, and accepts a partial configuration at init, with the migrate state option to move existing state. Locking is automatic when the backend supports it, and force-unlock is only for your own stuck lock. And terraform remote state reads the outputs of another state, while CLI workspaces keep several states for one configuration.
Quick checks for this lesson
The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.
A new configuration has no backend block and no cloud block. An engineer runs terraform apply. Where is the state written?
- In HCP Terraform, in a default workspace
- In terraform.tfstate, in the root module directory
- In the .terraform directory, next to providers
- Nowhere until a backend is configured
Show the answer
In terraform.tfstate, in the root module directory
Without a backend, Terraform uses the local backend, which writes terraform dot T F state in the root module directory. “In HCP Terraform, in a default workspace” is wrong, because HCP Terraform needs a cloud block. “In the .terraform directory, next to providers” is wrong, because the dot terraform directory holds providers and modules, not the state of the local backend. And “Nowhere until a backend is configured” is wrong, because the local backend is the default, and needs no configuration.
The state is in S3 with use_lockfile = true. Two engineers run terraform apply at the same moment, with no other options. What happens to the second run?
- Both runs write, and the last one wins
- It waits until the first run is over
- It stops with an error: the state is locked
- It merges its changes into the first run
Show the answer
It stops with an error: the state is locked
The first run holds the lock, so the second one cannot acquire it, and stops with an error. “Both runs write, and the last one wins” is exactly what locking prevents. “It waits until the first run is over” is wrong, because Terraform tries the lock again only when the lock timeout option is given, and here it is not. And “It merges its changes into the first run” is wrong, because Terraform never merges two runs.
The API team needs the subnet IDs that the network configuration exposes as root outputs, from its own state in S3. What lets the API configuration read them?
- A terraform_remote_state data source
- A second backend block for that state
- A moved block from the network state
- A CLI workspace named network
Show the answer
A terraform_remote_state data source
The terraform remote state data source reads the root module outputs of another state. “A second backend block for that state” is wrong, because a configuration has at most one backend, and it stores its own state. “A moved block from the network state” is wrong, because moved blocks change addresses within one state. And “A CLI workspace named network” is wrong, because a workspace is another state of the same configuration, not a way to read a different one.