Lesson 1 of 3 Importing, Inspecting and Troubleshooting

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.

Sort into groups

Import block or import command?

Exam skills 7.1.1 · 7.1.3

Decide which import method each statement describes.

Log in to try this activity Interactive activities are free with an account.

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.

  1. Write import blocks

    Add an import block for each object, without resource blocks.

  2. Generate configuration

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

  3. Review and tidy

    Remove defaults and computed values, replace literals with references and variables, move blocks to suitable files.

  4. Plan again

    Repeat until the plan shows only the imports and no unexpected changes.

  5. Apply

    Run terraform apply to complete the imports.

The following diagram shows the import process from blocks to apply.

Adopting hand-built resources through the normal review process. One import block per bucket and no resource blocks; plan -generate-config-out writes the configuration; the team reviews and tidies it, plans again until only the imports remain, and the pipeline applies the merged pull request.
The bucket name in the import block is illustrative.

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.

Put in order

Importing with generated configuration

Exam skills 7.1.1 · 7.1.2

Arrange the steps of importing existing buckets with generated configuration.

Log in to try this activity Interactive activities are free with an account.

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-logs

The 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.

terraform import command
Needs an existing resource blockYes
Preview in a planNo
Objects per runOne
Record in codeNo
import block
Needs an existing resource blockNo, can generate one
Preview in a planYes
Objects per runMany, including with for_each
Record in codeYes
Worked example

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.

Check yourself

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.