You have a few VMs on Azure and not ordered to be accessible at 24/7. However, you regularly forget to turn it off after use. As you know, keep the VMs running idly on Azure is wasted. So if you are looking for a tool that helps you to turn off your VM without login to the Azure portal? Then this post is for you. You might know about the Microsoft Flow application that allows automating the manual actions. So get used of the Flow, I would like to show you how to create a button on Flow to start and stop your Azure VMs.
1. Setup Automation Account and Runbook
Runbook is a helpful feature that allows automating the manual tasks in the Automation Account. So get used of this, the following steps will show you how to create and integrate the Runbooks with Flow. You shall found more information about the Runbooks here.
2. Implement Runbooks
To running a Runbook the Automation account need to be established. Here I will create a new Automation account named HbdAutoAccount.
Navigate to Runbook Gallery, look up and import 2 Runbooks Start Azure V2 VMs and Stop Azure V2 VMs with following names Start-Steven-PC and Stop-Steven-PC.
Now, I will have 2 Runbooks in the automation account as below screenshot.
3. Create Webhooks
Turn into each runbook, click Edit and Publish buttons before using it, because Azure is not allowing to run the unpublished Runbooks. If any change on the Runbook, remember to publish it again to get effective.
Next, go to the Webhooks position of Runbook and create a hook with the following information.
- Name: Start Steven PC
- Enabled: Yes
- Expires: 1 year from today.
- Resource Group Name: VMs, the resource group of VM.
- Name: Steven-PC, the VM name.
Create the same hook for Stop-Steven-PC. After 2 Webhooks had been set up, I have 2 URLs as below. These URLs will be used to create two buttons on Microsoft Flow.
- Start Steven PC: https://s3events.azure-automation.net/webhooks?token=q5F3KX1nRol1vjgkIz61NRLj6T%2fxNCQOeC4jDunBQaA%3d
- Stop Steven PC: https://s3events.azure-automation.net/webhooks?token=q5F3KX1nRol1vjgkIz61NRLj6T%2fxNCQOeC4jDunBQaA%3d
4. Create the start and stop buttons on Flow.
1. Create Start and Stop buttons on Microsoft Flow.
After logged into Microsoft Flow (if you don’t have Flow account just create one, it is free). Click on My Flow and Create From Blank then select Flow button for Mobile with action is HTTP:
- Flow name: Start Steven-PC
- Http method: POST
- URL: the start Steven PC URL had been established previously.
Then click Create Flow button. After this step, I will have a Mobile Button on Flow to start the VM. Following the same for the stop button.
Download Microsoft Flow for iPhone, and now I can Start and Stop my VM on Azure by using my phone directly.
2. Enable the auto shutdown of Azure VM.
Moreover, the Azure VM has the other feature so-called Auto Shutdown. It will help to shut down the VM during certain time periods. In here, the shutdown time of my VM is 1:00 am. So in case I missed, It will shut down my VM at 1:00 am every day automatically.
- With Flow button you can able to start and stop a set of VMs at the same time by adding the HTTP actions contexcturely.
- As I have openned 2 webhooks for Flow buttons this is a Security gap so shiuld not disclose the hook URLs to anyone even your wife.
- The expire date of the hooks should keep it as short as manageable and when it is expired, you should create a new one and update the URL back to the Flow buttons.
- Recently, the Azure for mobile has been released that allow you to manage Azure Resources on the go. However, using that tool is required the Azure Subscription. In case you want to release the VM for someone that they don’t have subscription, Flow is still useful for you.
The Vietnamese version here.