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.
But first, a little clarification about shutting down (thanks to Jeff Brubaker for pointing out). Stopped and Stopped (Deallocated) are not the same thing.
Stopped vs Stopped (Deallocated)
The first method to shutdown an Azure VM, is to Shutdown the Operating System while you are connected to it using Remote Desktop. It happens when you are done with your work, go to the Power options within the Windows OS and select the Shutdown option. This will essentially “turn off” or power down the Virtual Machine (VM), and stop it from running. Even though the VM is not running you will still be paying for the Azure VM compute resources. Azure Portal will report the status of the VM as “Stopped”.
The second method, is to go into the Azure Portal (or use the Azure CLI or Azure PowerShell) and use Azure to Stop the VM. Instead of just shutting down the Operating System, Azure will also deallocate the compute resources allocated for the VM. This releases the compute resources to be used for another customer within Microsoft Azure. This will cause Azure to no longer charge you for the compute resources, and Azure will report the status of the VM as being in a “Stopped (Deallocated)” state.
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
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
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!
Well, stay tuned for upcoming articles. Say hi, share this article, leave a comment or Subscribe now to stay updated through our newsletter. Also, check out our services or contact us at email@example.com for your software and consultancy requirements.