Add/Start/Delete/Deallocate a VM in Azure

Azure CLI samples full list | Microsoft Learn

-g = –resource-group
-n = –name
-d = –show-details in az vm list -d
az config set core.output=table


az login

az vm create -n vm-name -g resource-group-name –image Ubuntu2204 –admin-username admin-user-name –admin-password admin-user-password

create a windows vm

az vm create -n vm-name -g resource-group-name –image Win2012R2Datacenter –admin-username admin-user-name –admin-password admin-user-password

list the vms

az vm list -d or az vm list –show-details
az vm show -g resource-group-name -n vm-name

start the vm

az vm start -g resource-group-name -n vm-name

connect to vm using private key or username/password

ssh -i .ssh/pabc.pem username@public-ip-address
ssh username@public-ip-address

stop the vm

az vm stop -g resource-group-name -n vm-name
az vm list -d

–need to change it to de-allocate status or else it will continued to be billed

az vm deallocate -g resource-group-name -n vm-name
az vm list -d

Once done , log out from Azure CLI

az logout


az vm list -d –query “[?powerState==’VM running’]”

delete the resource groups are no longer needed

az group delete -n resource-group-name –no-wait

note that time is based on UTC

az vm auto-shutdown -g MyResourceGroup -n MyVm –time 1730 –email “foo@bar.com”

Assign output to a variable $IPADDRESS

IPADDRESS=”$(az vm list-ip-addresses -g resource-group-name -n vm-name –query “[].virtualMachine.network.publicIpAddresses[*].ipAddress” –output tsv)”

And test with curl – curl –connect-timeout 5 http://$IPADDRESS

az network nsg list -g resource-group-name –query ‘[].name’ \ –output tsv

az network nsg rule list -g resource-group-name –nsg-name nsg-name –query ‘[].{Name:name, Priority:priority, Port:destinationPortRange, Access:access}’ -otable

Allow inbound port 80

az network nsg rule create -g resource-group-name –nsg-name my-vmNSG –name allow-http –protocol tcp –priority 100 –destination-port-range 80 –access Allow

Thanks for reading and happy learning! billy-at-python.sg

Leave a Reply 0

Your email address will not be published. Required fields are marked *