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.
Create the test Azure resources
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.
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 VM 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 VM, 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.