Importing Existing Infrastructure into Terraform

A
Author
30 Jun, 2026 1 min read 1.25K views

Most teams adopt Terraform after resources already exist. Recreating them isn't an option, so you import them: tell Terraform that an existing resource belongs to a block in your code.

Import blocks

# imports.tf
import {
  to = aws_s3_bucket.uploads
  id = "acme-shop-uploads"
}

import {
  to = aws_db_instance.main
  id = "shop-prod-db"
}

Import blocks are part of the normal plan/apply workflow, so they can be reviewed in a pull request (unlike the older terraform import command, which changed state immediately).

Let Terraform write the configuration

terraform plan -generate-config-out=generated.tf

Terraform reads each imported resource and writes matching resource blocks into generated.tf. Treat it as a draft:

  • Remove attributes that are just defaults or computed values.
  • Replace hard-coded IDs with references to other resources.
  • Move blocks into the right files and modules.

Aim for a clean plan

terraform plan
# Plan: 2 to import, 0 to add, 0 to change, 0 to destroy.

Edit the configuration until the plan shows only imports and no changes. Any ~ or -/+ means your code doesn't match reality yet. Fix it before applying, especially for databases.

Importing many resources

import {
  for_each = toset(["logs", "backups", "exports"])
  to       = aws_s3_bucket.buckets[each.key]
  id       = "acme-shop-${each.key}"
}

After importing

  • Delete the import blocks once applied (or keep them as documentation; they're harmless).
  • Agree that changes now go through Terraform only; console edits create drift.
  • Run scheduled terraform plan jobs to detect drift early.
Was this article helpful?

Comments

No comments yet. Have a question or a tip? Be the first to share it.

Leave a comment

Comments are reviewed before they appear.