What is terraform import and when do you use it?
Understand terraform import, how it maps existing resources into state, import blocks with generate-config-out, and when to adopt infra without recreation.
Expected Interview Answer
terraform import brings an existing, manually-created or externally-managed real-world resource under Terraform's control by writing it into Terraform state and mapping it to a resource address in your configuration, so Terraform can manage it going forward instead of trying to recreate it.
You use it when infrastructure already exists — created via the console, a script, or another tool — and you want Terraform to own it without downtime. Historically you ran terraform import ADDRESS ID and hand-wrote matching configuration; modern Terraform supports declarative import blocks that can also generate configuration with -generate-config-out. Import only writes state; the CLI form never creates the config for you, so you must author HCL that matches the real resource or the next plan will show spurious changes.
- Adopts existing infrastructure without destroying and recreating it
- Avoids downtime when migrating manual resources to Terraform
- Reconciles drift where a resource exists but is missing from state
- Enables gradual migration of legacy environments to IaC
- Import blocks can auto-generate starter configuration
AI Mentor Explanation
terraform import is like adding a player who has been quietly practicing on your ground for years to the official team roster and scorebook. The player already exists and performs; you are not signing a new one, just recording who they are so the scorer can now track their runs and wickets. Until you write their details onto the team sheet to match reality, the official records and the actual player on the pitch will keep disagreeing at every match.
How terraform import maps a real resource to state
Real Resource
- aws_instance
- id: i-0abc123
Config Address
- aws_instance.web
- HCL block
Terraform State
- state entry
- id: i-0abc123
Step-by-Step Explanation
Step 1
Write a matching resource block
Author an empty aws_instance.web block (or use an import block) so Terraform has an address to import into.
Step 2
Find the real resource ID
Get the provider-specific identifier, such as the EC2 instance id i-0abc123, from the console or CLI.
Step 3
Run the import
Execute terraform import aws_instance.web i-0abc123, or add an import block and run terraform plan.
Step 4
Reconcile the configuration
Run terraform plan and adjust the HCL until no changes are proposed, so config matches reality.
Step 5
Commit and manage
Once plan is clean, commit the config and state so Terraform fully owns the resource going forward.
What Interviewer Expects
- Knows import only writes state, not configuration in the CLI form
- Understands the goal is adopting existing resources without recreating them
- Can describe both the CLI command and declarative import blocks
- Mentions -generate-config-out for auto-generating HCL
- Explains reconciling plan output until no changes remain
Common Mistakes
- Expecting terraform import to write the HCL configuration automatically
- Not authoring a matching resource block before importing
- Ignoring the post-import plan that reveals config drift
- Using the wrong provider-specific resource ID format
- Importing into the wrong resource address and corrupting state
Best Answer (HR Friendly)
“terraform import lets Terraform take over managing infrastructure that already exists but was created outside of it, like a server built by hand. It records that resource so Terraform tracks it, which avoids deleting and rebuilding it and helps teams migrate to infrastructure as code smoothly.”
Code Example
# 1. Write a matching (initially minimal) resource block
resource "aws_instance" "web" {
ami = "ami-0abcd1234"
instance_type = "t3.micro"
}
# 2. Import the existing instance into state
# terraform import aws_instance.web i-0abc123
# 3. Declarative alternative (Terraform 1.5+):
import {
to = aws_instance.web
id = "i-0abc123"
}
# terraform plan -generate-config-out=generated.tfFollow-up Questions
- Does terraform import generate the resource configuration for you?
- How do import blocks differ from the terraform import CLI command?
- What does -generate-config-out do?
- How do you import a resource into a module?
- What is the difference between import and terraform state mv?
MCQ Practice
1. What does the terraform import CLI command actually modify?
The CLI import writes only state; you must author matching configuration yourself so plans stay clean.
2. When is terraform import most appropriate?
Import adopts existing infrastructure into Terraform management without downtime or recreation.
3. Which Terraform feature can auto-generate configuration during import?
Declarative import blocks combined with plan -generate-config-out can scaffold HCL for the imported resource.
Flash Cards
What does terraform import do? — Maps an existing real resource into Terraform state at a resource address so Terraform can manage it.
Does CLI import create configuration? — No — it only writes state; you must author matching HCL or the next plan shows changes.
What is an import block? — A declarative Terraform 1.5+ construct with to and id that imports during plan/apply and can generate config.
When should you use import? — When infrastructure already exists outside Terraform and you want to adopt it without recreating it.