Terraform · Azure · Intermediate · 12 min
Fix Terraform resource already exists on Azure
Fix the AzureRM error that says a resource already exists and needs to be imported into the State: import blocks, the drift after import, generated config.

The resource is real and was created outside Terraform, so the AzureRM provider will not create it again. This runbook imports it with import blocks, catches the change the first plan wants to make, and fixes the configuration Terraform generates. Every step ran on a real Azure subscription with Terraform 1.17.2 and AzureRM 4.81.0.
On this page
terraform apply stops with a resource with the ID "..." already exists - to be managed via Terraform this resource needs to be imported into the State. The resource is real, it was created outside Terraform (in the portal, with the CLI, by another pipeline or another state file), and the AzureRM provider refuses to create it a second time. The fix is to import it, so Terraform takes it over instead of creating it, and then to make your code match what is really there.
Everything below was run on 13 November 2026 with Terraform 1.17.2 and the AzureRM provider 4.81.0 against a real Azure subscription. The resource group and storage account were created with the Azure CLI first, then managed with Terraform, then destroyed. Subscription IDs and names are masked.
0. Rules of engagement
- Import, do not delete and recreate. Deleting a resource group to "make room" for Terraform deletes everything in it.
- Read the plan after the import. An import that also says
to changemeans your code differs from the real resource; applying it changes production. - One state per resource. If the resource is already managed by another Terraform state, do not import it into a second one: two states fighting over one resource is worse than the error.
- Work in a branch and keep the old state (
terraform state pull > before.tfstate) until the plan is clean.
1. The error
The resources existed, created by hand with a tag:
# Created outside Terraform, the way most of these resources are born
az group create -n rg-import-demo -l northeurope --tags owner=ops
az storage account create -n stimportdemo -g rg-import-demo -l northeurope --sku Standard_LRS --kind StorageV2 --min-tls-version TLS1_2 --allow-blob-public-access false --tags owner=opsThe Terraform code declared the same resource group and storage account. terraform plan saw no problem (Plan: 2 to add, 0 to change, 0 to destroy.), because the plan does not ask Azure whether a resource with that name already exists. apply did:
# Real output of terraform apply
azurerm_resource_group.lab: Creating...
Error: a resource with the ID "/subscriptions/SUB-ID/resourceGroups/rg-import-demo" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_resource_group" for more information
with azurerm_resource_group.lab,
on main.tf line 1, in resource "azurerm_resource_group" "lab":
1: resource "azurerm_resource_group" "lab" {The error names the exact Azure resource ID and the Terraform address (azurerm_resource_group.lab). You need both.
2. Import with import blocks
Since Terraform 1.5 the import is code: an import block per resource, reviewed in a pull request and applied like any other change. Get the IDs from Azure:
# The resource IDs to import
az group show -n rg-import-demo --query id -o tsv
az storage account show -n stimportdemo -g rg-import-demo --query id -o tsv# imports.tf: map each Terraform address to the real Azure resource ID
import {
to = azurerm_resource_group.lab
id = "/subscriptions/SUB-ID/resourceGroups/rg-import-demo"
}
import {
to = azurerm_storage_account.lab
id = "/subscriptions/SUB-ID/resourceGroups/rg-import-demo/providers/Microsoft.Storage/storageAccounts/stimportdemo"
}Then plan:
# Real output of terraform plan: the import, and a change nobody asked for
# azurerm_resource_group.lab will be updated in-place
# (imported from "/subscriptions/SUB-ID/resourceGroups/rg-import-demo")
~ resource "azurerm_resource_group" "lab" {
id = "/subscriptions/SUB-ID/resourceGroups/rg-import-demo"
location = "northeurope"
managed_by = null
name = "rg-import-demo"
~ tags = {
- "owner" = "ops" -> null
}
}
Plan: 2 to import, 0 to add, 2 to change, 0 to destroy.The import itself worked, but the code has no tags, so applying this would remove the owner tag from both resources. This is the step people skip.
3. Make the code match reality
Add what the plan says is missing, here the tag on both resources, and plan again until it says 0 to change:
# main.tf (excerpt): the tag that already exists in Azure
resource "azurerm_resource_group" "lab" {
name = "rg-import-demo"
location = "northeurope"
tags = { owner = "ops" }
}# Real output: plan, apply, and a plan afterwards
Plan: 2 to import, 0 to add, 0 to change, 0 to destroy.
azurerm_resource_group.lab: Importing... [id=/subscriptions/SUB-ID/resourceGroups/rg-import-demo]
azurerm_resource_group.lab: Import complete [id=/subscriptions/SUB-ID/resourceGroups/rg-import-demo]
azurerm_storage_account.lab: Importing... [id=/subscriptions/SUB-ID/resourceGroups/rg-import-demo/providers/Microsoft.Storage/storageAccounts/stimportdemo]
azurerm_storage_account.lab: Import complete [id=/subscriptions/SUB-ID/resourceGroups/rg-import-demo/providers/Microsoft.Storage/storageAccounts/stimportdemo]
Apply complete! Resources: 2 imported, 0 added, 0 changed, 0 destroyed.
No changes. Your infrastructure matches the configuration.After a successful apply you can delete imports.tf; the resources are in the state now. Keeping it is harmless, since an import block for a resource already in the state does nothing.
4. Let Terraform write the code, then fix it
For a resource with many settings, Terraform can generate the configuration from the real resource:
# Only an import block, no resource block: Terraform writes generated.tf
terraform plan -generate-config-out=generated.tfFor our storage account the generated file had 52 lines and the plan failed on it:
# Real output
Warning: Config generation is experimental
Error: expected blob_properties.0.change_feed_retention_in_days to be in the range (1 - 146000), got 0
with azurerm_storage_account.lab,The generator writes every attribute it reads back, including values the provider will not accept as input. Remove or correct the offending line and plan again:
# The generated value 0 is not valid input; dropping the line keeps the default
grep -n "change_feed_retention_in_days" generated.tf
sed -i '/change_feed_retention_in_days *= *0$/d' generated.tf
terraform plan# Real output after the one-line fix
35: change_feed_retention_in_days = 0
# azurerm_storage_account.lab will be imported
Plan: 1 to import, 0 to add, 0 to change, 0 to destroy.Then move the resource from generated.tf into your own files and trim the attributes that only repeat defaults.
5. The older command: terraform import
Before import blocks, the only way was a CLI command. It still works and changes the state immediately, without a plan:
# Imports one resource straight into the state (no review step)
terraform import azurerm_resource_group.lab "/subscriptions/SUB-ID/resourceGroups/rg-import-demo"# Real output
azurerm_resource_group.lab: Importing from ID "/subscriptions/SUB-ID/resourceGroups/rg-import-demo"...
Import successful!Prefer import blocks in teams: they go through review and CI, and the plan shows the drift before anything is written.
6. Verify
terraform plansaysNo changes. Your infrastructure matches the configuration.terraform state listlists every imported address.- In Azure, nothing changed: compare the tags and settings you care about before and after.
Common mistakes
- Applying the first plan after an import.
2 to changeafter an import usually means "remove what someone set by hand". Fix the code first. - Importing into the wrong address. The
tomust match a resource block (or afor_eachkey such asazurerm_storage_account.lab["logs"]). Otherwise Terraform plans to destroy one and create another. - Importing the same resource into two states. Find out who already manages it (
terraform state listin the other workspace, or tags such asmanaged_by). - Deleting and recreating to get rid of the error. It works for an empty resource group and destroys data everywhere else.
- Wrong case in the ID. Use the ID exactly as
az ... --query idprints it.
7. Rollback
Until you apply, nothing has changed: delete imports.tf. After an apply, terraform state rm ADDRESS removes a resource from the state without deleting it in Azure, which returns you to the starting point. Our test ended with terraform destroy (Destroy complete! Resources: 2 destroyed.) because the resources were ours to delete.
Related guides
- Run Terraform init, validate, plan and apply safely: reading a plan before you apply it.
- Investigate an unexpected Terraform resource replacement: when the plan wants to destroy and recreate instead.
- Protect Terraform state, variables, modules and secrets: where the state lives and who can change it.
References
- Terraform, import blocks: https://developer.hashicorp.com/terraform/language/import
- Terraform, generating configuration: https://developer.hashicorp.com/terraform/language/import/generating-configuration
- AzureRM provider, resource group import: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group#import
Official reference
Technical steps are original explanations; consult the upstream documentation for version-specific details.
Behavior differs by distribution and tool version. Commands are examples for authorized systems; review output for secrets before sharing.
Found this useful?
Follow us on YouTube for video explainers, and find us on Facebook to keep up with what is new.