Quickstart: Export your first resources using Azure Export for Terraform
This article shows how to export Azure resources into local state files using Azure Export for Terraform.
- 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 state for the resource group and virtual machine from Azure to the local state file.
- 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
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 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 myResourceGroup
After the tool initializes, a list of the resources to be exported is displayed. Each line has an Azure resourceID matched to the corresponding AzureRM resource type. The list of available commands displays at the bottom of the display. Using one of the commands, scroll to the bottom and verify that the expected Azure resources are properly mapped to their respective Terraform resource types.
Press
w
to run the export.Key points:
- For a non-interactive resource, add the
--non-interactive
flag:aztfexport rg --non-interactive myResourceGroup
.
- For a non-interactive resource, add the
Note
Running Azure Export for Terraform can take several minutes to complete.
Verify the results
After the tool has finished exporting your Azure resources, run the following commands in the same directory that contains the generated files.
Run terraform init.
terraform init --upgrade
Run terraform plan.
terraform plan
If the terminal outputs No changes needed, then congratulations!
Your infrastructure and its corresponding state have been successfully exported to Terraform.
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
Next steps
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for