How To Automatically Start & Stop Your Azure Virtual Machine

At GSoft, we've been active users of the Cloud for many years now, working mostly with Azure but dabbling here and there with AWS and GCP. While we always try to use SaaS and PaaS offerings as much as possible since they require very little maintenance and typically have a "pay as you use" pricing model, we still have a few workloads that depend on traditional virtual machines.

In an effort to optimize our Virtual Machine availability and control costs, we investigated multiple options available on the market to help us automatically start and stop these Virtual Machines to coincide with when they are needed:

  • Azure Portal ;
  • Automation Accounts ;
  • Third-Party Solutions ;
  • AI-Powered Solutions ;

Here are the solutions we came across in overview and our take on each of them. This article should help you find the solution that fits your business needs.

The Azure Portal

The simplest (and free) option is offered on the Azure Portal itself. Each Virtual Machine has an auto-shutdown option directly in the portal:

Azure Portal Virtual Machine Schedule

This is a great solution to avoid having to pay for a virtual machine that was forgotten. However, this just handles half of the requirements as we also needed the VMs to be started back up at the right time for our users. We needed to dig deeper.

Automation Accounts

Azure also offers Automation Accounts that can be used to run automated scripts within your Azure environnement. These are significantly more complicated to set up and run but are also much more powerful and offer a large number of options. Pricing is quite low and the first 500 minutes of job run time each month are free.

The first step is to create a new Automation Account. Once that's done, you can go to Process Automation and create a Runbook. A Runbook is essentially a script that can be run on a schedule and with specific parameters. Luckily, there exists a large number of Runbooks already available in the Runbook gallery that is accessible right in the Azure portal, including Runbooks to manage VM startup and shutdown. Most Runbooks are in PowerShell but there are some Runbooks in Python as well.

After that, you must configure the Runbook to run at specific times by creating schedules. Virtual machine schedules can be created directly within the Runbook or they can be created as a Shared schedule in the Automation Account and can then be shared between Runbooks.

The Jobs section will contain entries for each time the jobs have run as well as any input, output, errors, warnings, logs, etc. that were generated by running the script. You can use this as an audit trail of each time the Runbook was executed.

It's also important to properly configure the Service Accounts and permissions to allow the Runbook to have access to the resources it needs to act on in Azure. If you don't have the correct permissions, the script will fail and you'll get the error messages in your logs. Service Principals are specified in the Automation Account's settings in the "Credentials" or "Connections" section.

If you have multiple Virtual Machines on Azure, you will need to create multiple schedules with the right parameters to accommodate each of them. Alternatively, you can create a single Runbook that will start or stop all VMs that have specific tags assigned to them. This allows you to have a single Schedule that will automatically apply on any VM that has the proper tags applied. It is even possible to use tags to specify a schedule. There's a lot of power here but you'll be performing a lot of manual configuration for each Virtual Machine and you will also need to change these as your needs evolve for each VM.

Third-Party Solutions

There are a few third-party solutions out there to assist you with virtual machine automation. They can be connected to your Azure tenant and display a list of all your VMs. They typically allow you to specify a schedule for a virtual machine or a group of VMs and they will make sure to follow it automatically for you. In terms of power and simplicity, this is the best solution so far because all configuration can be done via a user-friendly web interface. Additionally, you don't need to perform any monitoring since the service will take care of everything for you.

These schedules are typically fixed, however, and need to be adjusted manually anytime the usage pattern for a virtual machine changes. Additionally, they use fixed moments to stop and start VMs. This means that in many cases, the VM will be left on even if it is not being used.

These services typically cost around 5$ per month per VM but it can vary based on exact offering.

Artificial Intelligence-Powered Solution (our story)

At GSoft, we initially decided to go with an Automation Account. We're developers and admins, so we know how to handle scripting we figured we shouldn't have too much trouble. It worked quite well, but unfortunately it required a fair amount of maintenance. New VMs required new schedules. Changing usage patterns needed to be programmed in to schedules as needed. Basically, the solution didn't scale. It required our admins to remain available and constant communication between the users who depend on the VMs for their work and the IT team to make it work.

We build products for the infrastructure and productivity clouds and as such, we get to talk to a lot of IT admins. This problem was also shared by many of our customers and they had various solutions to this same problem. There had to be a simpler way.

That's when we decided to build a new product that could manage these changing requirements for us. Snooz.It's approach is completely different because it's based on Machine Learning. It analyzes the previous usage patterns for the Virtual Machine and then determines a prediction of when the next activity will occur. As such, it can monitor in near real-time the usage of a Virtual Machine, determine when it's not longer in use and shut it down. It can then use its AI to predict when the VM should next be needed and turn it back on before this moment. To the end-user, this is completely transparent. The virtual machine is up when they need it and in the end, that's all that's important to them.

This also means that there are no schedules to configure. Simply turn on Autopilot on one or more Virtual Machines and let Snooz.It automatically start and stop them as needed. Set it and forget it, letting Snooz.It make sure that you're saving as much as possible on your cloud costs.

We're quite happy with the solution and we invite you to try it now for free to see for yourself. Leverage the power of artificial intelligence to get all of the value with none of the manual work to set it up. We're always looking for feedback so be sure to let us know what you think and if it's been as useful for you that it has been for us.