Identify Ownership Before Importing Anything
A land registry does not transfer a field because you built a fence around it. Title moves through a recorded claim: a parcel number, a named owner, and a seller who agrees. An import is the same act. The object stays put; only the record of who answers for it changes.
The record has three inputs, and all three must agree before you run anything.
Remote identity is the provider's own identifier, not the Terraform address and not always the human name. For terraform_data, the import id is any string you choose, and it becomes the object's id. For provider-backed resources the id is the provider's: a bucket name, an instance id, sometimes an ARN. Some resources have no importer at all, and Terraform stops with "Resource Import Not Implemented".
Provider account is the identity that makes the calls. Terraform resolves it from provider arguments, environment variables, shared configuration and instance metadata, in that order, so an exported AWS_PROFILE can outrank the provider block.
The wrong-account failure is quiet
If no object with that id exists where Terraform landed, the provider says so. If a same-named object exists there, the import can succeed against something you did not mean to adopt. terraform_data is built in and has no account boundary, which is why the lab needs no credentials; every provider-backed object still needs this question answered.
State owner is the state file that already maps an address to that id. Two claims over one object do not merge: the last apply wins and changes become unattributable, and the lock serialises writes to one state, not two states against one object. Finding the owner means searching another workspace in the same backend, a second repository, a colleague's local state, or a console-driven resource. A managed-by tag is a hint, not proof.
The scenario is mundane. A runbook creates a control record by hand — a marker file, a lock row, an identifier — and six months later a pipeline needs it under Terraform. The import id is imported-001; only the claim changes.
Treat adoption as a scoped hand-off. Before the first plan, collect evidence you can show someone else:
- the object's id in the form the importer expects;
- the backend and workspace whose state will hold the claim;
- the state files you searched, and what each search returned;
- the person or team who agrees to stop editing it by hand.
Import mostly reads; apply writes state and, for provider-backed objects, calls the API. It proves the object exists, not that you can manage it.
Figure 1 turns the checklist into gates; each "no" has its own remedy.
%%{init: {'theme':'base','themeCSS':'.edgeLabel .label text, .edgeLabel text, .edgeLabel span { fill:#132440 !important; color:#132440 !important; }','themeVariables': {'primaryColor':'#16476A','primaryTextColor':'#ffffff','primaryBorderColor':'#132440','secondaryColor':'#3B9797','secondaryTextColor':'#ffffff','secondaryBorderColor':'#132440','tertiaryColor':'#c4873b','tertiaryTextColor':'#132440','tertiaryBorderColor':'#132440','lineColor':'#5a6e82','textColor':'#132440','titleColor':'#132440','mainBkg':'#16476A','nodeBorder':'#132440','clusterBkg':'#eef3f7','clusterBorder':'#132440','edgeLabelBackground':'#ffffff','actorBkg':'#16476A','actorTextColor':'#ffffff','actorBorder':'#132440','activationBkgColor':'#3B9797','activationBorderColor':'#132440','signalColor':'#5a6e82','signalTextColor':'#132440','noteBkgColor':'#c4873b','noteTextColor':'#132440','noteBorderColor':'#132440','labelBoxBkgColor':'#a8dede','labelBoxBorderColor':'#132440','labelTextColor':'#132440','loopTextColor':'#132440','altBackground':'#f8f9fa','fontFamily':'DM Sans, sans-serif'}}}%%
flowchart TD
A["Unmanaged object"] --> B{"Can this identity read it?"}
B -->|"no"| C["Wrong account or id"]
B -->|"yes"| D{"Does another state list it?"}
D -->|"yes"| E["Stop: agree hand-off"]
D -->|"no"| F{"Does this root own the address?"}
F -->|"yes"| G["Remove old record"]
F -->|"no"| H["Record id, tags, owner"]
H --> I["Write the import block"]
classDef primary fill:#132440,color:#ffffff,stroke:#0b172b;
classDef secondary fill:#16476A,color:#ffffff,stroke:#0f314a;
classDef ok fill:#3B9797,color:#ffffff,stroke:#2a6f6f;
classDef danger fill:#BF092F,color:#ffffff,stroke:#7a0420;
classDef warn fill:#c4873b,color:#132440,stroke:#8a5a1f;
class B,D,F primary;
class A,H secondary;
class I ok;
class C,E danger;
class G warn;
Figure 2 puts the three claims side by side with the evidence each needs.
The left and middle columns let the importer record the object; the third makes it legitimate.
Choose CLI Import or an Import Block
Both write the same state entry; they differ in timing and review surface.
CLI form. One command, no plan: the importer records the id and attributes, state is written immediately, and terminal output is the only record. The resource block must already exist and validate, because Terraform needs the address and schema before it can read anything. Whether configuration matches is unknown until someone runs a plan, possibly days later.
Import block form. Terraform 1.5 introduced the block, and a plan now renders the import as its own action beside the add, change and destroy counts. Because it is part of the plan, it can be saved, inspected, policy-checked and applied elsewhere. The target is static, the id may be an expression, and a for_each adopts a set. After a successful apply the block is a no-op; deleting it removes nothing from state.
The block's advantage is its trap. The plan shows the import and the changes the configuration implies; 1 to import, 1 to update attaches the object and modifies it in one run. Only the plan you were supposed to read distinguishes that from a deliberate change.
| Dimension | terraform import | import block |
|---|---|---|
| Configuration | Block must exist | Block required, unless generated |
| Intent lives in | Shell history | A committed, reviewed file |
| State written | Immediately, during the command | During apply, from a reviewed plan |
| Preview | None; next plan is the first review | 1 to import plus post-import actions |
| Wrong id | Fails, or imports the wrong object | Fails the plan before state changes |
| Best use | Single-object recovery | Routine adoption and pipelines |
Figure 3 follows both paths and the branches where each stops being safe.
%%{init: {'theme':'base','themeCSS':'.edgeLabel .label text, .edgeLabel text, .edgeLabel span { fill:#132440 !important; color:#132440 !important; }','themeVariables': {'primaryColor':'#16476A','primaryTextColor':'#ffffff','primaryBorderColor':'#132440','secondaryColor':'#3B9797','secondaryTextColor':'#ffffff','secondaryBorderColor':'#132440','tertiaryColor':'#c4873b','tertiaryTextColor':'#132440','tertiaryBorderColor':'#132440','lineColor':'#5a6e82','textColor':'#132440','titleColor':'#132440','mainBkg':'#16476A','nodeBorder':'#132440','clusterBkg':'#eef3f7','clusterBorder':'#132440','edgeLabelBackground':'#ffffff','actorBkg':'#16476A','actorTextColor':'#ffffff','actorBorder':'#132440','activationBkgColor':'#3B9797','activationBorderColor':'#132440','signalColor':'#5a6e82','signalTextColor':'#132440','noteBkgColor':'#c4873b','noteTextColor':'#132440','noteBorderColor':'#132440','labelBoxBkgColor':'#a8dede','labelBoxBorderColor':'#132440','labelTextColor':'#132440','loopTextColor':'#132440','altBackground':'#f8f9fa','fontFamily':'DM Sans, sans-serif'}}}%%
flowchart TD
A["Existing object"] --> B{"Which workflow?"}
B -->|"terraform import"| C["Importer writes state"]
C --> D{"Configuration matches?"}
D -->|"yes"| E["Next plan: no changes"]
D -->|"no"| F["Next plan: update or replace"]
B -->|"import block"| G["terraform plan: 1 to import"]
G --> H{"Any other actions?"}
H -->|"none"| I["Apply: state only, remote untouched"]
H -->|"update"| J["Apply also changes the object"]
H -->|"replace"| K["Apply destroys and recreates"]
F --> L["Review config before applying"]
J --> L
K --> L
I --> M["Plan again: no changes"]
classDef primary fill:#132440,color:#ffffff,stroke:#0b172b;
classDef secondary fill:#16476A,color:#ffffff,stroke:#0f314a;
classDef ok fill:#3B9797,color:#ffffff,stroke:#2a6f6f;
classDef danger fill:#BF092F,color:#ffffff,stroke:#7a0420;
classDef warn fill:#c4873b,color:#132440,stroke:#8a5a1f;
class B,D,H primary;
class A,C,G,I,M secondary;
class E ok;
class F,J warn;
class K danger;
The dangerous node is H: an import plan is not automatically a safe plan; read the action summary.
Review Generated Configuration as a Draft
Hand-writing a block for an object you never managed is guesswork. Terraform can draft one: keep the import block, leave out the resource block, and add a generation path.
terraform plan -generate-config-out=generated.tf
The plan can fail and still write the file
The output path must not exist. The flag is still experimental, and if the plan fails for an unrelated reason Terraform may still write the file. Treat the result as an artifact, not a result: untrusted input that happens to be HCL.
Generation transcribes the provider's view; it does not design a configuration. Every argument the schema exposes appears, including null values and attributes nobody manages. For terraform_data, that means a null input and a null triggers_replace beside nothing else.
Provider-backed resources show the same pattern. Empty lists, zeroed counters and computed identifiers are not wrong, but they hide the arguments you must decide about.
The review is editorial and ordered:
- Identity. Keep the argument that names the object. For
terraform_data, the id lives in the import block, not in an argument. - Required arguments. Keep what the provider cannot derive; delete what it recomputes.
- Nulls and computed values. Remove them; a managed
nullis a promise you never made. - Defaults. Know which optional arguments the provider fills on a fresh create.
- Sensitive values. Read every line before the file reaches Git.
Defaults cause most first-plan surprises. Generated configuration reflects what exists, not what a fresh create would do, and terraform_data has no defaults at all.
| Default | What it does | Review action |
|---|---|---|
An S3 bucket's force_destroy | Defaults to false and is never stored, so it cannot drift, but it decides what a destroy does. | Decide it deliberately before the first destroy. |
tags and tags_all | Omitting tags keeps existing tags; tags_all merges them with any default_tags. | Adding default_tags later proposes a tag update. |
| Bucket ACLs | AWS provider 4 moved them to aws_s3_bucket_acl. | A generated acl can fight a resource another team owns. |
Generation does not sanitise
If the schema lets the provider return a value, it can appear in the file, and the file is plaintext. State holds the same values regardless, but drafts get committed "temporarily". Keep the file out of version control until every line is read; a value later printed as (sensitive value) was written in full here.
The panels below are the reviewed end state and the commands that prove it. The HCL is the file after two phases: an empty block imported first, then input added as a separate change.
Generation runs while main.tf holds only the import block; the HCL panel is the file after the baseline and the input change. Delete the generated file once merged, or the duplicate declaration stops the next plan.
Resource Identity and Bulk Discovery Are Separate Capabilities
Everything so far concerns one object. Discovery has a different failure mode: finding what exists before deciding what to adopt.
list blocks describe a query. Terraform 1.14 added them; a block names a resource type, requires a provider, and filters through a nested config block. It belongs in a .tfquery.hcl file, not in ordinary configuration.
terraform query runs the query. Candidates return with a management status, and a generation flag can emit import and resource blocks. The default limit is 100, a hint about intended scale.
Requirements are stricter than they look. The CLI must be new enough, and the provider must implement list resources for that type. terraform_data does not, and support for import does not imply support for list.
The provider argument is mandatory and decides which account is searched. Results are a snapshot of what those credentials can read.
Inventory is not adoption
The real use is answering "which objects exist here that no state mentions?" Volume defeats review: two hundred generated blocks produce one enormous plan, and the reviewer approves that it is large, not that each identity and default is right. Every import is an ownership decision, and those do not batch.
Absence is not evidence of being unmanaged. Fewer results can mean fewer objects, a narrower policy, or an unhelpful filter.
The sensible split: use list and terraform query to build a candidate inventory, read it as a map rather than a work queue, and adopt a chosen subset with individual import blocks and individual no-change plans. The later deep dive takes query syntax and filters apart; discovery never substitutes for the ownership check in Figure 2.
Converge to a No-Change Plan Before Editing Behavior
Adoption has one acceptance test: after the import, a fresh plan proposes nothing. That is no-change, and the only proof that the configuration describes the object as it exists rather than as you imagined it.
Apply the import plan only when its summary is import and nothing else. Then plan again without generation and read the legend:
~is an in-place update.-/+or+/-is a replacement.+and-are separate creates and destroys.
An in-place update is still a change, and a replacement is a destruction. Neither belongs in an adoption commit.
That rule shaped the lab. Importing records only the id, so a block containing input = "kept" would turn the import plan into an import plus a change. The baseline imports an empty block and proves no-change first; the input is a second, deliberate plan. A provider-backed object needs the same discipline, even when the first plan tempts you with a small diff.
When the plan is not empty, the cause is usually on a short list:
| Cause | What you see | Response |
|---|---|---|
| Argument the provider does not store | A value that exists only in Terraform's model always differs. | Remove it or accept the drift knowingly. |
| Computed attribute | output for terraform_data; ARNs and identifiers for provider objects. | Do not manage it; manage the input. |
| Immutable argument | Changing triggers_replace forces replacement. | Stop and decide whether replacement is intended. |
| Default applied later | default_tags are right but were never on the object. | Plan the tag change separately. |
| Normalisation | Policy JSON, CIDR and provider rewrites compare unequal. | Match the provider's canonical form. |
| Identity difference | A different name is a different object. | Fix the address or the id, not the attribute. |
The rule is the same for all of them: edit the configuration until it describes reality. Do not apply a change to make reality match a draft generated ten minutes ago. If the object genuinely needs a new tag or a tighter policy, make that a separate plan with its own review. Mixing adoption with behaviour change destroys the evidence that the import was correct.
Replacement is not the first act of adoption
If the plan shows -/+ or +/-, an immutable argument changed: applying it deletes the object and creates a new identity. An in-place update is still a change. Find the forcing argument first, and check whether the draft copied a value the object does not have.
ignore_changes is a narrow tool, not a fix. It can suppress a computed attribute the provider rewrites on every read, which is legitimate when documented. Using it to hide a replacement you do not understand turns a visible risk into an invisible one.
Record the evidence while it is fresh: the saved import plan, the action summary, the post-apply state show output, and the no-change exit status. terraform plan -detailed-exitcode returns 0 for no changes, 1 for an error and 2 for a diff, making it a pipeline gate rather than something a human eyeballs. Figure 4 shows the three outcomes.
Import a Disposable Resource and Record the Evidence
The lab adopts one identity Terraform did not record and proves the result with a plan, not a screenshot of "Import successful".
Adopt a resource safely
Status: executed. Run on 5 November 2026 with Terraform 1.17.1, the built-in terraform_data resource and a local backend. No providers were downloaded and no cloud account was used.
| Phase | Plan summary | Exit code |
|---|---|---|
| 1. Import the empty block | 1 to import, 0 to add, 0 to change, 0 to destroy | 0 |
2. Add input = "kept" | 0 to add, 1 to change, 0 to destroy | 0 |
Prerequisites. Terraform 1.17.1 and a Git repository. No credentials or cloud account.
Minimal setup. An empty directory whose only file is a main.tf holding the version constraint and the import block.
- Ownership. Run the three checks from the code widget: workspace, state list and the
git grepscan. All must show that nothing owns the address. - Draft. Run init, then plan with
-generate-config-out=generated.tf. Expect a draft containing only nulls. - Edit. Delete the nulls, merge the empty resource block into
main.tf, deletegenerated.tf, and validate. - Import plan. Save it with
-out=tfplan-import. The summary must be1 to import, 0 to add, 0 to change, 0 to destroy. - Apply and inspect. Apply the saved plan, then run a state show. Expect only the imported id.
- No-change gate. Plan again with
-detailed-exitcodeand record the exit code. Expect 0. - Deliberate change. Add
input = "kept", plan, apply, then repeat the no-change check. Expect one change and a final 0. - Evidence. Record the version, both plan summaries, both exit codes and the final state output; never state or plan files.
Expected observation. A first plan whose only action is the import, a state holding just the id, and two exit codes of 0 split by one deliberate change.
| Symptom | Meaning |
|---|---|
Resource already managed by Terraform | Another state owns the address. |
A -/+ plan | An immutable argument, such as triggers_replace, differs. |
1 to import, 1 to change in phase 1 | The block was not empty. |
| An empty state list after apply | The wrong directory. |
Cost dimensions. Zero; nothing is provisioned.
Cleanup. Run terraform destroy to remove the record, confirm the state list is empty, then delete the directory.
Next in the Series
In Refactor Without Rebuilding: Moved and Removed Blocks, we'll explore Evolve resource and module addresses while preserving clear ownership and migration history.