Azure Virtual Machines

Introduction

Azure Virtual Machines (VMs) provide scalable computing resources in the cloud, offering full control over the operating system and configuration.

Key Features:

  • Multiple VM sizes and families
  • Windows and Linux support
  • Pay-as-you-go pricing
  • Auto-scaling capabilities
  • Global availability
  • Integrated monitoring

VM Creation

Azure CLI

# Create resource group
az group create --name myResourceGroup --location eastus

# Create VM
az vm create \
    --resource-group myResourceGroup \
    --name myVM \
    --image Ubuntu2204 \
    --admin-username azureuser \
    --generate-ssh-keys \
    --size Standard_DS2_v2

Azure PowerShell

# Create VM configuration
$vmConfig = New-AzVMConfig `
    -VMName "myVM" `
    -VMSize "Standard_DS2_v2"

# Set OS profile
$cred = Get-Credential
Set-AzVMOperatingSystem `
    -VM $vmConfig `
    -Windows `
    -ComputerName "myVM" `
    -Credential $cred

# Create VM
New-AzVM `
    -ResourceGroupName "myResourceGroup" `
    -Location "eastus" `
    -VM $vmConfig

Networking

Virtual Network Setup

# Create VNet and subnet
az network vnet create \
    --resource-group myResourceGroup \
    --name myVNet \
    --address-prefix 10.0.0.0/16 \
    --subnet-name mySubnet \
    --subnet-prefix 10.0.0.0/24

# Create public IP
az network public-ip create \
    --resource-group myResourceGroup \
    --name myPublicIP \
    --sku Standard

Network Security Groups

# Create NSG
az network nsg create \
    --resource-group myResourceGroup \
    --name myNSG

# Add inbound rule
az network nsg rule create \
    --resource-group myResourceGroup \
    --nsg-name myNSG \
    --name allow-ssh \
    --protocol tcp \
    --priority 1000 \
    --destination-port-range 22

Storage Options

Managed Disks

# Create managed disk
az disk create \
    --resource-group myResourceGroup \
    --name myDataDisk \
    --size-gb 128 \
    --sku Premium_LRS

# Attach disk to VM
az vm disk attach \
    --resource-group myResourceGroup \
    --vm-name myVM \
    --name myDataDisk

Storage Types:

  • Ultra Disk Storage
  • Premium SSD
  • Standard SSD
  • Standard HDD

Security

Identity and Access

# Enable system-assigned managed identity
az vm identity assign \
    --resource-group myResourceGroup \
    --name myVM \
    --role Contributor \
    --scope /subscriptions/mySubscriptionId

# Configure Azure AD login
az vm extension set \
    --resource-group myResourceGroup \
    --vm-name myVM \
    --name AADLoginForWindows \
    --publisher Microsoft.Azure.ActiveDirectory

Encryption

Security Features:

  • Azure Disk Encryption
  • Key Vault integration
  • Network security groups
  • Just-in-time access
  • Azure Security Center

Management

Monitoring

# Enable diagnostics
az vm diagnostics set \
    --resource-group myResourceGroup \
    --vm-name myVM \
    --storage-account mystorageaccount

# View VM metrics
az monitor metrics list \
    --resource myVM \
    --resource-group myResourceGroup \
    --resource-type "Microsoft.Compute/virtualMachines" \
    --metric "Percentage CPU"

Maintenance

Management Tasks:

  • Backup and recovery
  • Patch management
  • Auto-shutdown
  • Performance monitoring
  • Cost analysis

Best Practices

Performance Optimization

Guidelines:

  • Right-size VMs
  • Use Premium storage for production
  • Implement auto-scaling
  • Monitor performance metrics
  • Use availability sets

Cost Management

Cost Saving Tips:

  • Use reserved instances
  • Implement auto-shutdown
  • Clean up unused resources
  • Monitor usage patterns
  • Use budget alerts