Importing Existing Infrastructure
Infrastructure created outside Terraform can be brought under management by importing it. This lesson covers import blocks, generated configuration and the older terraform import command.
Learning outcomes
After completing this lesson, you will be able to:
- Describe import blocks and generated configuration.
- Distinguish import blocks from the terraform import command.
Why import
Organisations adopting Terraform usually already have infrastructure built by hand, by scripts or by other tools. Importing binds an existing object to a resource address in state, so that Terraform manages it from then on without recreating it. Import never creates, changes or deletes infrastructure by itself: it reads the object and records it. Each real object should be imported into exactly one address in one state, otherwise two configurations will fight over it.
Import blocks
An import block, available since Terraform 1.5, declares an import as part of the configuration. The to argument is the resource address and id is the object's import ID, whose format each resource type documents in the Import section of its provider page.
import {
to = aws_s3_bucket.logs
id = "acme-legacy-logs"
}
resource "aws_s3_bucket" "logs" {
bucket = "acme-legacy-logs"
}The next terraform plan shows aws_s3_bucket.logs: Preparing import... and then the import together with any changes needed to make the object match the configuration, and terraform apply performs it. Because the import is planned, it can be reviewed in a pull request like any other change and applied by the usual pipeline. Since Terraform 1.7, import blocks accept for_each, so many objects can be imported from a map.
locals {
buckets = { logs = "acme-legacy-logs", reports = "acme-legacy-reports" }
}
import {
for_each = local.buckets
to = aws_s3_bucket.legacy[each.key]
id = each.value
}In Terraform 1.12, import blocks must be in the root module, although to may name an address inside a child module, such as module.storage.aws_s3_bucket.this. After a successful import, the block can be removed or kept as a record; it has no further effect.
Import block or import command?
Generating configuration
Writing a resource block that exactly matches an existing object is tedious. If an import block's to address has no resource block, terraform plan -generate-config-out=generated.tf asks the provider for the object's current settings and writes a resource block for it into the named file, which must not exist yet.
-
Write import blocks
Add an import block for each object, without resource blocks.
-
Generate configuration
Run terraform plan -generate-config-out=generated.tf.
-
Review and tidy
Remove defaults and computed values, replace literals with references and variables, move blocks to suitable files.
-
Plan again
Repeat until the plan shows only the imports and no unexpected changes.
-
Apply
Run terraform apply to complete the imports.
The following diagram shows the import process from blocks to apply.

Review generated code
Generated configuration is a starting point, not finished code. It can contain arguments that conflict with each other or values that should be references, and Terraform marks the file as generated so that you review it.
Importing with generated configuration
The terraform import command
Before import blocks, the only method was the terraform import command, which is still available.
terraform import aws_s3_bucket.logs acme-legacy-logsThe resource block must already exist in configuration. The command reads the object and writes it into state immediately, one object per command, with no plan to review and no record in code. HashiCorp recommends import blocks for new work, because they are planned, repeatable, can import many objects and can generate configuration.
Adopting hand-built buckets
A team has 12 S3 buckets created by hand and wants Terraform to manage them through its normal pull request process, without writing every bucket's configuration by hand.
The decisive requirements are reviewability and generated configuration.
The team writes one import block per bucket, with no resource blocks, and runs terraform plan -generate-config-out=buckets.tf. They review the generated file, remove computed values and move shared settings into locals, then plan again until it shows 12 imports and no other changes. The pull request contains the import blocks and the tidied configuration, and after merge the pipeline applies it. Later, they can consolidate the buckets into one for_each resource with moved blocks.
Which option makes terraform plan write resource blocks for imported objects?
Show answer
-generate-config-out=FILE, for import blocks that have no matching resource block.
Exam Tips
Remember for the exam
- Import binds existing objects to addresses; it never creates infrastructure.
- import blocks (1.5+) are planned and support for_each (1.7+).
- plan -generate-config-out writes resource blocks for imports without them.
- terraform import needs the resource block and has no plan.