VM Resources

Virtual machines falls into Infrastructure as Service model and are a popular hosting solution. The client has full control over the operating system. A single virtual machine can only run one operating system. Azure offers options to manage and configure a large scale of identical virtual machines at the same time (scale set options) and to optimize the used of resources via load balancers. Azure can also give a protection against network and systems failures and can keep your resources available even during update process (availability sets).

Also, Azure allows you to deploy a virtual machine that has a virtual Desktop experience. This might be useful to deploy Azure Virtual Desktop for users that do not have any experience with command line interface or simply to use web applications with an integrated UI.

Azure CLI

Creation of a vm

The command below aim to create a virtual machine.

az vm create \
  --resource-group learn-bde9457f-9061-4f46-ba9f-199206dee6f9 \
  --name vm-name \
  --image UbuntuLTS \
  --admin-username azureuser \
  --generate-ssh-keys

Azure extension

az vm extension is to automate the process of updating configurations or adding extensions to an existing VM.

The command below aim to install nginx on an azure virtual machine. We specify a file URI which point to a script to install nginx. Then, we tell Azure to execute the script using --protected-settings option.

az vm extension set \
  --resource-group learn-bde9457f-9061-4f46-ba9f-199206dee6f9 \
  --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"}'

List VM IP address

az vm list-ip-addresses

This command gives information about all our VM IP address.

It is possible to be more specific by adding the --query parameter and to stock the output of a command into a variable. In the command below we ask specifically for the public IP address of the my-vm VM.

IPADDRESS="$(az vm list-ip-addresses \
  --resource-group learn-f84af197-9084-4bfd-aba5-a080400ccc17 \
  --name my-vm \
  --query "[].virtualMachine.network.publicIpAddresses[*].ipAddress" \
  --output tsv)"

Network Security Groups commands

List Network Security Groups

The command below will list the name properties of all Network Security group associated with the specified resource-group

az network nsg list \
  --resource-group learn-f84af197-9084-4bfd-aba5-a080400ccc17 \
  --query '[].name' \
  --output tsv

List rules associated with a Network Security Group

To list the rules associated to a specific NSG. The --query parameter allow us to be more specific and to query uniquely for the Priority, Port and Access. The --output option allows to display the output in a table format.

az network nsg rule list \
  --resource-group learn-f84af197-9084-4bfd-aba5-a080400ccc17 \
  --nsg-name my-vmNSG \
  --query '[].{Name:name, Priority:priority, Port:destinationPortRange, Access:access}' \
  --output table

Create a NSG Rule

The command below aim to create a rule for the NSG my-vmNSG that will allow inbound connection on port 80 (HTTP).

az network nsg rule create \
  --resource-group learn-f84af197-9084-4bfd-aba5-a080400ccc17 \
  --nsg-name my-vmNSG \
  --name allow-http \
  --protocol tcp \
  --priority 100 \
  --destination-port-ranges 80 \
  --access Allow

Last updated