cancel
Showing results for 
Search instead for 
Did you mean: 

Scheduled Start / Stop of Azure VMs to reduce costs.

I think everyone has the same problem: the Azure Virtual machines runs overnight when 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 to setups needed to be setup: Azure Automation Account and the Flow.

 

Azure Automation Accounts

 

recurrence_start_azure_vm_10.PNGThe 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.

 

recurrence_start_azure_vm_11.PNG

 

In the next window the name of the Account and the used Subscription and the name of the resource group is defined.

The Location should be in the same location as the subscription is and for "create azure run as account" should be Yes.

 

recurrence_start_azure_vm_12.PNG

 

After the account is created there the runbooks for start/stop processes has to be defined.

In the Azure Automation Account the runbooks can be found in the details.:

 

recurrence_start_azure_vm_13.PNG

 

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

 

recurrence_start_azure_vm_14.PNG

 

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.

 

recurrence_start_azure_vm_15.PNG

 

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

 

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

 

recurrence_start_azure_vm_16.PNG

 

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

 

recurrence_start_azure_vm_17.PNG

 

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

 

recurrence_start_azure_vm_18.PNG

 

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

 

recurrence_start_azure_vm_19.PNG

 

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.

 

Please be aware that described steps are an template for you but you have to check everything while doing your setup.

 

Microsoft Flow

After the setup in Azure 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% similiar.

Only the runbook name and the recurrence hour are different.

 

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

 

schedule_azure_vm_1.png

 

Now the steps to 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.

 

schedule_azure_vm_2.png 

 

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.

 

schedule_azure_vm_3.png 

 

In this setup the used credentials to allow flow to connect to azure are selected and 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.

 

Please be aware that this is only an example and you have to test it on your own risk. TEst it first on test or development environment and not on production environments.

Hope you enjoy this flow.

Find more on my blog www.dynamiacs.com or follow me on Twitter

Meet Our Blog Authors
  • Experienced Consultant with a demonstrated history of working in the information technology and services industry. Skilled in Office 365, Azure, SharePoint Online, PowerShell, Nintex, K2, SharePoint Designer workflow automation, PowerApps, Microsoft Flow, PowerShell, Active Directory, Operating Systems, Networking, and JavaScript. Strong consulting professional with a Bachelor of Engineering (B.E.) focused in Information Technology from Mumbai University.
  • Encodian Owner / Founder - Ex Microsoft Consulting Services - Architect / Developer - 20 years in SharePoint - PowerPlatform Fan
  • Cambridge UK Power Platform User Group Leader, Technical evangelist and speaker. Always says yes to coffee! #LetsGetCoffee
  • Passionate #Programmer #SharePoint #SPFx #Office365 #MSFlow | C-sharpCorner MVP | SharePoint StackOverflow, Github, PnP contributor