A friend of mine asked whether there were any ‘simple’ techniques to stop / start an Azure VM from a mobile application.
Obviously they could use the Azure Portal directly from the browser app on their phone, but this was deemed ‘not-simple-enough’.
Before we get into the solution, if you want to try this for yourself you will need:
- An O365 Account with Flow and PowerApps enabled.
- An Azure Account with a single VM (v2) (make a note of the Name and it’s ResourceGroup Name).
So, being a big fan of Microsoft Flow I came up with the following Flow Template:
The trigger is simple: ‘when a new item is created in a SharePoint list’. Based on the value of the Action field do something.
To create the Flow Template, lets first start with the SharePoint List.
Here is the SharePoint List:
Just two columns: Title which by default is created when a new SharePoint list is created; Action which I have created as a Choice with two values Stop and Start. That is it, that is the SharePoint List in its entirety. To create the Flow, choose the first Flow Template in the list.
On selecting the template you will be redirected to the Flow website. On editing the Flow Template you will be presented with the following flow:
Remove the last two actions so that you only see the When a new item is created action. Keep Flow open as we will be coming back to it later to complete the template.
Back in SharePoint, now click the PowerApps drop down. By clicking this dropdown, PowerApps will automagically build you a mobile application based on your list. In 2 minutes you have a working mobile application that you can publish and then share. That’s all I am going to cover in PowerApps, the application it builds gives us everything we need. Once you have published the app, go to your mobile device, download the PowerApps app for your device, login with your O365 Account used to create the SharePoint List and you should see the PowerApp created above to open and run. You could even pin the new app to your Home screen thereby bypassing the need to open PowerApps first (PowerApps will open in the background).
So now you have your trigger and your mobile app. All we need to do now is add the actions when a SharePoint List item is added with an action of Start and when an action is added of type Stop. For this I am going to use Azure Automation Runbooks:
To create the Runbook log into the Azure Portal and open your Azure Automation Account. If you don’t have one, click +New and search for Automation. Once your Automation Account is open click on the Runbooks option under Process Automation. For the purposes of this solution we are going to use two Gallery Runbooks, so choose Browse Gallery and search for StartAzureV2Vm and StopAzureV2Vm (Graphical)…for each, choose Import. For more information on how to create or import a Runbook please follow this document.
Once you have both Runbooks in your Account choose one of them and select Edit. For the purpose of this solution we want to stop start a single VM, so edit the Graphical Runbook and edit them so that once the script has connected to Azure, get a single VM only. Your Graphical Runbook should then look something like this:
Publish your Runbooks and try each of them out. You will be asked for the name ResourceGroup of the VM and the name of the Single VM.
If they both run successfully we now want to add a Webhook for each Runbook so that we can run the Runbooks remotely via our Flow template. For information on how to o create the Webhook for each Runbook please follow this document. Note, you only get the option once to copy the Webhook Url so make sure you click the Copy button. For ease, I pre-populated the RESOURCEGROUPNAME and VMNAME. As a strectch goal, you may want to try passing these values as part of the request – to do that you should add 2 extra columns to your SharePoint List so that once you have re-created the PowerApp you will have the option of entering the names which you can then use as part of your Flow.
Once you have the Webhooks for the Stop and Start Runbook, it’s now just a case of completing the Flow Template.
Following the Trigger, create a Condition. The Condition then is based on the Action Value if it is equal to Start then do the If yes Action, else do the If no Action. The Action is simply a HTTP and choose the Method POST. The Url is either the Start or Stop Runbook Webhook Url you created previously.
Voila! You can know use a mobile app to stop start your Azure VM.
This is obviously a very simple case, but hopefully it gives you food for thought on how you can use a mix of O365 and Azure Automation to manage your Azure Assets.