Creating Virtual Machines with Azure CLI
This document provides a comprehensive guide on how to create and configure Azure Virtual Machines (VMs) using the Azure Command-Line Interface (CLI).
Prerequisites
Before you begin, ensure you have the following:
- An Azure subscription. If you don't have one, you can create a free account.
- Azure CLI installed. You can download it from the official Azure CLI documentation.
- Signed in to your Azure account using
az login.
Creating a Basic VM
The simplest way to create a VM is by using the az vm create command. This command creates a VM with default settings for a specified operating system image.
Example: Creating a Ubuntu VM
az vm create \
--resource-group MyResourceGroup \
--name MyVM \
--image UbuntuLTS \
--admin-username azureuser \
--generate-ssh-keys
Explanation:
--resource-group: Specifies the resource group where the VM will be created.--name: The name of your virtual machine.--image: The OS image to use (e.g.,UbuntuLTS,Win2019Datacenter). You can list available images withaz vm image list --output table.--admin-username: The administrator username for the VM.--generate-ssh-keys: Automatically generates SSH public and private key files if they don't exist.
--admin-password instead of SSH keys.
Customizing VM Creation
The az vm create command offers numerous options to customize your VM:
Specifying VM Size
You can choose from a wide range of VM sizes, each with different CPU, memory, and network performance characteristics. Use az vm list-sizes --location eastus --output table to see available sizes in a region.
Example: Creating a VM with a specific size
az vm create \
--resource-group MyResourceGroup \
--name MySizedVM \
--image Win2019Datacenter \
--size Standard_DS2_v2 \
--admin-username azureuser \
--admin-password 'YourSecurePassword123!'
Selecting a Region and Availability Zone
You can specify the Azure region and availability zone for your VM.
Example: Creating a VM in a specific region and zone
az vm create \
--resource-group MyResourceGroup \
--name MyZonedVM \
--image CentOS \
--location westus2 \
--zones 1 \
--admin-username azureuser \
--generate-ssh-keys
Configuring Networking
By default, a virtual network and public IP address are created. You can specify existing networks or configure advanced networking options.
Example: Attaching to an existing virtual network
az vm create \
--resource-group MyResourceGroup \
--name MyNetworkedVM \
--image Ubuntu2004 \
--vnet-name MyExistingVnet \
--subnet MyExistingSubnet \
--public-ip-address MyExistingPublicIP \
--admin-username azureuser \
--generate-ssh-keys
Working with Disks
By default, Azure CLI creates a temporary disk and an OS disk for your VM. You can also attach additional data disks.
Example: Creating a VM with a managed data disk
az vm create \
--resource-group MyResourceGroup \
--name MyVMWithDataDisk \
--image Debian \
--data-disk-sizes-gb 100 \
--admin-username azureuser \
--generate-ssh-keys
For more advanced disk configurations, refer to the Storage section.
--output table or --output json options with many Azure CLI commands to easily parse and read the output.
Advanced VM Creation Options
The Azure CLI supports a wide array of options for creating VMs, including:
- Custom Images: Create VMs from your own custom OS images.
- Scale Sets: Deploy and manage groups of identical VMs.
- Extensions: Install custom scripts, agents, or software on your VM.
- Proximity Placement Groups: For low-latency workloads.
- Host Groups: For dedicated hosts.
Explore the full range of options by running az vm create --help.
Next Steps
Once your VM is created, you can connect to it using SSH (for Linux) or RDP (for Windows). Learn more about managing your VMs in the Managing VMs section.