By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Bringing existing cloud resources under Terraform control—without downtime or destruction.
You inherit a production AWS environment with 50 EC2 instances, 10 RDS databases, and 3 S3 buckets—all manually created via the console or CLI. Your CTO demands "everything in Terraform by Friday." terraform import is your lifeline.
terraform import
Real-world scenario:You’re onboarding a new client. Their AWS account has 200+ resources (VPCs, subnets, Lambda functions) with no Terraform state. You must: 1. Inventory existing resources.2. Write Terraform configs to match them.3. Import them into state without recreating or deleting anything.
terraform.tfstate
aws_instance.web
import
i-1234567890abcdef0
terraform plan
terraform state
terraform state list
terraform state show
terraform state rm
force_destroy = true
refresh
aws sts get-caller-identity
terraform -v
i-0123456789abcdef0
Create main.tf with a minimal resource block matching the instance:
main.tf
provider "aws" { region = "us-east-1" # Must match the instance's region! } resource "aws_instance" "web" { # Leave attributes empty for now—we'll fill them after import. # Required: ami, instance_type, subnet_id, etc. # Example (replace with your instance's values): ami = "ami-0c55b159cbfafe1f0" # Amazon Linux 2 instance_type = "t2.micro" subnet_id = "subnet-1234567890abcdef0" tags = { Name = "web-server" } }
Why this matters:Terraform needs a "target" to map the imported resource to. The config must match the real-world resource’s attributes (e.g., ami, instance_type), or terraform plan will show drift.
ami
instance_type
terraform init
Expected output:
Initializing the backend...Initializing provider plugins...Terraform has been successfully initialized!
terraform import aws_instance.web i-0123456789abcdef0
aws_instance.web: Importing from ID "i-0123456789abcdef0"...aws_instance.web: Import prepared! Prepared aws_instance for import aws_instance.web: Refreshing state... [id=i-0123456789abcdef0] Import successful!
⚠️ Common failure modes:- Error: Resource not found → Check the instance ID and region.- Error: No resource block found → Ensure aws_instance.web exists in main.tf.
Resource not found
No resource block found
terraform state show aws_instance.web
Expected output (truncated):
# aws_instance.web: resource "aws_instance" "web" { ami = "ami-0c55b159cbfafe1f0" arn = "arn:aws:ec2:us-east-1:123456789012:instance/i-0123456789abcdef0" instance_type = "t2.micro" private_ip = "10.0.1.100" subnet_id = "subnet-1234567890abcdef0" tags = { "Name" = "web-server" } vpc_security_group_ids = [ "sg-1234567890abcdef0", ] }
Copy the output from terraform state show into main.tf:
resource "aws_instance" "web" { ami = "ami-0c55b159cbfafe1f0" instance_type = "t2.micro" subnet_id = "subnet-1234567890abcdef0" vpc_security_group_ids = ["sg-1234567890abcdef0"] tags = { Name = "web-server" } }
No changes. Your infrastructure matches the configuration.
If you see changes:- Someone manually modified the instance (e.g., changed the instance_type).- Your config is missing attributes (e.g., user_data, ebs_block_device).
user_data
ebs_block_device
Repeat Steps 1–6 for: - Security groups (aws_security_group).- EBS volumes (aws_ebs_volume).- Elastic IPs (aws_eip).
aws_security_group
aws_ebs_volume
aws_eip
Pro tip: Use terraform state list to see all imported resources.
ec2:Describe*
aws-vault
bash export AWS_ACCESS_KEY_ID="..." export AWS_SECRET_ACCESS_KEY="..."
CostCenter
Environment
t3.xlarge
t3.medium
modules/ec2-instance
depends_on
terraform apply
Terraform = true
plan
resource
aws_instance.web_server
region
subnet_id
Answer: The Terraform config doesn’t match the imported resource’s attributes.
Command syntax: "Which command imports an existing S3 bucket my-bucket into aws_s3_bucket.data?"
my-bucket
aws_s3_bucket.data
Answer: terraform import aws_s3_bucket.data my-bucket
terraform import aws_s3_bucket.data my-bucket
Drift detection: "How do you check if an imported resource has been manually modified?"
apply
Question: "You need to import 50 EC2 instances into Terraform. What’s the most efficient approach?" Answer:1. Use aws ec2 describe-instances to list all instances.2. Generate Terraform configs dynamically (e.g., with jq or Python).3. Import in batches using a script (e.g., for i in $(aws ec2 describe-instances ...); do terraform import ...; done).
aws ec2 describe-instances
jq
for i in $(aws ec2 describe-instances ...); do terraform import ...; done
Challenge: Import an existing AWS Security Group into Terraform.1. Create a security group manually in the AWS Console (e.g., sg-1234567890abcdef0).2. Write a Terraform config for aws_security_group.web.3. Import the security group into state.4. Verify no drift exists.
sg-1234567890abcdef0
aws_security_group.web
Solution:
# main.tf resource "aws_security_group" "web" { name = "web-sg" description = "Allow HTTP/HTTPS" vpc_id = "vpc-1234567890abcdef0" ingress { from_port = 80 to_port = 80 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } }
terraform import aws_security_group.web sg-1234567890abcdef0 terraform plan # Should show no changes
Why it works: The config matches the real security group’s attributes (name, VPC ID, rules).
terraform import <resource> <id>
terraform state show <resource>
terraform state rm <resource>
terraform refresh
aws s3api list-buckets
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.