Home›Guides›Import existing resources

Import existing resources into OpenTofu or Terraform

Resources created in the cloud console are invisible to your code. Import blocks bring them under management without touching them — and, unlike the old import command, the import is reviewed in a plan first.

1. Write an import block

An import block says which existing object should become which resource address. You also need a matching resource block.

import {
  to = aws_s3_bucket.logs
  id = "my-company-logs"
}

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

2. Plan, and read the plan

Run tofu plan. The plan shows the import before anything is written to state. If it also shows changes to the resource, your resource block does not match reality yet — fix the code, not the resource.

3. Let OpenTofu draft the code (optional)

OpenTofu can generate resource blocks for resources not yet declared, with -generate-config-out. The docs mark this as experimental, and it does not work together with for_each on import blocks. Treat the output as a draft to clean up.

4. Import many objects at once

A for_each argument on an import block imports several objects as instances of one resource.

5. Apply, then keep or delete the block

Apply once the plan shows only the import. An import block is active only while the object is not yet tracked at its to address, so leaving it in place is harmless.

Our rule in client work: after an import, plan must show no changes. Anything else means the code is wrong.

Examples use OpenTofu (tofu); in Terraform the commands start with terraform. Check the documentation for your version before relying on a feature.

Hundreds of hand-built resources?We do this as a fixed-price job.ClickOps → Code →

Book a 30-minute call

Tell us what you are trying to fix. No preparation needed.

Book a 30-minute call