About convenience and automation
I love that Azure has the capability to start and stop my Virtual Machines on a schedule using DevTestLabs. There are some great use cases of DevTestLabs for developers and some great use cases for training
DevTestLabs has some great features but as an automation advocate I had a new requirement:
As a user (forgetful, busy nerd) I want a button on my mobile to turn on a VM for 4 hours and auto shut it down so that I can do some work and then the VM is not left running.
What could I use to meet this requirement? I had played around with Microsoft Flow a little and figured out I could make buttons on the homepage of my Android phone.
Here are the tasks I decided were involved:
- Find Azure connector for flow
- Add task in Flow to Start Azure VM
- Wait for x hours
- Add task in Flow to Stop and deallocate Azure VM
- Add a trigger from my mobile Home Screen
It all sounded plausable and I thought this would be easy since I found the ARM connector for flow
I hit an issue where I got blocked by the authentication in Azure with an error access token is from the wrong issuer:
I figure this is something to do with the way flow integrates with Live ID rather than an Azure AD tenant so I decided to try an Azure Service Principal.
To create a service principal in Azure AD
There is a step by step guide for creating a new service principal in Azure.
Be sure to retain the password (key) which will be similar to AAfBBQ5TADuc1dBn1Quqy3g4ABBT+fWzXT0ZYo=
To give your new service principal permissions to your VM
- Open the Azure Portal
- Find your virtual machine
- Open Access Control (IAM)
- Add “Virtual Machine Contributor” as shown below:
Note: I also gave the principal Reader on the subscription which may have been required to select the Resource Group. I prefer working on least priviledge so VM permissions would be preferred.
Create new Flow with HTTP trigger
-
Create a new flow using an HTTP trigger
-
Specify GET - this makes it easy to call from a shortcut
Initialise some variables
-
VmShortResourceId e.g. virtualMachines/v-stegg-win10
-
VmOnTimeMins e.g. 240
-
strDelayUntil - a calcuated time in the future by adding mins above using expression addMinutes(utcNow(), variables(‘VmOnTimeMins’))
Setup the Start of the VM
-
Add a new Azure Resource Manager Invoke Action
-
Add connection using new principal
-
Enter service principal details
-
Use the variable and values similar to below
Add a delay
We want to wait a while to allow the VM to be used then turn it off.
-
Add a new action
-
Select the delay until then add in the variable
Add a step to Stop the VM (Deallocate)
- Similar to before - ARM invoke
Test
Once you have saved the flow you can get the trigger Url in flow and enter it into your web browser
You can see the flow has been executed by selecting the Activity Log blade for the VM
Setup the button on your phone
On an Android phone you can go into Chrome, browse to the Url and then select Add to Home Screen
On a device without Chrome or Apple then you could add the Flow Apps: iOS Android
Done.
I also made a Video of this (sorry - no sound) that may help you follow the process
Next steps
- Add a physical IoT button (bttn) - just need to get my hands on one of these!
- Consider how to block kicking off the flow twice - Azure doesn’t error if you start a VM which is running so it’s not a big issue
- Add a bot to start this from Cortana e.g. “Hey, Cortana… turn on my dev box for 4 hours”