How to Import Existing Resources into Terraform State
A team is using Terraform to manage a multi-tier application on AWS. The configuration includes resources for VPC, subnets, security groups, and EC2 instances. Recently, a developer manually created an additional security group in the AWS console for testing, and now the team wants to manage it via Terraform. They have updated the configuration to include this security group with the same name and rules. When they run `terraform plan`, it shows that the security group will be created, but the existing one is not detected. They want to bring the existing security group under Terraform management without recreating it. The team is using a remote backend with state locking. What should they do?
Quick Answer
The correct answer is to use `terraform import` to bring the existing security group into Terraform state. This is the proper approach because `terraform import` maps a real-world resource—such as the manually created security group in AWS—to a resource block in your configuration, allowing Terraform to track and manage it without recreating it. Simply running `terraform apply` would attempt to create a duplicate, while manually editing the state file is error-prone and unsupported. On the HashiCorp Terraform Associate TF-003 exam, this question tests your understanding of state management and the distinction between `import`, `refresh`, and `apply`. A common trap is confusing `terraform refresh` with import; remember that refresh only updates existing state attributes, it does not add new resources. For a quick memory tip: think of `import` as "adopting" an existing resource into Terraform’s control, not rebuilding it.
⚠ Common exam trap
It's easy for candidates to confuse `terraform refresh` (which only updates attributes of resources already in state) with `terraform import` (which adopts resources not yet in state) — candidates pick refresh because it sounds like it 'syncs' with reality.
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` to import the existing security group into state.
`terraform import` is the purpose-built command for adopting resources that already exist outside of Terraform into the state file without destroying and recreating them. Running `terraform import aws_security_group.example sg-xxxxxxxx` maps the real AWS security group to the resource address in configuration, after which `terraform plan` shows no changes if the config matches reality. This preserves the existing security group's ID and any references to it, avoiding downtime or broken dependencies.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Run `terraform apply` and then modify the state to remove the duplicate.
Why it's wrong here
Applying first creates a second, duplicate security group, then leaves two resources to reconcile; removing state entries afterwards does not delete the real duplicate. It would be correct if the resource genuinely did not exist. Importing the existing group avoids creating anything new.
- ✗
Manually edit the Terraform state file to add the security group.
Why it's wrong here
Directly editing the state file bypasses Terraform's state-locking and serialisation, risking corruption and lock conflicts with the remote backend. It would be correct only for repairing already-corrupted state. The supported mechanism for adopting an existing resource is `terraform import`, which writes the mapping safely.
- ✗
Run `terraform refresh` to update the state with the existing resource.
Why it's wrong here
`terraform refresh` only reconciles state with the real infrastructure for resources already tracked; it cannot adopt an untracked security group, so the plan still shows creation. It would be correct for detecting drift on managed resources. `terraform import` is the command that brings the existing group under management.
- ✓
Use `terraform import` to import the existing security group into state.
Why this is correct
Terraform import reads the existing security group's real identifier and writes it into state, so subsequent plans reconcile configuration with the live resource instead of creating a duplicate, satisfying the stem's requirement to adopt it without recreation.
Visual reference
Go deeper
Related to this question
About these practice questions
One of 434 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 →
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. A team has an existing S3 bucket created outside Terraform. They want to manage it with Terraform by importing its state. Which of the following is the correct sequence of commands to read the bucket's configuration and avoid drift?
hard- A.Run terraform refresh, then terraform state pull to generate configuration.
- B.Run terraform plan, note the resource address, then run terraform import.
- C.Run terraform import, then terraform state show to generate configuration.
- ✓ D.Write a minimal resource configuration, run terraform import, then terraform plan, then adjust configuration to match state.
Why D: Importing an existing S3 bucket into Terraform requires first writing a minimal resource configuration (e.g., `resource "aws_s3_bucket" "example" { bucket = "existing-bucket-name" }`), then running `terraform import` to link the real-world bucket to the state file. After import, running `terraform plan` reveals any configuration-to-state mismatches, allowing you to adjust the resource block (e.g., adding `acl`, `versioning`, or `tags`) to match the actual bucket configuration, thereby avoiding drift. This workflow ensures the Terraform configuration accurately reflects the existing infrastructure before making changes.
JA
Written and reviewed by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
Last reviewed September 2026 · checked against the official HashiCorp exam blueprint
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.