Table of Contents
Why Import Existing Infrastructure?
When you have resources created manually (via console or CLI) that you want Terraform to manage going forward, you need to import them into Terraform state.
Method 1: terraform import (Classic)
Step 1: Write the Resource Configuration
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t3.micro"
# Add all attributes that match the existing resource
}
Step 2: Run the Import Command
terraform import aws_instance.web i-1234567890abcdef0
Step 3: Verify with Plan
terraform plan
# Should show no changes if configuration matches reality
Step 4: Adjust Configuration
If terraform plan shows differences, update your .tf files to match the imported resource.
Method 2: import Block (Terraform 1.5+)
The modern approach — declarative imports in configuration:
import {
to = aws_instance.web
id = "i-1234567890abcdef0"
}
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t3.micro"
}
Then run:
terraform plan -generate-config-out=generated.tf # Auto-generate config
terraform apply # Execute the import
Common Import IDs by Resource
| Resource | Import ID Format |
|---|---|
| aws_instance | Instance ID: i-1234567890abcdef0 |
| aws_s3_bucket | Bucket name: my-bucket |
| aws_iam_role | Role name: my-role |
| aws_vpc | VPC ID: vpc-abc123 |
| aws_security_group | SG ID: sg-abc123 |
| aws_db_instance | DB identifier: my-database |
| aws_lambda_function | Function name: my-function |
Best Practices
- Import one resource at a time — easier to debug
- Run plan after each import — verify configuration matches
- Use
terraform state showto see all imported attributes - Consider using
terraformerfor bulk imports - Document what was imported — leave comments in code
Learn More
- Terraform for Beginners Course — hands-on import labs
- Terraform By Example Book — real-world migration patterns
- Terraform Cheat Sheet — quick command reference


