Courseiva
Implement and maintain statehardMultiple ChoiceObjective-mapped

TF-004 Implement and maintain state Practice Question

A user accidentally ran `terraform state rm` on a critical resource, removing it from state but not destroying the actual infrastructure. Later, they need to re-import the resource. Which sequence of commands correctly accomplishes this?

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

`terraform import` using the resource address and ID of the resource

After removal, the resource is unmanaged. `terraform import` re-associates it with state. Option D is the correct sequence.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • `terraform state push` with the original state file

    Why it's wrong here

    terraform state push is a deprecated command, primarily intended for advanced state manipulation by directly uploading a modified state file to a remote backend. It is not designed for re-importing or recovering individual resources that have been removed from state. Using it to restore a previous state file would overwrite the entire current state, potentially causing more issues if other changes have occurred since the original state file was valid, and it doesn't address the specific problem of a single resource missing from state while the rest is intact.

  • `terraform plan` and then `terraform apply`

    Why it's wrong here

    Running terraform plan after a resource has been removed from state will cause Terraform to perceive the existing remote resource as unmanaged infrastructure. Since the configuration still declares the resource but it's absent from the state file, Terraform will propose creating a new instance of that resource. If terraform apply were executed, it would attempt to provision a duplicate resource, or if the resource name/ID must be unique, it would likely fail or prompt for destruction of the existing resource before recreation, which is not the desired outcome of simply re-associating an existing resource.

  • `terraform refresh` and then `terraform plan`

    Why it's wrong here

    terraform refresh is designed to reconcile the attributes of resources already tracked in the Terraform state with their current real-world configuration. It updates the state file with the latest attribute values from the cloud provider for known resources. Since the resource was explicitly removed from the state file using terraform state rm, terraform refresh has no record of it to update and therefore cannot discover or re-add it to the state. Consequently, terraform plan would still show the resource as needing to be created.

  • `terraform import` using the resource address and ID of the resource

    Why this is correct

    terraform import is the correct and intended method for bringing existing infrastructure under Terraform management. When a resource is accidentally removed from the state file via terraform state rm, the remote resource still exists. By specifying the resource's address (e.g., aws_instance.web) from the configuration and its actual cloud provider ID (e.g., i-0abcdef1234567890), terraform import reads the resource's current configuration from the provider and adds it back into the Terraform state file, linking it to the specified configuration block without modifying the live resource.

About these practice questions

Courseiva writes every TF-004 question from scratch — 428 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

1 more way this is tested on TF-004

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. Match each Terraform state command to its action.

medium
  • A.terraform state list: List resources in state
  • B.terraform state show: Display details of a specific resource in state
  • C.terraform state mv: Move an item in the state to a new address or state file
  • D.terraform state rm: Remove an item from the state
  • E.terraform state list: Remove items from state
  • F.terraform state mv: Rename a resource

Why A: The correct matches are: terraform state list → List resources in state, terraform state show → Display details of a specific resource, terraform state mv → Move an item, terraform state rm → Remove an item. Common confusions include swapping list with rm or thinking mv is primarily for renaming.

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This TF-004 practice question is part of Courseiva's free HashiCorp certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the TF-004 exam.