Lesson 1 of 3 Backends and State Locking

The Local Backend and CLI Workspaces

A backend decides where Terraform stores state. This lesson covers the default local backend and the CLI workspaces that let one configuration keep several states.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe the local backend and its limitations.
  • Explain how CLI workspaces store state and when not to use them.

The local backend

A backend defines where Terraform stores state and, for some backends, where operations run. When a configuration has no backend or cloud block, Terraform uses the local backend, which stores state in terraform.tfstate in the root module directory and writes the previous version to terraform.tfstate.backup before each update.

terraform {
  backend "local" {
    path = "state/terraform.tfstate"
  }
}

An explicit backend "local" block is needed only to change the path, with the path argument, or the directory for workspace states, with workspace_dir. The local backend locks state using operating system file locking, which prevents two Terraform processes on the same machine from writing at once but cannot coordinate different machines.

True or false

The local backend

Exam skills 6.1.1

Check how the local backend stores and protects state.

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

Limits of local state

Local state works for learning and personal experiments. For anything shared, it has significant drawbacks: teammates cannot access or lock it, it may hold secrets on an unencrypted disk, it can be lost with the laptop or the directory, and it tempts people to commit it to version control. Those drawbacks are why teams move to a remote backend or HCP Terraform.

CLI workspaces

Each working directory starts with one workspace, named default. CLI workspaces let the same configuration and backend hold several independent states. The commands are listed in the following table.

CommandEffect
terraform workspace new testCreate a workspace named test and switch to it
terraform workspace select prodSwitch to an existing workspace
terraform workspace listList workspaces; the current one is marked with *
terraform workspace showPrint the current workspace name
terraform workspace delete testDelete a workspace; the default workspace cannot be deleted

With the local backend, the default workspace uses terraform.tfstate and other workspaces use terraform.tfstate.d/<NAME>/terraform.tfstate. Remote backends store workspace states in their own way, such as separate keys in S3. The expression terraform.workspace returns the current name, so configuration can vary by workspace.

resource "aws_instance" "web" {
  instance_type = terraform.workspace == "prod" ? "m6i.large" : "t3.micro"
  tags          = { Name = "web-${terraform.workspace}" }
}

The following diagram shows where each CLI workspace's state file is stored.

Where the local backend stores state for each CLI workspace. terraform.tfstate belongs to the default workspace; a workspace named feature-x keeps its state in terraform.tfstate.d/feature-x/terraform.tfstate. workspace show names the current workspace and state list shows its resources.
The workspace name is illustrative.

Not an isolation boundary

CLI workspaces share one configuration, one backend and the same credentials. HashiCorp advises against using them to separate environments that need strong isolation, such as production and development; use separate root configurations or HCP Terraform workspaces, which have their own variables and permissions.

Match the pairs

Workspace commands

Exam skills 6.1.2

Match each CLI workspace task with its command.

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

Where is the state?

An engineer runs terraform workspace new feature-x, applies, and then cannot find the new resources in terraform.tfstate. The configuration uses the local backend.

The decisive fact is where non-default workspaces store state.

The state for feature-x is in terraform.tfstate.d/feature-x/terraform.tfstate; terraform.tfstate belongs to the default workspace. Running terraform workspace show confirms the current workspace, and terraform state list shows the resources in it. To remove the feature environment, the engineer destroys it while feature-x is selected, then selects another workspace and deletes feature-x.

Check yourself

Where does the local backend store the state for a CLI workspace named staging?

Show answer

In terraform.tfstate.d/staging/terraform.tfstate.

Exam Tips

Remember for the exam

  • Default backend: local, terraform.tfstate plus a .backup file.
  • Local locking protects one machine only.
  • CLI workspace states live in terraform.tfstate.d/<name>/ locally.
  • terraform.workspace returns the current name; the default workspace cannot be deleted.