Quickstart: Export Azure resources into HCL code using Azure Export for Terraform
In the article, Export your first resources using Azure Export for Terraform, you learn how to export Azure resources into local state files using Azure Export for Terraform. In this article, you learn how to generate the Terraform configuration files from your Azure resources.
- Create a test Azure resource group using Azure CLI or Azure PowerShell.
- Create a test Linux virtual machine using Azure CLI or Azure PowerShell.
- Export the resource group and virtual machine from Azure to HCL files.
- Test that the local state matches the state of the resources in Azure.
Prerequisites
Create the test Azure resources
Create a Linux VM.
Run az group create to create an Azure resource group.
az group create --name myResourceGroup --location eastus
Run az vm create to create the virtual machine.
az vm create \ --resource-group myResourceGroup \ --name myVM \ --image Debian11 \ --admin-username azureadmin \ --generate-ssh-keys \ --public-ip-sku Standard
Understand the hcl-only flag
Azure Export for Terraform supports a flag - --hcl-only
- that causes the generation of the following files from the exported resource(s):
- Generated
.tf
HCL files. - Mapping file
aztfexportResourceMapping.json
. - Skipped resources are listed in
aztfexportSkippedResources.txt
.
The --hcl-only
flag is supported for all primary export commands used for exporting:
- resource
- resource-group
- query
- mapping-file
To view the available Azure Export for Terraform commands, run the following command:
aztfexport --help
The --hcl-only
flag is useful in scenarios where you don't need the state or aren't sure if you need to generate the state. To export all the generated configuration to state, run aztfexport mapping-file
.
Tip
When using the --hcl-only
flag, target an empty directory to avoid making unwanted changes to any current state during the export stage.
Export an Azure resource
You can run the aztfexport
tool in one of two modes: interactive and non-interactive. For this demo, you use the non-interactive mode.
Create a directory in which to test.
Open a command prompt and navigate to the new directory.
Run
aztfexport resource-group
to export the resource group namedmyResourceGroup
.aztfexport resource-group --non-interactive --hcl-only myResourceGroup
Note
Running Azure Export for Terraform can take several minutes to complete.
Verify the results
After the tool has finished exporting your Azure resources, verify the following files in the directory where you ran Azure Export for Terraform:
main.tf
contains the HCL code that defines the exported resources.aztfexportResourceMapping.json
contains the Azure/Terraform mappings. The mapping file includes the following information for each exported Azure resource: Azure resource ID, Terraform resource type, and Terraform resource name. The contents of the mapping file mirror what Azure Export for Terraform displays during the export process.aztfexportSkippedResources.txt
contains the list of skipped resources. You shouldn't see this file for this example.
Clean up resources
When you no longer need the resources created in this article, do the following steps:
Navigate to the directory containing your Terraform files for this article.
Run terraform destroy.
terraform destroy