Loading Now

How to auto shutdown Azure VM when idle

There are three methods to automatically shut down idle Azure VMs. The built-in Auto-shutdown feature, located under Operations on the VM blade, allows for the deallocation of a VM at a specified time each day. However, this feature only stops the VM and does not restart it. To achieve both automated stopping and starting, you can utilise Azure Automation runbooks or Automation Tasks/Logic Apps. For managing schedules for numerous VMs across multiple subscriptions with the added benefit of upfront cost analysis, Turbo360’s Cost Analyzer is an excellent choice.

Key Takeaways

  • The native Azure Auto-shutdown feature is free and can be set up in just two minutes. Its limitation is the lack of an auto-start function, necessitating manual reactivation each morning.
  • Deallocating a VM ceases compute charges; you will continue to incur minimal charges for the disk, which is significantly less expensive.
  • Azure Automation enables both stopping and starting of VMs, but it requires the maintenance of runbooks and scripts, which can create additional complexity for teams.
  • When dealing with multiple VMs across different subscriptions, setting them up individually is not scalable. Turbo360 addresses this gap by providing upfront cost-saving analysis before implementation.

What Defines an “Idle” Azure VM?

An idle Azure VM is one that remains powered on but performs minimal or no useful tasks. Common examples include development or testing boxes left active overnight or machines running with low CPU and memory usage outside of business hours.

This is where confusion often arises: an idle VM incurs the same costs as an active one since Azure bills you for the reserved compute capacity rather than based on actual workload. Therefore, a VM running at just 3% CPU over the weekend costs the same as one under full load.

Non-production VMs frequently contribute to unnecessary expenses. Although they are primarily needed during business hours, they are often left running continuously, leading to wasted expenditure during off-hours.

How Do Idle VMs Slowly Drain Your Azure Budget?

A few key factors contribute to the unwanted costs:

  • Workloads often decline to near-zero during off-peak hours while VMs continue to operate at full allocated capacity.
  • Over-provisioned machines incur costs that go unused, and under-provisioned ones may take longer to find and resolve.
  • Dependencies on networking and storage resources (such as a SQL Server backend) can also lead to prolonged periods of VM idleness.

These issues result in a slow drain on your budget rather than an immediate spike, making it challenging to pinpoint which VMs contributed to increased costs when the bill arrives.

The solution is straightforward: identify what’s causing the idle times and power down the respective VMs when they’re not needed. However, manually doing this across a varied environment can be cumbersome, which is why automation is often the preferred route.

What Are the Built-In Options for Auto-Shutting Down Azure VMs?

Azure offers two built-in methods, both effective but with limitations worth considering before making a choice.

Option 1: Built-in Auto-shutdown (via Azure Portal)

This method is quick to implement as every VM includes a built-in scheduling feature.

  1. Access the Azure portal and navigate to your virtual machine.
  2. In the left sidebar under Operations, select Auto-shutdown.
  3. Turn it On, then specify your desired shutdown time along with the time zone. (Remember to check the default time zone, which is UTC.)
  4. Auto-shutdown-feature-in-Azure-Portal How to auto shutdown Azure VM when idle

  5. Optionally, enter an email address or webhook URL to receive a notification prior to the VM shutting down.
  6. Save your settings.

When the time approaches, Azure will deallocate the VM. You will no longer accrue compute charges, while your disk remains intact, ensuring data safety.

However, note that this Auto-shutdown feature only allows for scheduling a shutdown; there is no provision for starting the VM back up automatically. Therefore, if the VM deactivates at 6 PM, it requires someone to power it back on manually the following morning. While suitable for an individual VM, this becomes unwieldy when managing several.

Option 2: Azure Automation (Using Runbooks and Automation Tasks)

If you require the VM to restart autonomously, Azure Automation is the way forward.

The simplified version utilises Automation Tasks, managed through Logic Apps:

  1. Select the Automation option on the VM blade and navigate to Tasks.
  2. Add a task and choose the Deallocate virtual machine template, then set the desired time and frequency.
  3. Add another task using the Start virtual machine template for the morning hours.

If you wish to create a more complex setup, you can write a runbook, which is essentially a script detailing exactly which VMs to stop or start and when. Runbooks can be customised to target VMs based on tags, eliminating the need for hardcoding machine names.

It’s important to consider that while runbooks offer this flexibility, they can also introduce maintenance demands. A basic runbook does not differentiate between VMs, stopping all of them simultaneously, which may not align with operational needs. Tag-based runbooks provide better granularity, but they also require you to manage the scripts, with any bugs potentially resulting in a VM remaining shut down when it should be active, or vice versa.

Native Azure or Turbo360: Which One Is Right for You?

Built-in Auto-shutdownAzure AutomationTurbo360 Cost Analyzer
Shutdown schedulingYesYesYes
Startup schedulingNoYes (via runbook/task)Yes
Bulk scheduling of multiple VMsNo, individual setup requiredPartly, requires scriptingYes
Cross-subscription functionalityNoWith extra effortYes
Cost-saving projections before applicationNoNoYes
Setup requirementsVery lowMedium to high (requires scripts)Low
Covers non-VM servicesNoCustom coding neededYes, including App Service, SQL, Logic Apps, Cosmos DB, and more
CostFreeFree (though requires your time)Paid

How to Efficiently Automate VM Shutdowns at Scale Using Turbo360

If your application runs across 100 or more VMs distributed across various subscriptions, manually setting Auto-shutdown on each machine and restarting them each morning isn’t a sustainable strategy.

Turbo360’s Cost Analyzer provides the ability to create a single optimisation schedule that not only starts and stops VMs but can also adjust service tiers for compatible resources across all subscriptions, displaying projected savings before implementation.

Here’s how to set it up:

  1. Create a cost management group to encompass the subscriptions you wish to monitor. This will provide a consolidated view of your spending.
  2. Navigate to Optimization → Schedules → Add.
  3. Assign a name to the schedule, select your time zone, and write a brief description for easy identification later.
  4. Define the ‘up’ and ‘down’ hours. Typically, business hours run from around 8 AM to 5 PM, so outline the off-hours accordingly—estimated savings will display on screen as you adjust.
  5. Add-VM-optimization-schedule How to auto shutdown Azure VM when idle

  6. Select the VMs to be included in the schedule. These will be displayed by their respective subscriptions. Designate Start as the ‘Up’ action and Deallocate as the ‘Down’ action.
  7. Decide where the alerts will be sent. Turbo360 supports notifications to platforms such as ServiceNow, SMTP, PagerDuty, Microsoft Teams, Azure DevOps, Opsgenie, WebHook, Twilio, Slack, and more, ensuring your team is kept updated via their preferred channels.
  8. Review the summary—scheduled resources, projected savings, chosen notification channels—and click on Create Optimization schedule.

That’s it! Your VMs will now automatically stop and start according to the set schedule, with notifications sent out whenever an action is performed.

By implementing this method, you benefit from:

  • The ability to manage dozens or even hundreds of VMs within a single schedule, either stopping and starting simultaneously or in tailored groups.
  • Knowledge of potential savings before taking action, allowing for better financial planning.
  • An expanded scope of scheduling that extends beyond VMs; Turbo360’s Cost Analyzer can optimise App Service Plans, SQL Databases and pools, Logic Apps, Data Factory, Synapse pipelines, Cosmos DB (SQL Container, SQL Database, Tables), and VM Scale Sets.

Conclusion

Idle VMs present a straightforward opportunity for cost savings in your Azure expenditure, yet they are often overlooked. The built-in Auto-shutdown feature can cover a single VM in a matter of minutes. Azure Automation can manage both the shutdown and startup of VMs, provided you’re prepared to handle the scripting elements. However, as your VM fleet expands across multiple subscriptions, the practicality of individual setup diminishes, and an overarching scheduler that addresses the entire infrastructure while providing preliminary savings calculations can quickly prove its worth.

Related Resources

Share this content:


Discover more from Qureshi

Subscribe to get the latest posts sent to your email.

Discover more from Qureshi

Subscribe now to keep reading and get access to the full archive.

Continue reading