Quickstart: Export a virtual machine in the Azure portal
Applies to: ✔️ Any management plane resources from the AzureRM or AzAPI provider.
Existing Azure resources can be exported to Terraform through the Azure portal. This quickstart shows you how to use the Azure portal to export a resource group.
- Create a test Azure resource group using Azure CLI or Azure PowerShell.
- Create a test Linux virtual machine using Azure CLIor Azure PowerShell.
- Export the state for the resource group and virtual machine from Azure to Terraform.
- Test that the local state matches the state of the resources in Azure.
Prerequisites
Sign in to Azure
Sign in to the Azure portal with the experimental Terraform feature flag enabled.
Setup Virtual Machine
Azure CLI or Azure PowerShell
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
Azure portal
- Under Azure Services, select Virtual machines. If you don't see Virtual machines, search for it in the search bar.
- In the Virtual machines page, select Create, to see a dropdown. Select Azure virtual machine.
- Under Virtual Machine Name, type in myVM.
- Under Resource Group Name, select Create new, and type in myResourceGroup.
- Leave everything else as default. Select Review + create.
- Verify everything is configured properly, then select Create.
Register resource provider
Azure CLI or Azure PowerShell
- Run the command
az provider register -n Microsoft.AzureTerraform
- Register the feature flag:
az feature register --namespace Microsoft.AzureTerraform -n private
. This is a private preview feature thus you need to wait for manual approval from Microsoft internal team to use the feature. Check the status of your feature registration by runningaz feature show --namespace Microsoft.AzureTerraform --name private
.
Export resource group
Export the existing resource group to Terraform.
On the overview page for your resource group, expand the Automation tab, and select Export Template.
In the Export Template page, select Terraform.
Select either the
AzureRM
orAzAPI
provider. Code can also be generated for both providers at once for side by side comparison.Review the generated code.
Use the copy template button to paste directly into your code editor. Alternatively, use the download button to get a ZIP folder with the terraform configuration file.
Clean up resources
Delete resources
When no longer needed, you can delete the resource group, virtual machine, and all related resources.
- At the top of the page for the resource group, select Delete resource group.
- A page opens warning you that you're about to delete resources. Type the name of the resource group and select Delete to finish deleting the resources and the resource group.
Auto-shutdown
If the virtual machine is still needed, Azure provides an Auto-shutdown feature for virtual machines to help manage costs and ensure you're not billed for unused resources.
- On the Operations section for the virtual machine, select the Auto shutdown option.
- A page opens where you can configure the auto shutdown time. Select the On option to enable and then set a time that works for you.
- Once you set the time, select Save at the top to enable your Auto-shutdown configuration.
Note
Remember to configure the time zone correctly to match your requirements, as (UTC) Coordinated Universal Time is the default setting in the Time zone dropdown.
For more information, see Auto shutdown.
Next steps
In this quickstart, you deployed a simple virtual machine and exported the configuration to Terraform code.