Local and remote backend selection
Where does Terraform keep its memory?
Imagine two engineers maintaining the same catalog. If each has an unrelated state file, neither knows what the other manages. A backend determines the location of persistent state and, for some integrations, how operations run. The ordinary local backend suits a private disposable exercise; a shared backend can give a team a common record. A shared file alone does not ensure safe coordination. A backend is distinct from a provider: the former stores state or coordinates operations, while the latter manages resource APIs. Selecting a backend does not create its storage automatically.
Start with an explicit local backend
In a fresh private lab directory, this configuration uses the built-in provider and writes state to catalog.tfstate in the default workspace. It uses the inherited provisional CLI pin, not verified here.
terraform { required_version = "= 1.14.8" backend "local" { path = "catalog.tfstate" }}
resource "terraform_data" "catalog" { input = "sample-catalog"}After initialization, a first plan should propose creation of the local record. Only after approval, apply and inspect terraform state list; expect terraform_data.catalog. The output is a prediction, not an observed result. No cloud account is necessary.
Supply non-secret settings during initialization
The root module can name one backend; backend arguments cannot depend on variables, locals or resource attributes. Terraform needs state access before evaluating the ordinary resource graph. An alternative to the explicit path above is the following partial configuration, paired with a file named settings.local.tfbackend.
terraform { backend "local" {}}path = "catalog.tfstate"terraform init -backend-config=settings.local.tfbackendUse these as an alternative configuration, not as an additional backend block. The file contains backend arguments directly, not a nested terraform block. Do not supply passwords or tokens this way. Backend settings can persist in working directory metadata and saved plans; use the supported external credential mechanism for the chosen backend. The metadata under .terraform is not the same as the actual managed-resource state.
Choose a backend for the team
Compare local filesystem storage with S3 object storage, Azure Blob Storage (azurerm backend), and Google Cloud Storage (gcs backend). Local uses operating-system locking; S3 native lockfile support requires explicit enablement, while Azure and GCS document locking support. For each remote option, verify the existing storage, identity, network access, encryption settings, version recovery, access audit and lock permissions. Do not assume a default configuration satisfies the team’s controls. For a fictional AWS-based team, the following is an illustrative fragment only. Never initialize it: the bucket name is not an authorized resource.
terraform { backend "s3" { bucket = "example-terraform-state-not-a-lab" key = "catalog/dev.tfstate" region = "eu-west-1" use_lockfile = true }}The backend key selects the state object; it is not a resource address. For a shared state store, restrict both state and lock access. S3 bucket versioning is a useful recovery control but not a substitute for testing restoration. Older DynamoDB-based locking is deprecated in the current S3 documentation; check the pinned version and coordinate any migration.
Distinguish remote storage from hosted runs
An object-store backend can store state remotely while Terraform runs on the operator’s machine. HCP Terraform can also coordinate hosted runs depending on workspace configuration. The following fictional configuration is for reading only; initialization can contact the service or create a workspace.
terraform { cloud { organization = "fictional-learning-org" workspaces { name = "catalog-dev" } }}The cloud integration is an alternative to backend configuration, not an additional backend block. The older remote backend is a separate integration path. Compare execution location, identity, access to private networks and approval rules when choosing the approach.
Rehearse a backend change safely
Before a migration, stop writers, verify the source and destination, protect a recoverable state backup, and obtain approval. In the local catalog lab only, after the initial apply, change the path to migrated.tfstate and run terraform init -migrate-state. Review any prompt before accepting. Expect the same resource address and an unchanged normal plan if inputs and state were preserved. -reconfigure instead forgets the old backend settings without copying existing state; it is not a migration shortcut. Never point a fresh state at already-managed objects and apply blindly. Verify state identity and ownership before resuming changes.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Local and remote backend selection — knowledge check
Review backend selection and lock recovery.
What does a backend primarily define?
Which value can an ordinary backend block use?
Which is true of S3 native state locking in current documentation?
What does init -migrate-state attempt to do?
What is true of backend credentials passed in configuration?