cert-bank

Guides

이 글을 한국어로 읽기

Terraform State Backends and Workspaces: Local vs Remote, CLI vs HCP Terraform, and Which One a Question Wants

Terraform uses the word "workspace" for two different things, and it stores state in two very different kinds of place. Exam questions use that overlap on purpose: an option that would be right for an HCP Terraform workspace is wrong for a CLI workspace, and a fix that works for a remote backend does nothing for local state. The word workspace appears in the question or options of 103 of the 518 Terraform questions in this question bank, across both exams. This comparison sorts the four ideas apart and lists the clues that tell you which one a question is about.

Concept comparison · TF-TA004 and TF-IEP002

Terraform State Backends and Workspaces: Local vs Remote, CLI vs HCP Terraform, and Which One a Question Wants

The short answer

A backend decides where state lives. Local means a file in the working directory; remote means a shared store such as an S3 bucket or HCP Terraform, usually with locking, encryption and access control. A CLI workspace is just another state file under the same configuration, backend and credentials. An HCP Terraform workspace is a full replacement for a working directory, with its own state versions, variables, credentials, run history and permissions. When a question is about isolation, access control or separate credentials, a CLI workspace is almost never the answer.

Local versus remote state

Local backendRemote backend (for example s3) or HCP Terraform
Where state isterraform.tfstate in the working directory, plus a .backup copyA shared object or workspace that everyone reads
LockingA file lock on that one machineShared locking, where the backend supports it (s3 needs use_lockfile = true; HCP Terraform locks per workspace)
Secrets in statePlain JSON on a laptopEncryption and access control are available; state must still be treated as secret
RecoveryThe single backup fileBucket versioning, or state versions in HCP Terraform
Good forOne person learningAny team

Two rules that show up as wrong answers: Git is not a state store (no locking, no control over who reads the secrets inside), and a backend block cannot refer to named values such as input variables or locals. Per-environment values go in through partial configuration: terraform init -backend-config=prod.s3.tfbackend. To connect to HCP Terraform you normally write a cloud block instead of a backend block; the two are mutually exclusive, so a configuration has one or the other, never both.

Locking is not backup

A lock stops two runs from writing the same state at once: the second run cannot acquire the lock and fails. Three tools adjust this. -lock-timeout=10m makes a run wait and retry (the default is 0s, so it fails at once). -lock=false removes the protection and is rarely right. terraform force-unlock LOCK_ID clears a stale lock left by a killed run, and is safe only when you are sure nobody holds it.

For the s3 backend, the current answer is a lock file next to the state object (use_lockfile = true); locking with a DynamoDB table is deprecated. Terraform needs s3:DeleteObject on the lock file to release the lock, so "lock still held after a successful run" points to permissions. Keep the three protections apart: locking serializes writers, encrypt protects data at rest, and only bucket versioning lets you restore an older state after someone overwrote it.

Going deeper: -migrate-state versus -reconfigure, and the empty-state incident

Terraform remembers the initialized backend in the .terraform directory. When the backend block changes, terraform init stops until you say what should happen to the state. -migrate-state copies the existing state to the new location. -reconfigure ignores the old configuration and copies nothing, so the new location starts empty and the next plan proposes to create every resource that already exists. That is the classic production incident: someone picks -reconfigure to silence the error. Moving from an s3 backend to HCP Terraform follows the same path: swap in the cloud block, run init, and accept the offer to copy the state. Back up the state file first in every case.

CLI workspaces versus HCP Terraform workspaces

CLI workspaceHCP Terraform workspace
What it isA separate state instance in one working directory; the documentation calls it technically equivalent to renaming the state fileA managed unit with its own state versions, variables, credentials, runs and team permissions
Configuration and backendShared by all workspacesPer workspace
Credentials and accessThe same for every workspace, so not an access boundarySet per workspace (Read, Plan, Write, Admin) or per project (Read, Write, Maintain, Admin)
Commandsterraform workspace new/select/list/show; terraform.workspace in codeUI, API, or a cloud block naming one workspace or selecting several by tags
Documented fitA short-lived parallel copy to test a change, such as one per feature branchEach environment or component of a team's infrastructure

The two meet in one place. With cloud { workspaces { tags = [...] } }, the CLI's terraform workspace list and select operate on the HCP Terraform workspaces that carry those tags. name and tags cannot be combined, and each run still targets exactly one workspace.

Beginner tip: in any local test folder with the default local backend, run terraform workspace new test, apply something small, and then list the folder. You will find the new workspace's state in its own file under terraform.tfstate.d/test/, next to the default terraform.tfstate, with the same code and the same credentials. Seeing that a CLI workspace is only a second file makes the "is this an isolation boundary?" questions easy.

Which one does the question want?

  • "Dev credentials can select prod and apply" or "auditors require separate access per environment" → not CLI workspaces. Use separate root modules per environment, each with its own backend, or separate HCP Terraform workspaces.
  • "A throwaway copy to test a change without a new directory or backend" → a CLI workspace.
  • "Variables, credentials and run history per environment, with team permissions" → HCP Terraform workspaces, grouped in projects.
  • "Two engineers overwrote each other's changes" → a remote backend with locking.
  • "Restore state after a bad state push" → bucket versioning or HCP Terraform state versions, not locking.
  • "A fresh clone plans to create everything and state list is empty" → check terraform workspace show; a new clone starts in default. Do not apply that plan.
Going deeper: how a prod consumer quietly reads dev data

With a backend that supports CLI workspaces, each workspace keeps its state under a separate key. The terraform_remote_state data source reads only the producer's root-module outputs, and its optional workspace argument picks which of the producer's workspaces to read. It does not follow the consumer's own workspace: a consumer running in prod reads the producer's prod outputs only if it sets workspace = terraform.workspace (or another explicit name) or points at the right key. Nothing errors. A related trap: terraform workspace select dev followed by -var-file=prod.tfvars plans production sizes against dev state, because nothing ties a variable file to a workspace. Both are reasons why the documentation recommends separate configurations for environments that need strong separation.

Backend, CLI workspace or HCP Terraform workspace?
Must environments have separate access, so dev credentials cannot apply to prod?
YES →
Separate root modules, or separate HCP Terraform workspaces
↓ no
Variables, credentials and run history per environment, with team permissions?
YES →
HCP Terraform workspaces, grouped in projects
↓ no
Did two engineers overwrite each other's changes?
YES →
A remote backend with locking
↓ no
Must state be restored after a bad state push?
YES →
Bucket versioning or HCP Terraform state versions
↓ no
OTHERWISE →
A throwaway copy to test a change in the same directory: a CLI workspace
Read the question's clue against each row from the top: the first yes is the answer the exam wants.

Practise

Practise in the question bank: every question explains each option, right and wrong. Start with the domain you just studied, then take a timed mock exam.

Based on HashiCorp's Terraform documentation for backends, state locking and workspaces, and on the exam content for TF-TA004 and TF-IEP002. Backend options change between Terraform versions; check the documentation for the version you are tested on.