TF-004 Implement and maintain state Practice Question
A developer is working on a Terraform configuration that manages a single resource. They want to import an existing AWS EC2 instance into state. Which command should they use?
⚠ Common exam trap
HashiCorp often tests the distinction between `terraform import` (which only updates state) and `terraform apply` (which modifies infrastructure), so the trap here is that candidates mistakenly think `terraform apply` can also import resources because it can create new ones, but it cannot attach to an existing resource that is not already in state.
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
`terraform import` is the dedicated command for bringing an existing infrastructure resource (like an AWS EC2 instance) under Terraform management by attaching it to a resource block in the state file. It requires the resource address and the provider-specific ID (e.g., `aws_instance.my_instance i-1234567890abcdef0`) to map the real-world resource into the Terraform state without modifying the resource itself.
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 apply
Why it's wrong here
The `terraform apply` command executes the planned actions to create, update, or destroy infrastructure resources defined in the Terraform configuration. It provisions resources based on the `.tf` files and the current state, synchronizing the real world with the desired state. However, `apply` cannot discover or incorporate existing, unmanaged resources from a cloud provider into the state file; its function is to manage resources already defined in the configuration.
- ✗
terraform refresh
Why it's wrong here
The `terraform refresh` command updates the Terraform state file to reflect the current real-world attributes of *already managed* infrastructure resources. It compares the state with the actual provider data, detecting any manual changes or configuration drift for existing entries. However, `refresh` does not possess the capability to discover or import new, unmanaged resources into the state; it only synchronizes the attributes of resources it already tracks.
- ✓
terraform import
Why this is correct
The `terraform import` command is the designated tool for bringing existing infrastructure resources, which were provisioned outside of Terraform's management, into the Terraform state file. It establishes a link between a specified remote resource and a corresponding resource block defined in the Terraform configuration. This crucial command enables developers to adopt pre-existing infrastructure and manage it subsequently with Terraform, integrating it into the desired state.
- ✗
terraform state mv
Why it's wrong here
The `terraform state mv` command is used to move or rename resources within the Terraform state file, effectively changing their address or path. This operation is primarily for refactoring configurations, reorganizing modules, or correcting resource naming conventions for *already managed* resources. It operates solely on entries within the state and does not interact with the cloud provider to discover or import new, unmanaged infrastructure into Terraform's management.
Go deeper
Related to this question
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 →
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.