terraform import command. It covers the required arguments, Azure-specific ID formats, example usage, a recommended import workflow, and best practices to avoid surprises after importing resources.
What terraform import does (and does not do)
terraform importconnects an existing real resource to a declared resource block in your Terraform state.- It does not generate HCL configuration, nor does it create, modify, or destroy the remote resource.
- After a successful import, the resource’s attributes are recorded in the Terraform state and Terraform will manage it going forward.
Required arguments
The import command expects two positional arguments:- ADDR — the resource address in your Terraform configuration (must already exist in your
.tffiles). Example:azurerm_storage_account.example. - ID — the provider-specific identifier of the real resource (format depends on provider).
ADDR vs ID — quick comparison
For Azure resources the ID is typically the full Azure Resource Manager (ARM) ID shown above.
Example: import an Azure Storage Account
Run the import using the HCL resource address and the ARM resource ID:Trimmed help text (reference)
Recommended import workflow (step-by-step)
- Ensure the Terraform configuration contains a resource block that matches the resource type you intend to import.
terraform importdoes not create this block for you. - Confirm the correct provider-specific ID format from provider docs (for Azure, use the ARM resource ID).
- Run
terraform import ADDR ID. - Immediately run
terraform planto detect any differences (drift) between the imported state and your HCL. - Reconcile your HCL: update default values, add missing attributes, or change values to reflect the desired configuration.
- Run
terraform planagain until the plan shows only intended changes, thenterraform applyto persist any HCL-driven changes (if required).
Note:
terraform import is especially useful when adopting Terraform in a brownfield environment, recovering a lost state file, or migrating manually created resources into infrastructure-as-code without causing downtime or recreation.Best practices and important clarifications
- The resource block must exist in the configuration before importing. Terraform will not generate HCL for you.
- After import, the imported resource’s state attributes may differ from the configuration; always run
terraform planand reconcile differences. - Import only affects the state file; it does not change configuration files or the remote resource.
- Use provider documentation to determine the correct ID format for imports. For Azure, this is usually the ARM resource ID.
- Consider using separate branches and state backups when importing multiple resources to reduce risk.
Tools and further resources
- Azure: Microsoft maintains a tool called
aztfexportwhich can help convert existing Azure infrastructure into HCL and assist in generating Terraform configuration for larger migrations: https://github.com/Azure/aztfexport - Terraform docs: https://www.terraform.io/docs/cli/commands/import.html
- Azure Resource Manager ID docs: https://learn.microsoft.com/azure/azure-resource-manager/management/resource-name-rules
Note: After importing, reconcile your configuration with the imported state and run
terraform plan and terraform apply only when you have verified the intended changes.terraform import. It is the primary CLI command for bringing existing resources into Terraform state when adopting infrastructure-as-code in environments that already have resources provisioned.