Practice Exams:

HashiCorp Terraform Associate 004: Importing Infrastructure into Terraform

Terraform adoption often begins in an environment that already contains infrastructure. Import is the bridge between resources that exist in a provider and resources that Terraform should manage. The operation sounds simple—associate an existing object with a Terraform resource address—but the engineering work is really about bringing code, state, provider behavior, and the live object into a consistent model.

The current Terraform Associate 004 objectives explicitly include importing existing infrastructure, and the exam tests against Terraform 1.12. That makes import a core Terraform skill rather than an edge case. In production, the safest workflow treats import as a reconciliation project: understand the object, write the intended configuration, import it, inspect the plan, and remove unexpected differences before normal automation takes over.

Within cloud-native infrastructure, import matters because infrastructure as code is valuable only when the code is a trustworthy representation of what the platform actually contains. A state entry without maintainable configuration creates a new form of drift rather than solving the old one.

Inventory the resource before importing it

Start with the live object and its relationships. Identify the provider account or project, region, parent resources, network dependencies, attached policies, identifiers, lifecycle constraints, and any fields managed by other systems. Importing the wrong object into the right-looking address can create a dangerous plan later.

Decide whether the resource should truly become Terraform-managed. Some objects are intentionally managed by another controller, vendor service, or operational process. Two systems attempting to own the same mutable attributes will create recurring drift and unpredictable changes.

Infrastructure as code works best with clear ownership. The organization should know which tool is authoritative for each resource before it writes state.

Write the destination resource address first

Terraform needs a resource address that describes where the imported object belongs in configuration. That address can be a root resource, a resource inside a module, or an indexed instance created with count or for_each. The address should reflect the structure the team intends to maintain, not merely the fastest place to put the object.

If the long-term design uses a module, consider importing directly into that module address rather than importing to a temporary root resource and moving it later. The surrounding configuration should already include provider requirements, aliases where necessary, and the variables needed to evaluate the module.

Stable resource addressing matters because state connects the live object to the address. Renaming or restructuring later is possible, but every unnecessary move adds operational work and an opportunity for accidental recreation.

Choose CLI import or import blocks deliberately

Terraform supports the traditional CLI import workflow and configuration-driven import blocks. The CLI command creates the state association immediately, while import blocks let the import participate in plan and apply workflows and can be reviewed as configuration. Teams should choose the approach that fits their automation and change-control model.

Configuration-driven import is useful when imports need code review, repeatability, or coordination with generated configuration. The classic CLI path can be efficient for controlled one-off migrations. In both cases, the critical step is what happens after the state association: the configuration and live resource still need to converge.

Avoid treating import as proof that Terraform now understands every setting. Provider schemas determine what is recorded, computed, defaulted, or configurable. The first plan after import is where that gap becomes visible.

Use the first plan as a reconciliation report

After import, run a plan and read every proposed change. Some differences are harmless defaults or computed values. Others reveal that the written configuration does not match production. A plan that wants to replace or materially modify a just-imported resource should stop the migration until the difference is understood.

Reconcile configuration toward the intentional live state or deliberately approve a change toward the desired future state. Do not blindly copy every provider-reported attribute into code. Some fields are read-only, ephemeral, provider defaults, or details the team should not manage explicitly.

Configuration risk is relevant because import can expose years of undocumented settings. The migration is an opportunity to decide which settings are required and which are historical residue.

Handle secrets and sensitive values carefully

Import can bring references to sensitive infrastructure into state even when the Terraform configuration does not contain the secret in plaintext. Teams should understand which provider attributes are stored in state and protect the backend accordingly. State is operational data with security consequences.

Do not paste secrets into configuration merely to silence a plan. Prefer provider-supported secret references, sensitive variables, write-only or ephemeral capabilities where appropriate, and external secret-management patterns. The correct approach depends on the resource and provider.

Terraform state should be treated as controlled infrastructure metadata. Import does not reduce the need for encryption, access control, locking, backup, and careful handling of state copies.

Import in dependency-aware batches

Large estates are easier to migrate in logical layers. Foundational network or identity objects may need to be represented before dependent services. Importing everything at once creates a plan too large to reason about and makes it difficult to identify which configuration difference produced which proposed change.

Use small batches with repeatable checks: import, plan, reconcile, peer review, and then advance. Record which resources remain outside Terraform so operators understand the boundary during the migration period.

When modules create many related resources, decide whether the migration unit should be the whole module or a smaller subset. The right boundary is the one that allows the team to validate ownership and change safely without leaving hidden cross-tool dependencies.

Protect against accidental recreation

The most dangerous import failure is not a failed command; it is a successful import followed by an apply that replaces a critical resource unexpectedly. Read provider force-replacement behavior, lifecycle settings, naming constraints, immutable fields, and dependencies before allowing automated apply.

Temporary lifecycle controls can provide protection during migration, but they should not become permanent substitutes for understanding the configuration. A blanket prevent_destroy rule can stop catastrophe, yet it can also hide a structural problem that will surface later.

Change governance is appropriate for high-impact imports. Treat the first managed apply as a production change with rollback thinking, approvals, observation, and a clear recovery path.

Finish by making Terraform authoritative

Engineers preparing through HashiCorp certifications should think beyond the import command. A completed migration means the resource is represented in maintainable configuration, state is stored safely, the plan is understood, CI can run consistently, and operators know that future changes belong in Terraform.

Update runbooks and ownership. If console changes remain common after import, drift will return immediately. Some organizations restrict manual changes technically; others allow emergency changes but require them to be reconciled into code afterward. Either model can work if the authority boundary is explicit.

The final test is predictability. A normal plan should show no unintended changes, another engineer should understand the configuration, and the team should be able to explain how a future modification will move from code review to applied infrastructure.

Import is not simply a state operation. It is a controlled transition from unmanaged or differently managed infrastructure into an infrastructure-as-code operating model.

The safest teams use import to create alignment among the live object, configuration, state, provider version, automation, and ownership—then prove that alignment with a clean, explainable plan.

Use generated configuration as a starting point

Configuration-generation features can accelerate import for supported workflows, but generated code should be reviewed as a draft rather than accepted as architecture. Provider schemas often expose many attributes, and generated configuration may reflect the current object without expressing the clean interface or module structure the team wants to maintain.

Normalize names, remove unnecessary values, add variables and modules where appropriate, and document intentional defaults before the imported resource becomes part of routine automation. The goal is readable desired-state code, not a mechanical transcript of provider state.

Compare the cleaned configuration with a fresh plan after every refactor. That proves that readability improvements did not accidentally change the managed infrastructure during the migration.

Preserve service continuity during migration

Import projects often occur on infrastructure that cannot be taken down simply to make the code cleaner. Plan migrations so read-only discovery, configuration writing, import, and reconciliation can happen without changing the live service until the team is ready for an intentional apply.

Freeze or tightly coordinate manual changes during the final reconciliation window. If operators continue editing the resource while the migration team is matching configuration to state, the target moves and the first managed plan becomes harder to trust.

After cutover, communicate the new change path clearly. The migration is incomplete if administrators continue to use the console because they do not know that Terraform is now authoritative.

Record the migration outcome

Finish each import batch with a short migration record: resources imported, addresses used, configuration source, notable reconciliations, state backup location, validation result, and any remaining manual dependencies. This history is valuable when a later plan proposes a surprising change.

A clean post-import plan should be captured as evidence of the new baseline. Future drift can then be compared with a known point where configuration, state, and the provider agreed about the managed object.

Related Posts

• Databricks Lakehouse Engineering

• Microsoft AI-103: Cost Control for Azure AI Apps

• Microsoft AI-103: Vector Search Design on Azure

• Microsoft AB-100: Securing GitHub Copilot in Enterprises

• Microsoft SC-500: Protecting Copilot Data with Purview

• CompTIA CS0-003: Detection Engineering from Rule to Signal

• Anthropic CCAO-F: Scaling Claude Across an Enterprise

• Microsoft AZ-104: Hybrid Identity for Azure Admins

• CompTIA SY0-701: Risk Registers That Drive Action

• Cisco 200-301: Wireless LAN Controllers