Start or Stop all VMs of a Resource Group in Azure

There is no direct way to start or stop all VMs of a resource group in Azure. In Azure, shutting down the server is not enough, you still pay the full compute price. It is recommended that you deallocate your servers if you want to avoid being charged while the servers are not in use. In this post, you will learn how to setup a few commands quickly. You must have Azure CLI 2.0 installed for this script to work.

Step 1: Script the common tasks

If you are using Resource Groups in Azure to organize your servers, it will make your job a lot easier in the long run. The following script has a few different parameters:

  • start: Starts all servers in the resource group
  • stop: Stops & deallocates all servers in the resource group
  • status: List detailed status of all servers in the resource group
  • ip: Shows the public IPs of all servers in the resource group

and so on...

Feel free to add more scenarios as you like. To use this script, you need to replace YOUR_RESOURCE_GROUP_NAME appropriately.

Step 2: Call script with parameters

Assuming you have created a script.sh file for the script above, and given it execute permissions using chmod 755 script.sh, you can pass the arguments directly:

  • /path/to/script.sh stop
  • /path/to/script.sh start

and so on...

Step 3: Create Aliases (Optional)

Assuming you are using bash, you can create aliases by editing your ~/.bash_profile and pasting the code below:

alias vmstart="/path/to/vm/script.sh start"  
alias vmstop="/path/to/vm/script.sh stop"  
alias vmlistip="/path/to/vm/script.sh ip"  
alias vmstatus="/path/to/vm/script.sh status"  

Restart your terminal or source the bash_profile using source ~/.bash_profile.

Enjoy your new commands

  • vmstart : Start all VMs
  • vmstop : Stop all VMs
  • vmlistip : Show public IPs of all servers
  • vmstatus : Show detailed status of servers

You might be thinking why you would need to list public IPs of all the VMs? Well, if you deallocate your VMs the public IPs are released, and you will get new ones when you start them again. Having the IPs handy will help you update and connect to the VMs quickly without having to go to the Azure dashboard.

Hope this helps!

What next?

Not much! Say hi, leave a comment or Subscribe now to stay updated through our monthly newsletter. Also, check out our services or contact us at contact@attosol.com for your software and consultancy requirements.

Ads:

Rahul Soni

⌘⌘ Entrepreneur. Author. Geek. ⌘⌘

Kolkata, India

Subscribe to Attosol Technologies

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!