How to Import an Azure azapi_resource into Terraform
terraback azure import --method bulk. Terraback writes the matching azapi_resource resource block and the Terraform 1.5+ import block for you, so you do not have to run terraform import by hand. The import ID is the full ARM resource ID followed by the API version as a query string (<arm-id>?api-version=<version>).Import Azure azapi_resource with Terraback (recommended)
Terraback reverse-engineers your live infrastructure: it reads the Azure Resource Manager (azapi) resource with read-only credentials, generates the HCL, and produces the exact import block. Two commands take you from a live Azure azapi_resource to managed Terraform.
Scan your Azure account
terraback scan all azure --subscription-id YOUR_IDGenerate import blocks and import into state
terraback azure import --method bulkThe Terraform import block
Terraback emits a Terraform 1.5+ import block like the one below. Because the block lives in your configuration, the import is reviewable in a pull request and repeatable across environments.
import {
to = azapi_resource.main
id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/prod-rg/providers/Microsoft.HybridCompute/machines/prod-arc-machine?api-version=2023-10-03-preview"
}Example azapi_resource configuration
Here is a realistic Azure azapi_resource block. Terraback generates a fuller version from your actual resource attributes; this is a minimal, valid starting point.
resource "azapi_resource" "main" {
type = "Microsoft.HybridCompute/machines@2023-10-03-preview"
name = "prod-arc-machine"
parent_id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/prod-rg"
location = "eastus"
body = {
properties = {}
}
schema_validation_enabled = false
}Gotchas when importing a Azure azapi_resource
- Unlike every other azurerm resource, the azapi import ID must append the API version as a query string: <arm-id>?api-version=2023-10-03-preview. Omitting it makes the import fail.
- type combines the ARM resource type and API version with an @, for example Microsoft.HybridCompute/machines@2023-10-03-preview; it must match the api-version used in the import ID.
- parent_id is the containing scope (the resource group, subscription, or parent resource ID), not the resource's own ID.
- Terraback emits lifecycle { ignore_changes = [body] } and schema_validation_enabled = false because azapi round-trips the raw ARM body, which can otherwise produce noisy diffs.
Doing it manually with terraform import
The native approach is to write the azapi_resource block by hand, then run terraform import azapi_resource.example <import-id> for every resource, one at a time. That works for a handful of resources, but it does not scale: you author all the HCL yourself and repeat the command for each item. Terraback generates the HCL and the import blocks for your whole account in one pass.
Import other Azure resources
Import your whole Azure account in minutes
Terraback scans 80+ Azure resource types and emits clean Terraform plus import blocks, running locally with read-only credentials. $499 once, no SaaS.