Virtual Machine

VM Image

  • az vm image list: list all VM images

  • az vm image list-publishers --location australiaeast --output table: List image publishers

  • az vm image list-offers --location australiaeast --publisher Canonical|RedHat --output table

  • az vm image list-skus --location australiaeast --publisher RedHat --offer RHEL --output table

  • az vm image list --location australiaeast --publisher RedHat --offer RHEL --sku 9_1 --all --output table: this command can be used with az vm create --image

Create VM using Azure CLI

  • Create Resource Group

  • Create Virtual Network

  • Create Virtual Machine

Create Resource Group

  • Use az account list-locations: List region

  • View Resource Group: az group list

# create shell variables
resourceGroup=RG_Lab
location=australiaeast 
az group create --name $resourceGroup --location $location

Create Virtual Network

  • az network vnet list

Create a VM

  • To deploy a VM on a VNet, they must have the same Azure location.

  • Once a VM is created, the VNet to which it's connected can't be changed.

  • --generate-ssh-keys: id_rsa key will generated in .ssh folder

Verification

  • List VM in table: az vm list -otable

  • View VM: az vm show --name $vmName --resource-group $resourceGroup

  • or az vm show -n $vmName -g $resourceGroup

  • Extracting Object ID: az vm show --name $vmName --resource-group $resourceGroup --query 'networkProfile.networkInterfaces[].id' --output tsv

  • Assign that value to ShellVariable to get Nic ID:

    • nicId=$(az vm show -n $vmName -g $resourceGroup --query 'networkProfile.networkInterfaces[].id' -o tsv)

    • az network nic show --ids $nicId

    • Result:

  • Obtain Public IP address and subnet Object IDs

  • Read value and set as variables

  • Query VM IP Address: vmIpAddress=$(az network public-ip show --ids $ipId --query ipAddress -o tsv)

  • View Result

Remove all resources in the group

  • az group delete --name $resourceGroup --no-wait: keep the CLI from blocking

  • az group wait --name $resourceGroup --deleted: watch the process and wait until the deletion is complete

Manage VM

  • az vm list -otable: List VM in table

  • az vm show -n $name -g $ResourceGroup

  • az vm stop -n $name -g $ResourceGroup: stop VM, but it's still bill, to stop billing, need to deallocate it

  • az vm deallocate -n $name -g $ResourceGroup: deallocate a VM so it's not billed

Resize VM

  • List vm size: az vm list-vm-resize-options --resource-group $resourceGroup --name $vm --query "[].name"

Redeploy VM

  • If you face difficulties troubleshooting SSH or application access to a Linux virtual machine (VM) in Azure, redeploying the VM may help. When you redeploy a VM, it moves the VM to a new node within the Azure infrastructure and then powers it back on. All your configuration options and associated resources are retained

  • az vm redeploy --resource-group myResourceGroup --name myVM

Reference

Last updated