Share via


Create a VM with a virtual hard disk

This example creates a virtual machine using a VHD. It creates a resource group, a storage account, and a container, then it creates a VM by uploading the VHD to the container. It replaces the ssh public key with your public key so that you have access to the VM.

You'll need a bootable VHD. The script looks for ~/sample.vhd.

To run this sample, install the latest version of the Azure CLI. To start, run az login to create a connection with Azure.

Samples for the Azure CLI are written for the bash shell. To run this sample in Windows PowerShell or Command Prompt, you may need to change elements of the script.

If you don't have an Azure subscription, create an Azure free account before you begin.

Clean up deployment

Run the following command to remove the resource group, VM, and all related resources.

az group delete -n az-cli-vhd

Script explanation

This script uses the following commands to create a resource group, virtual machine, availability set, load balancer, and all related resources. Each command in the table links to command specific documentation.

Command Notes
az group create Creates a resource group in which all resources are stored.
az storage account list Lists storage accounts
az storage account check-name Checks that a storage account name is valid and that it doesn't already exist
az storage account keys list Lists keys for the storage accounts
az storage blob exists Checks whether the blob exists
az storage container create Creates a container in a storage account.
az storage blob upload Creates a blob in the container by uploading the VHD.
az vm list Used with --query check whether the VM name is in use.
az vm create Creates the virtual machines.
az vm list-ip-addresses Gets the IP address of the VM that was created.

Next steps

For more information on the Azure CLI, see Azure CLI documentation.

Additional virtual machine CLI script samples can be found in the Azure Linux VM documentation.