Courseiva
Use Terraform outside the core workflowmediumMultiple ChoiceObjective-mapped

TF-004 Use Terraform outside the core workflow Practice Question

A developer accidentally deleted a resource from the Terraform state file using 'terraform state rm'. The resource still exists in the cloud provider. How can the developer re-import the resource without affecting other resources?

⚠ Common exam trap

A common misconception is that `terraform refresh` can automatically discover and add untracked resources, when in fact it only updates state for resources already in the state file.

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

Use 'terraform import' with the resource address and ID.

`terraform import` is the designated command to bring an existing cloud resource back under Terraform management without affecting other resources. It requires the resource address (as defined in the configuration) and the provider-specific resource ID, allowing Terraform to write the current state into the state file without modifying the actual infrastructure.

Answer analysis

Option-by-option breakdown

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

  • Re-run 'terraform apply' to recreate the resource.

    Why it's wrong here

    "terraform apply" compares the desired state (configuration files) with the actual state (Terraform state file and cloud provider). If a resource exists in the configuration but is missing from the state file, "apply" would attempt to *create a new instance* of that resource. This would either fail due to naming conflicts if the resource already exists in the cloud, or create a duplicate, rather than re-associating with the accidentally deleted (from state) existing resource.

  • Use 'terraform state push' with a previous state backup.

    Why it's wrong here

    "terraform state push" is primarily used to manually upload a local Terraform state file to a remote backend, effectively overwriting the current remote state. While it could restore the *entire* state to a previous version, it is a blunt instrument for recovering a *single* accidentally deleted resource from the state, as it doesn't selectively add but rather replaces the whole state, potentially losing other recent changes.

  • Run 'terraform refresh' to discover and add the resource.

    Why it's wrong here

    "terraform refresh" updates the Terraform state file by querying the actual infrastructure to detect any drift (changes made outside of Terraform) for resources *already present* in the state. It cannot discover or add resources that exist in the cloud provider but are *not currently tracked* by Terraform's state, as its function is to validate and update existing entries, not to find new ones.

  • Use 'terraform import' with the resource address and ID.

    Why this is correct

    "terraform import" is specifically designed to bring existing infrastructure resources, which were created outside of Terraform or are no longer tracked by its state, under Terraform's management. It requires the resource's Terraform address (e.g., `aws_instance.web`) and its unique identifier from the cloud provider (e.g., `i-0abcdef1234567890`). This command adds the resource to the state file, allowing Terraform to manage it going forward.

About these practice questions

One of 428 original TF-004 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

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.