Terraform Import Existing Resources: Fix State Mismatch and Drift
Existing AWS resources not in Terraform state? Use terraform import to bring them under management, fix state drift, and avoid accidental resource deletion — with exact commands for EC2, RDS, S3, VPC, and EKS.
Resources created manually or by other tools are not in your Terraform state. When Terraform plans, it tries to create them again or destroys existing ones. terraform import fixes this.
Basic Import Syntax
terraform import <resource_address> <resource_id>Common Resource Imports
EC2 Instance
terraform import aws_instance.web i-0a1b2c3d4e5f67890S3 Bucket
terraform import aws_s3_bucket.my_bucket my-bucket-nameVPC
terraform import aws_vpc.main vpc-0a1b2c3d4e5fRDS Instance
terraform import aws_db_instance.postgres my-postgres-dbEKS Cluster
terraform import aws_eks_cluster.main my-cluster-nameFull Import Workflow
Step 1: Write the resource in Terraform
resource "aws_s3_bucket" "logs" {
bucket = "my-company-logs-2026"
}Step 2: Import
terraform import aws_s3_bucket.logs my-company-logs-2026Step 3: Run plan and fix config to match reality
terraform plan
# Shows drift — update .tf to match actual resourceStep 4: Confirm no changes
terraform plan
# "No changes. Your infrastructure matches the configuration."Bulk Import with import blocks (Terraform 1.5+)
import {
to = aws_s3_bucket.logs
id = "my-company-logs-2026"
}
import {
to = aws_instance.web
id = "i-0a1b2c3d4e5f67890"
}terraform plan -generate-config-out=generated.tf
# Generates resource config automaticallyCommon Errors
Resource already in state:
terraform state rm aws_s3_bucket.logs
terraform import aws_s3_bucket.logs my-company-logs-2026Plan shows changes after import:
Update your .tf to match actual resource attributes. Use terraform state show aws_s3_bucket.logs to see all current values.
State Management Commands
# List all managed resources
terraform state list
# Show resource details
terraform state show aws_s3_bucket.logs
# Remove from state (stop managing)
terraform state rm aws_s3_bucket.old_logsMore Terraform? Read our Terraform state lock error fix and OpenTofu complete guide.
Today I Fixed
Short real fixes from production — posted daily
Stay ahead of the curve
Get the latest DevOps, Kubernetes, AWS, and AI/ML guides delivered straight to your inbox. No spam — just practical engineering content.
Related Articles
Terraform Backend S3 Init Failed — Every Cause and Fix (2026)
terraform init fails with S3 backend errors — access denied, bucket does not exist, state lock issues, wrong region. Here's every cause and the exact fix for each one.
Terraform Destroy Stuck on Dependency Violation: Fix in 5 Minutes
terraform destroy failing with 'DependencyViolation' or hanging on a resource that refuses to delete? Here is exactly how to diagnose orphaned dependencies, out-of-band changes, and deletion protection blocking a clean destroy.
Terraform Import: Fixing State Conflicts with Existing Resources
Getting state conflicts or duplicate resource errors with terraform import? Learn how to import existing AWS resources, fix config mismatches, and use moved blocks to rename state entries.