A practical brownfield approach is:
- Export in a way that preserves safety first.
-
aztfexportis the right tool for existing Azure resources that are not yet under Terraform management. - Since the current workflow uses
--hcl-only, the generated HCL can be cleaned up before state import. - After refactoring, run
terraform planto confirm no drift before making changes.
-
- Refactor the generated code into logical files incrementally.
- Splitting into
compute.tf,networking.tf,storage.tf,recovery.tf, andmonitoring.tfis a reasonable organization pattern. - Keep provider and Terraform blocks consistent while reorganizing files.
- If exporting into a directory that already contains Terraform files, Azure Export for Terraform supports
--appendto add resources into an existing environment and avoids filename conflicts by generating files with a.aztfexportsuffix.
- Splitting into
- Handle unsupported or side-effect resources deliberately.
- Some Azure resources have no corresponding Terraform resources.
- Some resources are created as side effects of other resources, such as an OS disk created when provisioning a virtual machine.
- In interactive mode, those resources can be skipped before writing configuration and importing state.
- Import state only after the configuration is in the desired structure.
- The goal of getting to a clean
terraform planbefore future changes is aligned with the documented workflow: export HCL, inspect the resources, and integrate them into production infrastructure and remote backends. - For AzAPI-managed resources, existing resources can be brought under management with import blocks in Terraform 1.5+ or with
terraform import. The resource ID must include the API version as a query parameter. - If many resources must be imported at once,
aztfexportcan generate both HCL and import blocks automatically.
- The goal of getting to a clean
- Use portal export selectively where it helps.
- Azure portal export can generate Terraform configuration and state directly from a resource.
- In the portal flow, the output can target either AzureRM or AzAPI, then the generated configuration and state can be downloaded and validated with
terraform plan. - Portal export has limits: up to 1,000 resources and a maximum exported configuration payload of 4 MB, and some resource types or properties may not export.
- For large estates, work by scope.
-
aztfexportsupports exporting a single resource, a resource group and nested resources, or an entire subscription. - For 25+ resource groups, a manageable pattern is to process one resource group or one logical scope at a time, refactor that scope, validate with
terraform plan, then move to the next scope.
-
- Use the VS Code extension where it reduces manual effort.
- The Microsoft Terraform VS Code extension supports Azure resource export via
aztfexportand provides authoring help for AzureRM and AzAPI, including resource type discovery, property/value autocompletion, hover hints, syntax validation, and code samples. - That makes it useful during refactoring even when the initial export is already complete.
- The Microsoft Terraform VS Code extension supports Azure resource export via
For the Copilot part, the available material does not provide guidance on using Microsoft 365 Copilot to refactor Terraform codebases or on handling Terraform code confidentiality with Copilot. Based on the documented tooling, the supported path is to use aztfexport, the Azure portal export experience, and the Microsoft Terraform VS Code extension for export, migration, and authoring assistance.
- Migration paths between Azure, AzureRM, and AzAPI Terraform providers
- Overview of Azure Export for Terraform CLI
- How Azure Export for Terraform Works
- Using Azure Export for Terraform in advanced scenarios
- Overview of the Terraform AzAPI provider
- Migration paths between Azure, AzureRM, and AzAPI Terraform providers
- Overview of the Terraform AzAPI provider