Launch and manage Azure VMs: pick a VM size and image, attach managed disks, open ports, give a VM a fixed public IP, configure it at boot with cloud-init, and cut costs with Spot and reserved pricing.
A Virtual Machine is a server you rent by the second. Why: it is the general-purpose workhorse — you launch one, it boots in a minute, and you pay while it runs. Everything below assumes the resource group, VNet, and subnets from earlier lessons.
List the regions available to you (VMs are created in a region)
az account list-locations --query '[].name' --output tsv | headA VM "size" (e.g. Standard_B1s, Standard_D2s_v5) sets CPU, memory, and disk performance. The letter is the family: B = burstable/cheap, D = general purpose, F = compute, E = memory. Why: match the workload — a small site fits a B-series; a database wants an E-series. Bigger = more capable and pricier.
List sizes available in a region with their vCPUs and memory
az vm list-sizes --location eastus \
--query '[].{size:name, vCPUs:numberOfCores, memoryMB:memoryInMb}' \
--output table | headAn image is the operating system (and any pre-installed software) a new VM copies onto its disk. Why: it decides what your server starts as — Ubuntu, Debian, Windows Server, or your own captured image. Azure ships short "aliases" for common ones.
List the popular image aliases (UbuntuLTS, Debian, Win2022, ...)
az vm image list --output tableSearch the full catalog for Ubuntu images
az vm image list --publisher Canonical --all --output table | headCombine an image, a size, and a subnet. Why SSH keys: az can generate a key pair so you log in without a password; you keep the private half. This single command creates a real, billable VM (and a NIC and public IP for it).
Create an Ubuntu VM in the public subnet, generating SSH keys
az vm create \
--resource-group learn-rg \
--name my-vm \
--image Ubuntu2204 \
--size Standard_B1s \
--vnet-name my-vnet --subnet public \
--public-ip-address web-ip \
--admin-username azureuser \
--generate-ssh-keysSSH in using the public IP from the output ssh azureuser@<public-ip>
A new VM is locked down. az vm open-port adds an NSG rule to allow a port. Why: this is the quick way to expose, say, HTTP (80) for testing — though for real workloads you manage NSG rules explicitly as in the networking lesson.
Allow HTTP to the VM for a quick test
az vm open-port --resource-group learn-rg --name my-vm \
--port 80 --priority 300cloud-init runs a script the first time a Linux VM boots — the standard way to install software automatically. Why: instead of SSHing in to set up each VM by hand, you bake the setup into creation so VMs come up ready to serve.
Save this as cloud-init.yaml — installs and starts nginx on first boot
#cloud-config
package_update: true
packages:
- nginx
runcmd:
- systemctl enable --now nginxPass it at create time with --custom-data
az vm create --resource-group learn-rg --name web-vm \
--image Ubuntu2204 --size Standard_B1s \
--vnet-name my-vnet --subnet public \
--admin-username azureuser --generate-ssh-keys \
--custom-data cloud-init.yamlA managed disk is durable storage Azure manages for you; it survives reboots and can be detached and reattached. Why types matter: Standard HDD is cheapest, Premium SSD is fast, and you size for performance. Snapshots give you point-in-time backups.
Create and attach a 32 GB Premium SSD data disk
az vm disk attach --resource-group learn-rg --vm-name my-vm \
--name data-disk --new --size-gb 32 --sku Premium_LRSSnapshot the OS disk for backup
DISK_ID=$(az vm show --resource-group learn-rg --name my-vm \
--query 'storageProfile.osDisk.managedDisk.id' --output tsv)az snapshot create --resource-group learn-rg \
--name my-vm-snapshot --source $DISK_IDHow you pay changes the price a lot. Pay-as-you-go: per-second, no commitment (default). Spot: spare capacity up to ~90% cheaper, but Azure can evict it with little notice (great for batch jobs). Reserved Instances / Savings Plans: commit to 1–3 years for a big discount on steady workloads.
Create a Spot VM — same command plus a priority + eviction policy
az vm create --resource-group learn-rg --name batch-vm \
--image Ubuntu2204 --size Standard_B1s \
--vnet-name my-vnet --subnet public \
--admin-username azureuser --generate-ssh-keys \
--priority Spot --eviction-policy Deallocate --max-price -1