Auto Scaling Azure Web Apps Vertically (Increase Size and Decrease Size)

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

  1. An Account in your Azure Active Directory that has permissions to your Azure Tenant
  2. The Subscription ID of your Azure tenant
  3. The name of the App Service Plan your Web App is in
  4. The name of the Resource Group your Web App is in
  5. An Azure Automation Account

Once you have that information, we can begin the setup for Auto Resizing your Azure Web App!

The Setup

Creating the Asset

  1. Click on the Azure Automtion Service in the Azure Portal
  2. Click on “All Settings” then scroll down to the “Assets” icon and click on it
  3. Click on the Box that says “Credentials”
  4. At the top, click on “Add Credential”
  5. Fill in the information of the account you will use for Azure Automation
    1. Remember, it also needs to have the appropriate Azure tenant Permissionscred-setup
  6. Click “Create”

Install the Necessary Modules

  1. Scroll back to the Assets blade and click on “Modules”
  2. At the top, click on “Browse Gallery”
  3. You will want to search for the “AzureRM.Websites” module
  4. Click on it and then click Importazurerm.websites
  5. 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

  1. Go all the way back to the Azure Automation service and under resources, click on “Runbooks”
  2. Click “Add a runbook”
  3. Fill out the information and click createrunbook
  4. Once it has been created, click on Edit.
  5. This will bring you to a pane where you will configure the PowerShell Script.
  6. Enter the script below, substituting your own variables (This will be the runbook to enlarge the web app).
  7. 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:
  8. Click “Publish”
    1. 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.

Configure Schedule

  1. On each Runbook, click on “Schedule” under the details section and then click on “Add a Schedule
  2. Click “Link a Schedule to your runbook” and then click “Create a new Schedule”
  3. 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.
  4. Click “Create”
  5. 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.


2 thoughts on “Auto Scaling Azure Web Apps Vertically (Increase Size and Decrease Size)

  1. adeel

    Thank you for sharing this post.
    I have been trying the above mentioned stops but giving me lots of errors.
    “Add-AzureRMAccount : unknown_user_type: Unknown User Type

    At line:2 char:12

    + $account = Add-AzureRMAccount -credential $cred

    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo : CloseError: (:) [Add-AzureRmAccount], AadAuthenticationFailedException

    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Profile.AddAzureRMAccountCommand

    Select-AzureRmSubscription : Run Login-AzureRmAccount to login.

    At line:3 char:1

    + Select-AzureRmSubscription -SubscriptionId “a6f55d29-c550-4eb2-ad58-2 …

    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo : InvalidOperation: (:) [Set-AzureRmContext], PSInvalidOperationException

    + FullyQualifiedErrorId : InvalidOperation,Microsoft.Azure.Commands.Profile.SetAzureRMContextCommand

    Set-AzureRmAppServicePlan : Run Login-AzureRmAccount to login.

    At line:4 char:1

    + Set-AzureRmAppServicePlan -Name “autotestplan” -ResourceGroupName “te …

    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo : InvalidOperation: (:) [Set-AzureRmAppServicePlan], PSInvalidOperationException

    + FullyQualifiedErrorId :


    Can you Pls look into this ASAP.


Leave a Reply to adidas nmd Cancel reply

Your email address will not be published. Required fields are marked *