Terraform Advanced — terraform import Command

less than 1 minute read

The classic CLI approach to importing existing resources into Terraform state.


The Workflow

1. Write the resource block in .tf (empty or partial)
2. Run terraform import <address> <id>
3. terraform plan to see drift
4. Update .tf code to match actual resource
5. terraform plan shows "No changes" ✅

Example: Import an S3 Bucket

# Step 1: Write the resource block
resource "aws_s3_bucket" "legacy" {
  # We'll fill this in after import
}
# Step 2: Import
$ terraform import aws_s3_bucket.legacy my-existing-bucket
aws_s3_bucket.legacy: Importing...
aws_s3_bucket.legacy: Import successful!

# Step 3: Check state
$ terraform state show aws_s3_bucket.legacy
# Shows all current attributes
# Step 4: Update the resource to match
resource "aws_s3_bucket" "legacy" {
  bucket = "my-existing-bucket"
}
# Step 5: Verify
$ terraform plan
No changes. Your infrastructure matches the configuration.

Limitations of CLI Import

  • You must manually write the .tf code
  • Only one resource at a time
  • No built-in way to generate config
  • Doesn’t handle complex resources well (nested blocks)

What’s Next?

Use the declarative import block instead.

← Importing Infrastructure Import Block (HCL) →

Updated: