TF-Associate-004 domain - 11% of the exam

Terraform state management

Terraform state management is 11% of the TF-Associate-004 exam. These are the objectives it covers, each with practice questions, with every answer explained.

The domain in numbers

31
Practice questions
4
Objectives
8
Easy
16
Medium
7
Hard

Where people go wrong

  • Assuming the local backend protects what it stores

    The wrong answers credit the local backend with protection it does not give: an encrypted binary blob, hashed output values, attribute values left out of state, or encryption per user. The local backend writes state as plain-text JSON in terraform.tfstate with no encryption, and attribute values such as a generated password are recorded verbatim because state needs them to detect drift. Anyone with access to the file can read them, so the file itself must be treated as a secret.

    See it in a sample question
  • Using state rm or -replace to handle a rename

    The wrong answers deal with a renamed resource block by removing, replacing or hand-editing state. The state rm command only forgets the object, so the next apply tries to create a second one and the original is orphaned. The -replace flag forces a destroy and recreate, which deletes the real bucket or instance. Manual editing of state is unsupported. The state mv command rebinds the existing object to the new address, so the next plan shows no changes.

    See it in a sample question
  • Misplacing what plan and refresh do

    The wrong answers misplace the work of plan and refresh. One has plan write the refreshed values back to the state file, one says a refresh-only run must come first, and one picks -refresh=false, which skips the refresh and so cannot show drift. By default plan refreshes in memory, reports the change needed to undo an out-of-band edit, and applies nothing. The apply -refresh-only mode is what updates state to match real infrastructure without proposing resource changes.

    See it in a sample question
  • Expecting apply or plan to activate a new backend

    The wrong answers expect a new backend block to take effect through apply, plan or refresh, or through uploading the state file by hand. Backend configuration is processed only by terraform init. When the backend changes, init detects it and offers to migrate the existing local state to the new backend, prompting before it copies anything. Apply does not initialise a backend, plan never migrates state, and refresh does not read backend configuration.

    See it in a sample question
  • Treating state locking as universal or absent

    The wrong answers treat locking as either present everywhere or missing from the local backend. Locking is a backend feature, so support varies and some backends offer none. The local backend takes a file lock on the state, but that protects one machine only and does not reliably coordinate separate machines writing to a shared drive. Remote backends such as S3 with a lock table, and HCP Terraform, coordinate locks across everyone sharing the state.

    See it in a sample question

Objectives in this domain

Sample questions from this domain

Free sampleTerraform state managementeasy

A configuration using the default local backend provisions a database whose randomly generated password is exposed as an output. A reviewer asks whether the password is protected at rest in the state file. What should the practitioner tell them?

resource "random_password" "db" {
  length = 20
}
output "db_password" {
  value     = random_password.db.result
  sensitive = true
}
  • AMarking the output 'sensitive = true' encrypts the value inside terraform.tfstate, so it is protected at rest.
  • BThe password never reaches state because 'random_password' keeps generated secrets only in provider memory.
  • CThe local backend hashes all output values before writing them, so only an irreversible digest is stored.
  • DThe value is stored as plaintext in terraform.tfstate, so the file must be treated as a secret and access to it restricted. Correct
State written by the local backend is plaintext, so sensitive values are stored unencrypted and the file must be protected. The local backend serialises state to plaintext JSON with no encryption, and the 'sensitive' flag only suppresses console display. Any secret, including a generated password, is readable in terraform.tfstate, so the file must be secured.

Why A is wrong: The 'sensitive' flag only hides the value from CLI output; it does not encrypt anything written to state, so this overstates its effect.

Why B is wrong: Resource attributes, including generated passwords, are always persisted to state so Terraform can detect drift, so this is incorrect.

Why C is wrong: The local backend performs no hashing of values; it records real attribute values verbatim, so no irreversible digest is produced.

Why D is correct: The local backend writes state as plaintext JSON, so the generated password appears in clear text and the file itself must be guarded.

Free sampleTerraform state managementhard

You renamed a resource block from aws_s3_bucket.logs to aws_s3_bucket.audit_logs. The bucket already exists and holds production data. You want a plan that shows no changes after the rename. Which command reconciles the state so Terraform tracks the existing bucket under the new address?

# before: resource "aws_s3_bucket" "logs" { ... }
# after:  resource "aws_s3_bucket" "audit_logs" { ... }
  • Aterraform state rm aws_s3_bucket.logs, then re-run apply so Terraform imports the bucket under the new name automatically.
  • Bterraform state mv aws_s3_bucket.logs aws_s3_bucket.audit_logs to move the existing state entry to the new address. Correct
  • Cterraform apply -replace=aws_s3_bucket.audit_logs to align the new address with the running bucket.
  • Dterraform taint aws_s3_bucket.logs so Terraform re-registers the bucket under audit_logs on the next apply.
Use terraform state mv to rebind an existing resource to a renamed address without recreating it. Renaming a resource block changes its state address, so Terraform sees the old address as gone and the new one as absent; state mv relinks the existing tracked object to the new address so no create or destroy is planned.

Why A is wrong: state rm only forgets the resource; a following apply would try to create a brand-new bucket and clash with the existing one rather than adopting it.

Why B is correct: state mv rebinds the existing state object to the renamed address without touching the real bucket, so the next plan shows no changes.

Why C is wrong: The -replace flag forces destroy-and-recreate of the resource, which would delete the production bucket and its data rather than simply renaming the state entry.

Why D is wrong: taint is deprecated and marks a resource for recreation, not renaming; it would schedule destruction of the bucket and never reconciles a changed address.

Free sampleTerraform state managementhard

Before running a plan you want to update Terraform's state to match the real infrastructure and see which resources have drifted, but you must not propose or apply any configuration changes. Which command achieves exactly this in Terraform 1.x?

$ terraform ____
  • Aterraform refresh, because it is the actively recommended command for updating state without side effects.
  • Bterraform plan -refresh=false to compare state to reality while skipping any changes.
  • Cterraform import for each resource to pull current values back into the state file.
  • Dterraform apply -refresh-only to reconcile state with real infrastructure and report drift without changing resources. Correct
Use terraform apply -refresh-only to reconcile state with reality and report drift without changing resources. The refresh-only mode reads live attributes, writes the reconciled values into the state file, and reports detected drift, but it deliberately proposes no create, update, or destroy actions on the managed resources.

Why A is wrong: The standalone terraform refresh command still exists but is deprecated; HashiCorp now steers practitioners to the refresh-only plan and apply modes instead.

Why B is wrong: Setting -refresh=false does the opposite of what is wanted: it skips the refresh entirely, so drift is not detected and state is not updated.

Why C is wrong: import brings unmanaged resources under management one address at a time; it is not a way to refresh already-managed resources or to report drift across the state.

Why D is correct: The refresh-only apply updates the state file to match live infrastructure and surfaces drift, while proposing no changes to the resources themselves.

Free sampleTerraform state managementmedium

A team currently keeps Terraform state in a local file and wants to move to an S3 backend so the whole team shares one state. After adding the backend configuration shown, what must the practitioner do for Terraform to start using it, and what happens to the existing local state?

terraform {
  backend "s3" {
    bucket = "acme-tfstate"
    key    = "prod/terraform.tfstate"
    region = "eu-west-1"
  }
}
  • ARun terraform init; Terraform detects the new backend and offers to migrate the existing local state to S3. Correct
  • BRun terraform apply; the backend block takes effect on the next apply and the local state is uploaded automatically.
  • CRun terraform plan; planning re-reads the configuration and silently switches the backend without touching the old state.
  • DManually copy terraform.tfstate into the S3 bucket first, because init refuses to move an existing local state.
Changing the backend block takes effect only when terraform init runs, which offers to migrate existing state. Backend initialisation is a job of terraform init, not plan or apply. When init detects a new or changed backend it prompts to migrate the current state into it, so the local state is preserved rather than discarded.

Why A is correct: Correct: a backend change is only picked up by terraform init, which prompts to copy the current local state into the new backend so nothing is lost.

Why B is wrong: Tempting because apply writes state, but a backend change is not initialised by apply. Terraform will error asking you to run init before any apply.

Why C is wrong: Plausible because plan reads the whole configuration, but plan never changes backends or infrastructure, and it does not migrate state.

Why D is wrong: This sounds safe but is wrong: init handles the migration for you and prompts before copying, so a manual upload is unnecessary and risks a mismatched lineage.

Free sampleTerraform state managementmedium

An engineer notices that state locking behaves differently depending on how state is stored. Which statement most accurately describes when Terraform can lock state?

  • ALocking depends on the backend, and it is available only where the backend supports it, so some backends offer no locking at all. Correct
  • BLocking is always applied identically regardless of backend, because Terraform itself owns the lock rather than the backend.
  • CLocking is available only when using the local backend, since remote backends cannot coordinate a shared lock.
  • DLocking works for every remote backend but is unavailable for the local backend under any circumstances.
State locking is a backend-provided capability, so its availability depends on the configured backend. Terraform delegates locking to the backend implementation, so a backend that supports locking will guard operations while one that does not offers no protection; this is why locking support is documented per backend.

Why A is correct: Correct: locking is a backend feature, so whether and how state is locked is determined by the configured backend.

Why B is wrong: Tempting because locking feels like core behaviour, but the locking mechanism is provided by the backend, so support varies.

Why C is wrong: Tempting but reversed: remote backends such as S3 with a lock table and HCP Terraform are exactly where robust locking is provided.

Why D is wrong: Tempting because remote backends are associated with locking, but the local backend does lock the on-disk file during operations.

Other domains in this exam

See also the TF-Associate-004 cert hub, the study guide, and the cheat sheet.

Examworthy is not affiliated with or endorsed by HashiCorp. Original, blueprint-aligned practice material only.