Scheduled Start/Start Azure Machine

I think everyone has the same problem: the Azure Virtual machines runs overnight where nobody is working on it and the costs are growing.

To reduce the cost for virtual machines in Azure Microsoft Flow can be used to schedule the start/stop processes of each Virtual Machine or complete resource groups in Azure.

For this there are two setups needed to be setup: Azure Automation Account and the Flows.

Azure Automation Accounts

First steps are done in Azure. In Azure the service “Automation Accounts” can be used for start/stop processes.

The service can easily be found by the search function in the top pane.

In the Automation Accounts a new Account will be created by choosing “+ ADD”. It is necessary to choose the Subscription in which the virtual machines are located. If there are virtual machines in different subscriptions for each subscription an Automation Account is needed.

In the next window the name of the Account and the used Subscription and resource group is defined. The Location should be in the location as the subscription is and for “create azure run as account” should be Yes.

After the account is created there the runbooks for start/stop processes has to be defined. In the Account the runbooks can be find in the details.:

For this process the needed templates can be found in the gallery:

In the gallery there are the needed runbooks available: The “Stop Azure V2 VMs” and the “Start Azure V2 VMs”. Both of them are needed as a runbook in the Automation Account.

The Following steps are similar to the Stop and the Start Runbook.

After selecting the runbook template this template need to be imported into the Automation Account.

In the next window the name of the runbook and a description is defined.

The imported runbook need to be published to be available. For this the imported runbook has to be edit:

In the edit screen the button “publish” will publish the runbook to be activated.

These steps are the setup which has to be done before setup the flows. This setup has to be done only one time because all of the future VMs in the subscription can be used with this setup.

Microsoft Flow

After this is finished the setup in Microsoft Flow can be done. For this business case you have to setup only 2 steps for stop and 2 steps for start. These steps are 95% similar. Only the runbook name and the recurrence hour are different.

Here an overview of the created flow which is now described how to setup.

Now the steps for stop a machine are described.

The first step is the trigger for the recurrence so that Flow knows when to run.

To setup this step there is a connector called “Schedule” and within this connector there is a trigger called “recurrence”.

In the trigger there is the setup for the time and when the flow has to run. Here in this example the trigger runs once a day at 08:36 in the time zone UTC+01:00.

There are also options available to use another frequency. If you choose weekly at the frequency you can choose on which weekdays the flow should run.

At the bottom there is a short preview of your setup is shown.

The next step in this flow is the action to do something. Here the action is an action from the connector called “Azure Automation”. The action to do is the “create Job” action. This action allows flow to create automated job in azure. This job is defined through the selected runbook. To setup this the new Action called Azure Automation is created within the flow.

In this setup the used credentials to allow flow to connect to azure are  selected. The mentioned azure subscription is selected. Then the resource group and the account of the needed Automation Action can be selected. The needed runbook for the stop process is selected and in the fields “VM Name” and “ResourceGroupName” the information from the virtual machine which is to processed by this flow has to entered.

The same setup but with another runbook has to be done for the start process.