Azure Virtual Machines CLI Documentation

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:

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:

Note: For Windows VMs, you would typically use --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.

Pro Tip: Use the --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:

Explore the full range of options by running az vm create --help.

Security Alert: Avoid hardcoding passwords or sensitive information directly in your scripts. Use Azure Key Vault or environment variables for better security practices.

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.