Azure Web App Scaling
One of the common myths with Cloud computing (whether it be AWS, Azure, or someone else) is that it is cheaper than having it on premise. Cloud computing is fantastic at easily and quickly scaling resources to meet demand at peak times and scale back when it is not necessary to reduce operational cost. However, it is going to be more expensive. This is generally because the host is taking on much of the operational work that goes into it and provides you with the tools to configure it to meet your exact needs.
Azure Web Apps are on such service that meets a lot of needs customers have without having them create and maintain an IIS server themselves. Microsoft makes it easy to quickly and easily create more instances to meet user demand of the website. They also take out the work of load balancing between the multiple instances and you are able to reduce the instance count once demand has subsided. We call this ‘scaling sideways’ as you are adding more of the same instances. You obviously incur a cost for the additional instances, but auto scaling allows you to scale back when you dont need to meet that demand.
Customer Requirement: Auto Scale the Azure Web App to a Smaller Size During Non-Business Hours
If the Web App can automatically scale from 1 to 4 instances on a schedule, certainly Microsoft would provide a way to scale the Web App from a small instance to a larger one and vis versa?
The reality of this is no (at least at the time of writing this post). It seems like such a simple thing Microsoft could provide us with the ability to do within the same tools that allow us to add instances and subtract them. Sadly no. But, all hope is not lost, and there is a way for us to do it using my favorite tool….PowerShell!
Azure Automation to the Rescue!
Azure Automation allows us to not only shrink and enlarge our Azure Web App, but also allow us to do it on a schedule of our choosing. To do this, you’ll need to note a few things
- An Account in your Azure Active Directory that has permissions to your Azure Tenant
- The Subscription ID of your Azure tenant
- The name of the App Service Plan your Web App is in
- The name of the Resource Group your Web App is in
- An Azure Automation Account
Once you have that information, we can begin the setup for Auto Resizing your Azure Web App!
Creating the Asset
- Click on the Azure Automtion Service in the Azure Portal
- Click on “All Settings” then scroll down to the “Assets” icon and click on it
- Click on the Box that says “Credentials”
- At the top, click on “Add Credential”
- Fill in the information of the account you will use for Azure Automation
- Click “Create”
Install the Necessary Modules
- Scroll back to the Assets blade and click on “Modules”
- At the top, click on “Browse Gallery”
- You will want to search for the “AzureRM.Websites” module
- Click on it and then click Import
- Click on “OK”Note: You may get an error that says you need to install the AzureRM.Profile Module first. If that is the case, simply repeat the steps above, but instead of “AzureRM.Websites” use “AzureRM.Profile”
Create the Runbook
- Go all the way back to the Azure Automation service and under resources, click on “Runbooks”
- Click “Add a runbook”
- Fill out the information and click create
- Once it has been created, click on Edit.
- This will bring you to a pane where you will configure the PowerShell Script.
- Enter the script below, substituting your own variables (This will be the runbook to enlarge the web app).
PowerShell1234$cred = Get-AutomationPSCredential -Name 'automationcred'$account = Add-AzureRMAccount -credential $credSelect-AzureRmSubscription -SubscriptionId "XXXXXXXX"Set-AzureRmAppServicePlan -Name "MyAppServicePlan" -ResourceGroupName "MyRG" -Tier Standard -WorkerSize Medium
- Repeat steps 1-6 but instead of a Medium instance, change the last line in the script to be small. It should look like the one below:
PowerShell1234$cred = Get-AutomationPSCredential -Name 'automationcred'$account = Add-AzureRMAccount -credential $credSelect-AzureRmSubscription -SubscriptionId "XXXXXXXX"Set-AzureRmAppServicePlan -Name "MyAppServicePlan" -ResourceGroupName "MyRG" -Tier Standard -WorkerSize Small
- Click “Publish”
- Note: You can also run it in the Test Pane. Do know that this will actually run the job and change the size of the Web App.
- On each Runbook, click on “Schedule” under the details section and then click on “Add a Schedule
- Click “Link a Schedule to your runbook” and then click “Create a new Schedule”
- Configure the start time and when it should reoccur. For this example, I’ll have it run at 6:00:00 and reoccur every 24 hours. This way, this particular job runs every day at the same time.
- Click “Create”
- Click “Ok” on the Schedule Runbook blade
That’s it! Once you do that for both runbooks, you are set to scale your Azure Web App in Size.
This script does scale the App Service Plan that the Web App is in, so if you have multiple Web Apps in the same App Service Plan, they will all change size accordingly.
Things to Consider
While it may be a tempting idea to say “I dont need my Web App to be a S1 size, I can make it a Basic size to save even more money!” you’d be putting yourself at risk for losing out on some of the configuration features that come with the Standard series (like SSL Certs, Storage Amount, etc.) Scaling down that far may cause big issues for your Web App.
You will also want to consider the run time of all your Azure Automation jobs. If you are trying to leverage the free tier of Azure Automation to accomplish this task, you will need to keep it under 500 minutes/month. That is easy for this job alone, but other jobs you may have configured could eat into that limit and you risk incurring costs in the basic tier.