Skip to content

Importing existing resources

How to bring resources that already exist in Azure under management, so the repository owns them without recreating them.

You need this in three situations:

  • Brownfield onboarding. The customer already has management groups, policy assignments, subscriptions or resource groups, and the landing zone should adopt them rather than build next to them.
  • Adopting a resource made outside the repository. Someone created it in the portal or with a script, and it should stay. Drift detection never sees such a resource, because no unit manages it. A change to a resource a unit already manages is drift, not an import; see Drift detection.
  • Recovery. A resource exists in Azure but not in state, for example after an apply that failed half way. See A half-failed apply.

An import is a pull request like any other change. You add an import block to the unit's main.tf, the plan shows the import, a reviewer approves it, and tfpr apply writes the resource into state. There is no import command and no local state change.

Procedure

Three pull requests: write the code, import, clean up.

  1. Write the code for the resource, without an import. Add the module or resource to the unit's main.tf with the values the existing resource already has: its name, location and tags. Open the pull request and let it plan, but do not apply. The plan shows the resource being created, and the line above each create is the address you import into:

    # module.avm-res-resources-resourcegroup.azapi_resource.this will be created
    

    That address is the one reliable way to find the target. Many Azure Verified Modules manage a resource through azapi_resource, not the azurerm type you would expect, and the name inside the module is rarely obvious.

  2. Add the import block in the same pull request. Point it at that address and at the resource's Azure ID:

    import {
      to = module.avm-res-resources-resourcegroup.azapi_resource.this
      id = "/subscriptions/${local.context.subscription_id}/resourceGroups/rg-app-prod-weu"
    }
    

    az resource show --ids and az <service> show --query id print the ID. Build it from local.context where you can, so the block does not hard-code a subscription.

  3. Read the plan. The report heading and the per-unit check row count the import, for example 1 to import, 0 to add, 1 to change, 0 to destroy, and the plan output marks each resource (imported from "...").

    The goal is an import with nothing else. Any create or destroy for the imported resource means the address or the ID is wrong. Any change to a real property, such as a tag, a SKU or a location, means the code does not match the resource yet: fix the code, push, and read the plan again. Apply only when nothing real changes.

  4. Apply and merge. Get the required review, comment tfpr apply, and merge once tfpr / merge-gate is green. The apply report reads 1 imported, 0 added, ....

  5. Remove the import block in a follow-up pull request. Once applied, the block does nothing, and leaving it means every later plan checks it again. Delete it in a small pull request. Its plan must say No changes; if it does not, the import did not land and the resource is not in state.

A one-time change after an azapi import

A resource imported into azapi_resource usually plans as one import plus one in-place update, even when the code matches. The update only sets the provider's own fields, such as response_export_values, retry and timeouts, and a managedBy = null in the body. Azure sees no difference. Check that the diff touches nothing else, then apply. The follow-up pull request in step 5 plans as No changes.

Many resources at once

An import block per resource scales to a whole estate, and one pull request can import into several units. Keep each pull request to one area, such as the management group hierarchy or one landing zone, so the plan stays readable. For many resources of one kind, an import block takes for_each.

Terraform can write the code for imported resources itself (terraform plan -generate-config-out), but only for plain resources, not inside modules, and it needs a local run with access to the state. In this layout, where units use Azure Verified Modules, write the module inputs by hand and use the create addresses from step 1.

What not to do

  • Do not run terraform import, terraform state or terragrunt run -- import from a shell. It changes state with no plan and no review, and the engine never sees it.
  • Do not apply an import whose plan also creates or replaces the resource. A replace on an imported resource destroys the existing one.
  • Do not import a resource into two units. Each resource has one owner.

To stop managing a resource without destroying it, the reverse of an import, see Decommissioning a unit.