Importing existing infrastructure
Adopt resources created by hand with import blocks and -generate-config-out, find the right ID, and clean up generated code before you trust it.
The terraform state commandsMoved blocks and safe refactoring
Importing existing infrastructure
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- An `import` block pairs a real object with an address in your code: `to` is the address and `id` is the provider's identifier. The next plan shows it as an import, and apply writes it into state.
- `terraform plan -generate-config-out=generated.tf` writes a first draft of the resource block for you. It is a template to edit, not code to commit as it is.
- The `import` block replaced the old one-resource-at-a-time `terraform import` command. It shows up in a plan, can be reviewed, and with `for_each` (Terraform and OpenTofu 1.7) handles many objects.
- Importing only fills state. If your resource block disagrees with the real object, the plan after the import shows the differences, and applying changes the real thing.
- Terraform 1.12 added an `identity` attribute as an alternative to `id`, for providers that support it. Terraform 1.14 added `terraform query` to find objects in bulk. The second is a Terraform feature, and I found no OpenTofu equivalent.
The bucket nobody wrote down
A team’s first Terraform project starts in an account that already has a year of infrastructure in it. Someone built the VPC in the console, a script made the buckets, and a contractor left a database behind. You can’t re-create these, because they hold data and traffic. You also can’t leave them out, because the point of Terraform is that the code describes what exists. So the job is to teach state about objects that already exist, without touching them.
The old command and the new block
The original way was terraform import ADDRESS ID. It had real problems: it changed state immediately, with no plan to review, it handled one object per invocation, and you wrote the resource block by hand first. Terraform 1.5 introduced the import block, and OpenTofu has it as well. It lives in code:
import {
to = aws_s3_bucket.uploads
id = "notes-uploads-example-1234"
}
resource "aws_s3_bucket" "uploads" {
bucket = "notes-uploads-example-1234"
}
I wrote that from the AWS provider docs for aws_s3_bucket and did not run it. to is the address the object will have in state. id is whatever the provider uses to identify it. For a bucket that is the bucket name, and the provider docs say so in an Import section on every resource page. The ID is the part people get wrong, because it varies: a name, an ARN, a vpc-id/subnet-id pair. Always read that section.
The state-level effect, shown with the toy planner from earlier in this group (a stand-in that compares address sets and prints my own labels):
Bucket exists in cloud but not in state:
+ create aws_s3_bucket.notes
--- with import
# aws_s3_bucket.notes will be imported
Without the block, a plan sees a resource in the code and nothing in state and proposes to create it, which fails on the name or makes a duplicate. With it, the plan reports an import. Apply writes the object into state. The block can stay or go afterwards, because once the object is in state it has done its work. Remove it in a later cleanup pull request.
Many at once and new ways to identify
Since Terraform and OpenTofu 1.7, for_each works in an import block, so a map of names to IDs can import a dozen buckets in one go. Terraform 1.12 added an identity attribute, mutually exclusive with id. Instead of one string it takes the structured identity the provider defines. The AWS provider docs for aws_s3_bucket list bucket as required and account_id and region as optional, and show:
import {
to = aws_s3_bucket.example
identity = {
bucket = "bucket-name"
}
}
That is also from the provider docs, not a run of mine. It is a Terraform 1.12 feature in the changelog, and only providers that implement identity support it.
For finding objects, Terraform 1.14 added terraform query, which runs list operations against existing infrastructure and can generate import configuration from the results. The docs describe it as the bulk route. I found it in the Terraform 1.14 changelog and the docs, and did not find an OpenTofu equivalent in its changelogs.
Generating the resource block
Writing a resource block with forty arguments to match a live object is tedious and error-prone. So the plan can write it:
terraform plan -generate-config-out=generated.tf
Give it a path to a file that does not exist. For each import block that has no matching resource block, Terraform asks the provider for the object’s attributes and writes a block. The docs state clearly what this produces: a template with Terraform’s best guess at each argument, which you should review, trim and reorganize before committing. It was marked experimental when 1.5 shipped, and the docs warn about the formatting changing in later versions. They also note a limitation: for some resources with complex schemas, the provider’s values conflict and Terraform cannot build valid configuration, and prints an error instead.
My workflow is the one the docs suggest. Generate. Delete arguments that are computed or equal to the provider default. Replace repeated literals with variables. Move the block into the file where it belongs. Run plan again until it reports no changes. That last step matters most. A plan that wants to change the freshly imported object means your code and the real thing disagree, and you decide who is right before you apply.
Habits
Import into a branch, in a pull request, with the plan as evidence. Take a copy of the state or turn on bucket versioning first, since an import is a state write. If you import an object that a different configuration already manages, two states claim one object, and the next change from one breaks the other. Check terraform state list in the other repo first.
Importing one object does not import its neighbours. A VPC import leaves its subnets, route tables and gateways untracked, and the plan will cheerfully propose to create duplicates of those. Plan the whole slice you are adopting before you import any of it.
About the video
The embed is “Terraform Import Got WAY Easier!” from TeKanAid, published March 2025, 14 minutes. Its description says it covers the import block, -generate-config-out and the pitfalls of the old command, which is the core of this lesson. It is sponsored by UpCloud, which the description discloses, and the technique shown is provider-neutral. It was published before terraform query and, as far as the description shows, does not cover for_each in imports or identity. Ned Bellavance’s 2023 video on the 1.5 import block is in deeper, with more discussion of what the feature does not do. English was judged from the title, description and the English auto-caption track.
Where this fits
Import, move and remove all deal with the mapping between code and reality at one moment. Over time the mapping drifts, because people change things by hand. That is drift detection and refresh-only plans.
Check yourself
You import a bucket and the next plan shows `~ update in-place` on its tags. What does that mean?
The import only wrote the real object into state. Your resource block declares different tags than the bucket has, so the plan wants to change the bucket to match your code. Either fix the block or accept the change.
Where do you find the `id` to put in an import block?
In the provider's documentation for that resource type, in its Import section. For `aws_s3_bucket` it is the bucket name. For other resources it can be an ARN, a combined key or a name.
Why is the generated configuration from `-generate-config-out` not ready to commit?
Terraform asks the provider for every attribute and writes them all, including computed and default values, and it may fail on resources with conflicting arguments. The docs call it a best guess and recommend trimming and restructuring it.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.