Courseiva
Read, generate and modify configurationmediumMultiple ChoiceObjective-mapped

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.

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.

Use `terraform import` to import the existing security group into state. `terraform import` is the proper way to bring existing resources under Terraform management. Option A is incorrect because running `terraform apply` would attempt to create a duplicate security group, resulting in an error or duplicate resource. Option B is incorrect because manually editing the Terraform state file is error-prone and not recommended. Option C is incorrect because `terraform refresh` updates the state with the current state of resources already in the state file, but does not import new resources; it would not detect the manually created security group.

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

    will create a duplicate resource and cause conflict

  • Manually edit the Terraform state file to add the security group.

    Why it's wrong here

    prone to errors and not a best practice

  • Run `terraform refresh` to update the state with the existing resource.

    Why it's wrong here

    refresh only updates attributes, does not add new resources

  • Use `terraform import` to import the existing security group into state.

    Why this is correct

    proper method to adopt existing resources

Visual reference

192.168.1.0 /24 256 addresses (254 usable) 192.168.1.0 /25 Subnet A 128 addr (126 usable) 192.168.1.128 /25 Subnet B 128 addr (126 usable) Borrowing 1 bit from host portion creates 2 subnets (/25)

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

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 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.