🎉 DevOps Interview Prep Bundle is live — 1000+ Q&A across 20 topicsGet it →
All Articles

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.

Shubham2 min read
Share:Tweet

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

bash
terraform import <resource_address> <resource_id>

Common Resource Imports

EC2 Instance

bash
terraform import aws_instance.web i-0a1b2c3d4e5f67890

S3 Bucket

bash
terraform import aws_s3_bucket.my_bucket my-bucket-name

VPC

bash
terraform import aws_vpc.main vpc-0a1b2c3d4e5f

RDS Instance

bash
terraform import aws_db_instance.postgres my-postgres-db

EKS Cluster

bash
terraform import aws_eks_cluster.main my-cluster-name

Full Import Workflow

Step 1: Write the resource in Terraform

hcl
resource "aws_s3_bucket" "logs" {
  bucket = "my-company-logs-2026"
}

Step 2: Import

bash
terraform import aws_s3_bucket.logs my-company-logs-2026

Step 3: Run plan and fix config to match reality

bash
terraform plan
# Shows drift — update .tf to match actual resource

Step 4: Confirm no changes

bash
terraform plan
# "No changes. Your infrastructure matches the configuration."

Bulk Import with import blocks (Terraform 1.5+)

hcl
import {
  to = aws_s3_bucket.logs
  id = "my-company-logs-2026"
}
 
import {
  to = aws_instance.web
  id = "i-0a1b2c3d4e5f67890"
}
bash
terraform plan -generate-config-out=generated.tf
# Generates resource config automatically

Common Errors

Resource already in state:

bash
terraform state rm aws_s3_bucket.logs
terraform import aws_s3_bucket.logs my-company-logs-2026

Plan 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

bash
# 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_logs

More Terraform? Read our Terraform state lock error fix and OpenTofu complete guide.

🔧

Today I Fixed

Short real fixes from production — posted daily

Browse fixes
Newsletter

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

Comments