Get full Access to Website Content, WebShark packet capture tool and Proximity Download. Purchase
⛶ Night Mode

Azure Compute and Networking Services

Azure Virtual Machines (VMs)

Azure Virtual Machines (VMs) provide Infrastructure as a Service (IaaS), allowing you to create and manage virtualized servers in the cloud. They offer:
Full control over the OS
Ability to run custom software & configurations
Scalability without maintaining physical hardware

VMs can be provisioned quickly using preconfigured images, including OS and development tools.


Scaling Azure VMs

Azure offers two key options for scaling VMs:

1. Virtual Machine Scale Sets

✅ Manage a group of identical, load-balanced VMs
✅ Automate scaling based on demand or schedule
✅ Built-in load balancing for optimized resource utilization
✅ Ideal for big data, compute workloads, and containers

2. Virtual Machine Availability Sets

✅ Ensures high availability by preventing a single point of failure
✅ Uses:

  • Update Domains: Stagger reboots during updates
  • Fault Domains: Spread VMs across different power/network sources
    ✅ No additional cost—only pay for VM instances

Common Use Cases for VMs

💻 Testing & Development – Quickly create and delete OS/application environments
☁️ Running Cloud Applications – Scale dynamically based on demand
🔗 Extending Datacenter to Cloud – Host apps (e.g., SharePoint) in Azure
⚠️ Disaster Recovery – Use Azure VMs as failover during datacenter outages
🚀 “Lift & Shift” Migration – Move physical servers to VMs with minimal changes


Create an Azure virtual machine

In this exercise, you create an Azure virtual machine (VM) and install Nginx, a popular web server.

You could use the Azure portal, the Azure CLI, Azure PowerShell, or an Azure Resource Manager (ARM) template.

In this instance, you’re going to use the Azure CLI.

Task 1: Create a Linux virtual machine and install Nginx

Use the following Azure CLI commands to create a Linux VM and install Nginx. After your VM is created, you’ll use the Custom Script Extension to install Nginx. The Custom Script Extension is an easy way to download and run scripts on your Azure VMs. It’s just one of the many ways you can configure the system after your VM is up and running.

1. From Cloud Shell, run the following az vm create command to create a Linux VM:

az vm create \
  --resource-group "[sandbox resource group name]" \
  --name my-vm \
  --public-ip-sku Standard \
  --image Ubuntu2204 \
  --admin-username azureuser \
  --generate-ssh-keys 

Your VM takes a few moments to come up. You named the VM my-vm. You use this name to refer to the VM in later steps.

az vm create --resource-group "[sandbox resource group name]" --name my-vm --public-ip-sku Standard --image Ubuntu2204 --admin-username azureuser --generate-ssh-keys
anjanchandra_com [ ~ ]$ az vm create --name sandbox-vm --resource-group learn-665c0045-cc94-47b7-aae1-d0d5bc209e33 --public-ip-sku Standard --image Ubuntu2204 --admin-username azureuser --generate-ssh-keys
SSH key files '/home/anjanchandra_com/.ssh/id_rsa' and '/home/anjanchandra_com/.ssh/id_rsa.pub' have been generated under ~/.ssh to allow SSH access to the VM. If using machines without permanent storage, back up your keys to a safe location.
{
  "fqdns": "",
  "id": "/subscriptions/651da11b-7757-4742-ac91-148e47c2869b/resourceGroups/learn-665c0045-cc94-47b7-aae1-d0d5bc209e33/providers/Microsoft.Compute/virtualMachines/sandbox-vm",
  "location": "westus",
  "macAddress": "00-22-48-0B-6E-1B",
  "powerState": "VM running",
  "privateIpAddress": "10.0.0.4",
  "publicIpAddress": "13.83.87.223",
  "resourceGroup": "learn-665c0045-cc94-47b7-aae1-d0d5bc209e33",
  "zones": ""
}
anjanchandra_com [ ~ ]$ 

2. Run the following az vm extension set command to configure Nginx on your VM:

az vm extension set \
  --resource-group "[sandbox resource group name]" \
  --vm-name my-vm \
  --name customScript \
  --publisher Microsoft.Azure.Extensions \
  --version 2.1 \
  --settings '{"fileUris":["https://raw.githubusercontent.com/MicrosoftDocs/mslearn-welcome-to-azure/master/configure-nginx.sh"]}' \
  --protected-settings '{"commandToExecute": "./configure-nginx.sh"}'    

The az vm extension set command in Azure CLI is used to install or update a virtual machine extension on an Azure VM. Extensions allow you to add post-deployment configurations, such as running scripts, configuring monitoring agents, or enabling security tools.


Basic Syntax

az vm extension set --resource-group <ResourceGroup> --vm-name <VMName> \
  --name <ExtensionName> --publisher <PublisherName> --version <Version>

Example Use Cases

1. Install the Custom Script Extension (Run a Script on VM)

az vm extension set --resource-group MyResourceGroup --vm-name MyVM \
  --name CustomScriptExtension --publisher Microsoft.Azure.Extensions \
  --version 2.1 --settings '{"commandToExecute":"echo Hello World"}'

🔹 Runs the command "echo Hello World" inside the VM.


2. Install the Azure Monitoring Agent

az vm extension set --resource-group MyResourceGroup --vm-name MyVM \
  --name AzureMonitorLinuxAgent --publisher Microsoft.Azure.Monitor \
  --version 1.0

🔹 Installs the Azure Monitor Agent for collecting VM logs.


3. Install the Azure VM Access Extension (Reset SSH or RDP Credentials)

az vm extension set --resource-group MyResourceGroup --vm-name MyVM \
  --name VMAccessForLinux --publisher Microsoft.OSTCExtensions \
  --version 1.5 --protected-settings '{"username":"adminuser", "password":"NewP@ssword123"}'

🔹 Resets the username/password for a Linux VM.


Common Parameters

ParameterDescription
--resource-groupThe name of the resource group containing the VM.
--vm-nameThe name of the virtual machine.
--nameThe extension name (e.g., CustomScriptExtension).
--publisherThe extension publisher (e.g., Microsoft.Azure.Extensions).
--versionThe version of the extension.
--settingsJSON-formatted settings for the extension.
--protected-settingsJSON-formatted sensitive settings (like passwords).

List Available Extensions

If you’re unsure which extensions are available, run:

az vm extension image list --location eastus --output table

https://raw.githubusercontent.com/MicrosoftDocs/mslearn-welcome-to-azure/master/configure-nginx.sh

#!/bin/bash

# Update apt cache.
sudo apt-get update

#We need to add repo before installing nginx-core. Otherwise we get an error
sudo add-apt-repository main
sudo add-apt-repository universe
sudo add-apt-repository restricted
sudo add-apt-repository multiverse  

# Install Nginx.
sudo apt-get install -y nginx

# Set the home page.
echo "<html><body><h2>Welcome to Azure! My name is $(hostname).</h2></body></html>" | sudo tee -a /var/www/html/index.html

Install ngnix using this bash script. With az extension set, you can run this shell script inside the Azure VM.

az vm extension set \
  --resource-group "learn-8ffd011b-7d78-4692-886b-eada42e016a9" \
  --vm-name sandbox-vm \
  --name customScript \
  --publisher Microsoft.Azure.Extensions \
  --version 2.1 \
  --settings '{"fileUris":["https://raw.githubusercontent.com/MicrosoftDocs/mslearn-welcome-to-azure/master/configure-nginx.sh"]}' \
  --protected-settings '{"commandToExecute": "./configure-nginx.sh"}' 

Command output

anjanchandra_com [ ~ ]$ az vm extension set \
  --resource-group "learn-8ffd011b-7d78-4692-886b-eada42e016a9" \
  --vm-name sandbox-vm \
  --name customScript \
  --publisher Microsoft.Azure.Extensions \
  --version 2.1 \
  --settings '{"fileUris":["https://raw.githubusercontent.com/MicrosoftDocs/mslearn-welcome-to-azure/master/configure-nginx.sh"]}' \
  --protected-settings '{"commandToExecute": "./configure-nginx.sh"}' 
{
  "autoUpgradeMinorVersion": true,
  "enableAutomaticUpgrade": null,
  "forceUpdateTag": null,
  "id": "/subscriptions/16982123-2d3c-44ce-ab2d-0c419f4ce699/resourceGroups/learn-8ffd011b-7d78-4692-886b-eada42e016a9/providers/Microsoft.Compute/virtualMachines/sandbox-vm/extensions/customScript",
  "instanceView": null,
  "location": "westus",
  "name": "customScript",
  "protectedSettings": null,
  "protectedSettingsFromKeyVault": null,
  "provisionAfterExtensions": null,
  "provisioningState": "Succeeded",
  "publisher": "Microsoft.Azure.Extensions",
  "resourceGroup": "learn-8ffd011b-7d78-4692-886b-eada42e016a9",
  "settings": {
    "fileUris": [
      "https://raw.githubusercontent.com/MicrosoftDocs/mslearn-welcome-to-azure/master/configure-nginx.sh"
    ]
  },
  "suppressFailures": null,
  "tags": null,
  "type": "Microsoft.Compute/virtualMachines/extensions",
  "typeHandlerVersion": "2.1",
  "typePropertiesType": "customScript"
}
anjanchandra_com [ ~ ]$