Azure Infrastructure to Terraform
Manually importing Azure resources into Terraform one by one is slow. AZTFExport automates the whole thing.
You have a Terraform project, but it’s completely out of sync with your actual infrastructure. Resources have been added, removed, or modified, and now you need to update the code. But how do you update your Terraform configuration as quick as possible?
The same challenge applies when starting a project. Maybe IaC wasn’t in scope initially, but now it’s becoming necessary. How do you integrate Terraform into an existing environment easily?
In this post, we’ll go over strategies to efficiently update Terraform code, handle drift, and introduce IaC into projects that didn’t start with it.
The Standard (Slow) Approach
Your first instinct might be to use Terraforms built-in approach for defining and importing resources. Here’s what that process typically looks like:
1. Manually Define Resources in Code
First, you’d write the Terraform configuration for each existing resource. For example, an Azure Storage Account:
resource "azurerm_storage_account" "example" {
name = "mystorageaccount"
resource_group_name = "my-resource-group"
location = "westeurope"
account_tier = "Standard"
account_replication_type = "LRS"
}
At this stage, Terraform wouldn’t know the resource already exists in Azure.
2. Manually Import Each Resource
Next, you’d import the resource using its full Azure resource ID:
terraform import azurerm_storage_account.example /subscriptions/XXXX/resourceGroups/my-resource-group/providers/Microsoft.Storage/storageAccounts/mystorageaccount
This means manually looking up the resource ID using the Azure CLI or the Azure portal. And yes, you’d have to do this for every single resource.
3. Run terraform plan and Fix Differences
After importing, you’d check for mismatches between your code and the actual infrastructure:
terraform plan
If Terraform detected differences, you’d need to change the .tf file to prevent unnecessary changes.
4. Repeat for Every Resource (Not Ideal)
Since Terraform doesn’t support bulk imports, you’d have to do this for each resource. For large infrastructures, this can quickly become a slow and painful process.
A Better Way: AZTFExport
Instead of manually defining and importing resources one by one, you can use AZTFExport. This tool automates the process of exporting existing Azure resources into Terraform code.
Why AZTFExport?
AZTFExport scans your Azure environment, detects deployed resources, and generates Terraform .tf files along with a state file (terraform.tfstate). This means:
- No need to manually write Terraform code for existing resources.
- No need to manually import resources.
- No need to lookup resource IDs manually.
Installing AZTFExport
You can install AZTFExport easily using winget (or check other installation methods at GitHub):
winget install aztfexport
Running AZTFExport
To demonstrate the process I’ve created a simple setup with one storage account, and one managed identity that has the Blob storage Reader role on the storage account:

Now, we will use AZTFexport to automatically generate Terraform code. Use following commands:
az login # select the subscription where your rg is
aztfexport rg blog-terraform
I’m using interactive mode here, but you can also use the non-interactive mode, by adding --non-interactive after rg.
⚠️ You may get an error, which could be due to ARM variables being set. You can clear them in PowerShell using the following command:
Get-ChildItem env:ARM_* | ForEach-Object { Remove-Item -Path "env:$($_.Name)" }
Once AZTFExport finishes, it prompts you to save the configuration. Press W to save. It will:
- Import all resources into the Terraform state.
- Generate the .tf files.


Let’s open up main:

We can now see our resource group, identity, and storage account, but not the role assignment. This is because, by default, AZTFexport does not export role assignments for some reason.
You have to explicitly enable it (you can use the option --append or --overwrite if you want to keep your previous configuration or overwrite it):
aztfexport rg --include-role-assignment --overwrite blog-terraform
And now, we can see the role assignment as well:

Final thoughts
Using the AZTFExport tool definitely speeds up the Terraform configuration process compared to manually importing resources. However, there are still some limitations:
- Links between resources are hardcoded. For example, role assignments have IDs and scopes hardcoded instead of using references to other resources.
- All resources are named using the default format res-x, which isn’t very practical. A more meaningful naming convention would be useful.
At the time of writing, the tool is at version 0.17.1, meaning further improvements are likely in future releases.