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:

  1. Find Azure connector for flow
  2. Add task in Flow to Start Azure VM
  3. Wait for x hours
  4. Add task in Flow to Stop and deallocate Azure VM
  5. 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 Screen grab of flow connector for ARM

I hit an issue where I got blocked by the authentication in Azure with an error access token is from the wrong issuer: Screen grab: error 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

  1. Open the Azure Portal
  2. Find your virtual machine
  3. Open Access Control (IAM)
  4. Add “Virtual Machine Contributor” as shown below: Screen grab: add IAM roles for service principal to VM

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

  1. Create a new flow using an HTTP trigger http-get1.jpg

  2. Specify GET - this makes it easy to call from a shortcut http-get2.jpg

Initialise some variables

  1. VmShortResourceId e.g. virtualMachines/v-stegg-win10 VmShortResourceId.jpg

  2. VmOnTimeMins e.g. 240 VmOnTimeMins.jpg

  3. strDelayUntil - a calcuated time in the future by adding mins above using expression addMinutes(utcNow(), variables(‘VmOnTimeMins’)) strDelayUntil.jpg

Setup the Start of the VM

  1. Add a new Azure Resource Manager Invoke Action invoke-rm-start0.jpg

  2. Add connection using new principal connect-with-sp.jpg

  3. Enter service principal details connect-with-sp2.jpg

  4. Use the variable and values similar to below invoke-rm-start.jpg

Add a delay

We want to wait a while to allow the VM to be used then turn it off.

  1. Add a new action delay-until1.jpg

  2. Select the delay until then add in the variable delay-until2.jpg

Add a step to Stop the VM (Deallocate)

  1. Similar to before - ARM invoke invoke-rm-stop.jpg

Test

Once you have saved the flow you can get the trigger Url in flow and enter it into your web browser http-get-url.jpg

You can see the flow has been executed by selecting the Activity Log blade for the VM azure-vm-activity-log.jpg

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

  1. Add a physical IoT button (bttn) - just need to get my hands on one of these!
  2. 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
  3. Add a bot to start this from Cortana e.g. “Hey, Cortana… turn on my dev box for 4 hours”