Automate Azure Resources with PowerApps
Microsoft’s Power Platform is a low code platform which enable the organization to analyse data from multiple sources, act on it through application and automate business process. Power Platform contains 3 main aspects (Power Apps,Power BI and Microsoft Flow) and it also integrates two main ecosystems(Office 365 and Dynamics 365). In this blog we will see in detail about Power Apps which helps to quickly build apps that connects data and run on web and mobile devices.
We wills see how to build a simple Power App to automate scaling of resources in Azure in a simple yet powerful fashion to help save you money in the cloud using multiple Microsoft tools. In this post, we will use Azure Automation run books to code Azure Power Shell scripts, MS Flow as orchestration tool and MS Power Apps as the simple interface. If you have an Azure Environment with lot of resources it becomes hassle to manage the scaling part of it if you don’t have the auto scaling implemented. The following Power App will help to understand how easy it is to build the above scenario.
We will use the following Azure resources to showcase how scaling and automation can save lot of cost.
- AppService Plan
- Azure SQL database
- Virtual Machines
Step 1: Create and Update Automation Account :
First we need to create the Azure Automation account. Navigate to Create Resources -> search for Automation Account on the search bar. Create a new resource as follows,
Once you have created the resource, you also need to install the required modules for this particular demo which includes
Step 2: Create Azure PowerShell RunBooks
Next step is to create the run books by going to the Runbooks blade, for this tutorial lets create 6 run books one for each resources and its purpose. We need to create PowerShell scripts for each of those types
Scale Up SQL
-Scale Up ASP
-Scale Down SQL
-Scale Down ASP
PowerShell scripts for each of these are found in the Github repository. We need to create runbook for each of the scripts.
All the runbooks above can be scheduled and automated to run for desired length of time, particular days of the week and time frame or continuously with no expiry. For an enterprise for non-production you would want it to scale down end of business hours and at the weekend.
Step 3: Create and Link Microsoft Flow with Azure Automation Run books
Once we tested with the above Powershell scripts and the runbooks, tested and published now we can move on the next step to create the Flow. Navigate to Flow and Create New App from the template.
Select the template as PowerApps Button and the first step we need to add is the automation job. When you search for automation you will see the list of jobs available. Select Create Job and pick the one you created above. If you want to all the actions in one app, you can add one by one, If not you need to create separate flows for each one.
In this one, i have created one with the ScaleUpDB job which will execute the Scale up command of the database.
Once you are done with all the steps save the flow with necessary name.
Step 4 : Create Power Apps and and link to your Flow button
Once we create PowerApp flow buttons login to MS PowerApps with a work/school account. Navigate to Power Apps which will give a way to create a blank canvas for Mobile or tablet. Next you can then begin to customise the PowerApp with text labels, colour and buttons as below
In this case we will have a button to increase/decrease the count of instances of the sql db, my app looked like below with few labels and buttons.
Next is to link the flow to the button of the power App by navigating to the Actions -> Power Automate
Once both Scale Up/Scale down actions are linked, save the app and publish
Step 5 : Verify Automation
Inorder to verify if things are working correctly, click on scale up and scale down few times and navigate to Azure Portal and open the Automation account we created.
Navigate to the overview tab to see the requests for each job made via the power app as below.
In order to look at the history navigate to the Jobs blade
further you can build a customized app for different environments with different screens using Power App. With the help of Azure Alert, whenever you get an alert regarding the heavy usages of resources/spikes, with single click of button you will be able scale up and scale down the resources as you need.
Improvements and things to consider:
This is just a starting point to explore more on this functionality, but there are improvements you could add to make this really useful.
(i) Sometimes the azure automation action fails to start the runbook. When you are implementing flow needs to handle this condition.
(ii) Sometimes a runbook action will be successful, but the runbook execution errored. Consider using try/catch blocks in the PowerShell and output the final result as a JSON object that can be parsed and further handled by the flow.
(iii) We should update your code to use the Az modules rather than AzureRM.
Note : The user who executes the PowerApp also needs permission to execute runbooks in the Automation Account.
With this App, It becomes handy for the operations team to manage the portal without logging in. Hope it helps someone out there! Cheers.