Back to Terraform

Adopt Existing Infrastructure with Reviewable Imports

November 5, 2026 Wasil Zafar 18 min read

Move unmanaged resources under Terraform without accidentally replacing them

Table of Contents

  1. Identify Ownership Before Importing Anything
  2. Choose CLI Import or an Import Block
  3. Review Generated Configuration as a Draft
  4. Resource Identity and Bulk Discovery Are Separate Capabilities
  5. Converge to a No-Change Plan Before Editing Behavior
  6. Import a Disposable Resource and Record the Evidence

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.

Identify Ownership Before Importing Anything
%%{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.

Three claims that must agree before an import Remote identity, execution context and state owner must agree before import. Three claims, one object 1. Remote identity imported-001 any string; becomes id 2. Execution context built-in provider local backend, workspace No credentials 3. State owner terraform_data.adopted no other root lists it One writer Disagreement is a stop, not a retry A second owner means two writers; an unclear id means the wrong record.

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.

# main.tf — import block form.
import {
  to = terraform_data.adopted
  id = "imported-001"
}

resource "terraform_data" "adopted" {}
# CLI form: the resource block must already exist and validate.
terraform import terraform_data.adopted imported-001
terraform plan

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.

Dimensionterraform importimport block
ConfigurationBlock must existBlock required, unless generated
Intent lives inShell historyA committed, reviewed file
State writtenImmediately, during the commandDuring apply, from a reviewed plan
PreviewNone; next plan is the first review1 to import plus post-import actions
Wrong idFails, or imports the wrong objectFails the plan before state changes
Best useSingle-object recoveryRoutine adoption and pipelines

Figure 3 follows both paths and the branches where each stops being safe.

Choose CLI Import or an Import Block
%%{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:

  1. Identity. Keep the argument that names the object. For terraform_data, the id lives in the import block, not in an argument.
  2. Required arguments. Keep what the provider cannot derive; delete what it recomputes.
  3. Nulls and computed values. Remove them; a managed null is a promise you never made.
  4. Defaults. Know which optional arguments the provider fills on a fresh create.
  5. 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.

DefaultWhat it doesReview action
An S3 bucket's force_destroyDefaults 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_allOmitting tags keeps existing tags; tags_all merges them with any default_tags.Adding default_tags later proposes a tag update.
Bucket ACLsAWS 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.

# main.tf — final state. The block was imported empty; input came after.
terraform {
  required_version = "1.17.1"
}

import {
  to = terraform_data.adopted
  id = "imported-001"
}

resource "terraform_data" "adopted" {
  input = "kept"
}
# Ownership evidence first.
export ADDRESS="terraform_data.adopted"
export OBJECT_ID="imported-001"

terraform version
terraform workspace show
terraform state list | grep -F "$ADDRESS" || echo "not managed in this state"
git grep -n "$OBJECT_ID" -- '*.tf' | grep -v '^main.tf:' \
  || echo "id not referenced by other configuration"

# Phase 1: draft from the import block alone.
terraform init -input=false
rm -f generated.tf
terraform plan -input=false -generate-config-out=generated.tf
# Review, drop both nulls, merge an empty resource block, delete generated.tf.
terraform validate
terraform plan -input=false -out=tfplan-import
terraform show -no-color tfplan-import | grep -E 'to import|to add|to change|to destroy|imported from'

terraform apply -input=false tfplan-import
terraform plan -input=false -detailed-exitcode
echo "baseline exit=$?"

# Phase 2: input = "kept" is a separate, reviewed change.
terraform plan -input=false -out=tfplan-input
terraform apply -input=false tfplan-input
terraform plan -input=false -detailed-exitcode
echo "final exit=$?"

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:

CauseWhat you seeResponse
Argument the provider does not storeA value that exists only in Terraform's model always differs.Remove it or accept the drift knowingly.
Computed attributeoutput for terraform_data; ARNs and identifiers for provider objects.Do not manage it; manage the input.
Immutable argumentChanging triggers_replace forces replacement.Stop and decide whether replacement is intended.
Default applied laterdefault_tags are right but were never on the object.Plan the tag change separately.
NormalisationPolicy JSON, CIDR and provider rewrites compare unequal.Match the provider's canonical form.
Identity differenceA 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.

Three plan outcomes after an import State and configuration feed a plan that resolves to no change, update or replacement. What the next plan decides State after import address to remote id Reviewed config identity plus chosen args terraform plan refreshed comparison No changes Exit code 0. Record it and stop. In-place update A change, not an import. Review separately. Replacement Stop. An immutable argument differs. Edit configuration until the plan is empty, then change behaviour in a new plan.

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

Lab

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.

PhasePlan summaryExit code
1. Import the empty block1 to import, 0 to add, 0 to change, 0 to destroy0
2. Add input = "kept"0 to add, 1 to change, 0 to destroy0

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.

  1. Ownership. Run the three checks from the code widget: workspace, state list and the git grep scan. All must show that nothing owns the address.
  2. Draft. Run init, then plan with -generate-config-out=generated.tf. Expect a draft containing only nulls.
  3. Edit. Delete the nulls, merge the empty resource block into main.tf, delete generated.tf, and validate.
  4. Import plan. Save it with -out=tfplan-import. The summary must be 1 to import, 0 to add, 0 to change, 0 to destroy.
  5. Apply and inspect. Apply the saved plan, then run a state show. Expect only the imported id.
  6. No-change gate. Plan again with -detailed-exitcode and record the exit code. Expect 0.
  7. Deliberate change. Add input = "kept", plan, apply, then repeat the no-change check. Expect one change and a final 0.
  8. 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.

SymptomMeaning
Resource already managed by TerraformAnother state owns the address.
A -/+ planAn immutable argument, such as triggers_replace, differs.
1 to import, 1 to change in phase 1The block was not empty.
An empty state list after applyThe 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.