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 a free account before you begin.

Sample script


# Create a resource group
az group create -n myResourceGroup -l westus

# Create the storage account to upload the vhd
az storage account create -g myResourceGroup -n mystorageaccount -l westus --sku PREMIUM_LRS

# Get a storage key for the storage account
STORAGE_KEY=$(az storage account keys list -g myResourceGroup -n mystorageaccount --query "[?keyName=='key1'] | [0].value" -o tsv)

# Create the container for the vhd
az storage container create -n vhds --account-name mystorageaccount --account-key ${STORAGE_KEY}

# Upload the vhd to a blob
az storage blob upload -c vhds -f ~/sample.vhd -n sample.vhd --account-name mystorageaccount --account-key ${STORAGE_KEY}

# Create the vm from the vhd
az vm create -g myResourceGroup -n myVM --image "" \
        --os-type linux --admin-username deploy --generate-ssh-keys

# Update the deploy user with your ssh key
az vm user update --resource-group myResourceGroup -n custom-vm -u deploy --ssh-key-value "$(< ~/.ssh/"

# Get public IP address for the VM
IP_ADDRESS=$(az vm list-ip-addresses -g az-cli-vhd -n custom-vm \
    --query "[0][0].ipAddress" -o tsv)

echo "You can now connect using 'ssh deploy@${IP_ADDRESS}'"

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.