Stage 5 · Platform
Compute & Containers
Azure Virtual Machines
VM sizes, managed disks, availability sets, scale sets, and VM extensions.
Virtual Machines Overview
Azure Virtual Machines are Infrastructure-as-a-Service (IaaS) compute resources. You get full control over the operating system, networking, and storage. VMs are the foundation for workloads that cannot run on PaaS or containers.
az vm create \
--resource-group rg-webapp \
--name vm-web-01 \
--image Ubuntu2204 \
--size Standard_D2s_v3 \
--admin-username azureuser \
--ssh-key-values ~/.ssh/id_rsa.pub \
--tags Environment=production Team=webThe --ssh-key-values flag uploads your public key for SSH access. Never use --password for production VMs.
VM Sizes and Families
Azure offers hundreds of VM sizes organized into families, each optimized for different workloads. Choosing the right family can save 50% or more compared to a generic size.
| Family | Optimized For | Example Sizes |
|---|---|---|
| Dv5/Ev5 | General purpose workloads | D2s_v5, D4s_v5, E4s_v5 |
| Fsv2 | Batch processing, gaming | F2s_v2, F4s_v2, F8s_v2 |
| Mv2 | In-memory databases | M8ms, M16ms, M32ms |
| Nv4/Npv4 | GPU workloads, ML inference | NC4as_T4_v3, ND96asr_v4 |
| B-series | Dev/test, burstable | B1s, B2ms, B4ms |
# List all available sizes in a region
az vm list-sizes --location eastus --query "[].{name:name, vcpus:numberOfCores, memoryMB:memoryInMb}" --output table
# List sizes that support premium storage
az vm list-sizes --location eastus --query "[?premiumIo==`true`].{name:name, vcpus:numberOfCores}" --output tableNot all VM sizes are available in all regions. Always verify availability before designing around a specific size.
B-series VMs use burstable CPU credits — they run at baseline and burst when needed. They cost 50-70% less than equivalent D-series VMs and are ideal for dev/test environments.
Managed Disks
Managed Disks are block-level storage volumes attached to VMs. Azure manages the underlying storage account — you just specify the size and type. Premium SSD v2 and Ultra Disks offer the lowest latency for I/O-intensive workloads.
# Create and attach a Premium SSD data disk
az vm disk attach \
--resource-group rg-webapp \
--vm-name vm-web-01 \
--name disk-web-data \
--size-gb 128 \
--sku Premium_LRS \
--lun 0
# List disks attached to a VM
az vm show \
--resource-group rg-webapp \
--name vm-web-01 \
--query "storageProfile.dataDisks[].{name:name, sizeGB:diskSizeGb, caching:caching}" \
--output tableManaged Disks are billed per GB/month plus per-transaction costs. Premium SSDs provide single-digit millisecond latency for production workloads.
Availability Sets and Zones
Availability distributes VMs across failure domains (racks) and update domains. Availability Sets protect against hardware failures within a datacenter. Availability Zones distribute VMs across physically separate datacenters within a region.
| Feature | Availability Set | Availability Zone |
|---|---|---|
| SLA | 99.95% | 99.99% |
| Failure domain | Rack within a datacenter | Separate datacenter |
| Network latency | < 1ms (same DC) | 1-2ms (same region) |
| Cost | Free | Free |
# Create VMs across two zones for high availability
az vm create \
--resource-group rg-webapp \
--name vm-web-az1 \
--image Ubuntu2204 \
--size Standard_D2s_v3 \
--zone 1 \
--admin-username azureuser \
--ssh-key-values ~/.ssh/id_rsa.pub
az vm create \
--resource-group rg-webapp \
--name vm-web-az2 \
--image Ubuntu2204 \
--size Standard_D2s_v3 \
--zone 2 \
--admin-username azureuser \
--ssh-key-values ~/.ssh/id_rsa.pubAvailability Zones provide a 99.99% SLA — the highest for Azure VMs. Always prefer Zones over Availability Sets when available in your region.
Virtual Machine Scale Sets
Virtual Machine Scale Sets (VMSS) let you create and manage a group of identical, load-balanced VMs. The number of VM instances can automatically increase or decrease based on demand.
# Create a VMSS with autoscaling
az vmss create \
--resource-group rg-webapp \
--name vmss-web \
--image Ubuntu2204 \
--instance-count 2 \
--vm-sku Standard_D2s_v3 \
--admin-username azureuser \
--ssh-key-values ~/.ssh/id_rsa.pub \
--upgrade-policy-mode Automatic \
--autoscale \
--min-count 2 \
--max-count 10 \
--scale-out-cpu-threshold 75The autoscale configuration creates a default scale-out rule (when average CPU > 75%) and a scale-in rule (when average CPU < 25%). The instance count stays between --min-count and --max-count.
VM Extensions and Custom Script
VM Extensions are small applications that run post-deployment tasks on VMs. They can install software, configure settings, or collect diagnostics. The Custom Script Extension lets you run arbitrary scripts.
# Upload a script to a storage account
az storage blob upload \
--account-name stscriptsprod \
--container-name scripts \
--name setup-web.sh \
--file ./scripts/setup-web.sh
# Execute the script on a VM
az vm extension set \
--resource-group rg-webapp \
--vm-name vm-web-01 \
--name CustomScript \
--publisher Microsoft.Azure.Extensions \
--settings '{"commandToExecute":"bash /tmp/setup-web.sh", "fileUris":["https://stscriptsprod.blob.core.windows.net/scripts/setup-web.sh"]}'The Custom Script Extension downloads files from the fileUris and executes the command. It is commonly used for initial VM configuration that is not handled by cloud-init.
Custom Script Extensions execute with root privileges on Linux and SYSTEM on Windows. Ensure scripts are tested and do not expose secrets or execute untrusted code.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.