How To Setup Auto-Start And Auto-Shutdown In Azure Virtual Machines

 
Azure services cost varies based on utilization and uptime and is one of the important factors for overall billing amount. In most scenarios, we use Azure VM during office hours or some specific duration only but it's useless in off-hours. And paying Azure virtual cost during off-hours is simply unnecessary. In other words, we might not need to run it the full  24 hours for users or services running in the VMs, however, the utilization can be only during office hours or daytime. Therefore, we can optimize the resources and reduce the cost significantly by implementing auto-shutdown and auto-start in azure virtual machines. This means, we can add schedule for shutting down and starting virtual machine automatically during off hours.
 
In this article, I will illustrate how to implement auto-start and auto-shutdown in Azure virtual machine which will significantly cut the overall cost.
 

Auto-Start and Auto-shutdown of virtual machine

 
This is one the features of Azure which enables you to start and stop virtual machines on user-defined schedules, provides insights through Azure Monitor Logs, and send optional emails by using action groups. This feature is available for both Azure Resource Manager and classic VMs for most scenarios.
 
For this article, I will use Azure DevTest Lab with my virtual machine.
 
First, open Azure portal with link Portal.azure.com and select DevTest Lab or you can search for it from search bar.
 
Note
You need to have subscription or can create trial one.
 
 
Then choose your lab. You can create new lab. Here I have already one lab with virtual machine window server with SQL server developer version. Choose lab, select configuration and policies as shown below,
 
 
After selecting configuration and policies, you will see the following 3 options associated with start and stop schedules as portrayed.
 
 

Auto Shutdown policy

 
Set the level of control that lab users have over their individual VM shutdown schedules. Choose between granting users the freedom to set a schedule or instituting a mandatory schedule.
 
There are three choices: 1. User sets a schedule and can opt out, 2. User sets a schedule and cannot opt out and 3. User has no control over the schedule set by lab administrator.
 
By the options, it is clear that we can either allow users to set a schedule with or without opt out or only to administrator.
 
 

Auto shutdown

 
This option will allow you to set auto shutdown time and notifications. This policy applies auto-shutdown to virtual machines in the lab. Set enforcement policy for this schedule through the Auto shutdown policy blade.
 
 
You can enable with on and off option, then set schedule time for auto-shutdown (i.e. off-hours) which can be any off-hours or based on user defined, third option for time zone of the schedule, and finally, option to send notifications before shutdown with yes /no.
 

Auto start

 
This option for configuring auto start of azure virtual machines. Auto-start allows you to automatically start virtual machines in a lab at a scheduled time each day. The options are more flexible compare to shutdown policy.
 
Three options are the same as shutdown policy: Allow auto-start with Yes and No, schedule start time and time zone. Another exclusive option is that you can choose days for auto start policy as illustrated below.
 
 
Note
This policy does not apply auto-start to any virtual machines in the lab. To apply auto-start policy to individual virtual machines, go to the virtual machine blade and enable auto-start.
 
Let me explain this note in more detail, here we simply added policy for auto start to whole resources under this lab, however, we need to apply auto start individually to each virtual machine.
 
Now I will navigate one virtual machine under this resource lab and apply the auto start policy to the particular virtual machine.
 
 
We can simply apply the policy auto start to the virtual machine with option allow this Virtual machine to be scheduled for automatic start as shown above.
 

Conclusion

 
In this article, we learned about Azure virtual machine policies related with automatic start and stop the virtual machine based on user-defined schedule. This policy and configuration provided a flexible option to optimize our virtual machines based on demand and reduce the overall cost significantly. Therefore, we can schedule auto start of virtual machines based on requirement and turn it off during off hours or weekends.