Woocommerce Menu

Auto scale up and down Azure Database depending on usage timeline

This is sub post of main post Azure Resource Cost Optimization. It’s better if you start reading from main post. If you simply want to know how you can switch between different tires automatically depending on time line, you can straight way jump into this post.

Most simplest way to scale up and down db automatically depending on time line of the day, is creating runbook under Azure automation account and associate PowerShell script with it. In order to fallow this mechanism, you should aware on traffic patterns of the day of the db. As an example as a developer, you can auto scale up and down your DB in Dev, QA and UAT environment depending on development time line of the day and the week for sure.

When it comes production environment, if you are aware on traffic pattern of the day then simply you can follow this mechanism . Otherwise you might need to scale up and down depending on CPU usage and that can be done using features exposed through Azure Monitor + Azure Runbook. I will explain that in different post.

Lets jump into implementation

  1. Create Azure Automation account here
  2. Create a Azure runbook with run book type PowerShell under created Automation account
  3. Create run as account under Azure automation account that you created. Run as account is needed to give access to runbook when accessing Azure resources of your subscription.
  4. Add this PowerShell script to the Runbook you just created and set below default parameters.
    You can find time zone that you are planning to run your runbook by querying DB “select * from sys.time_zone_info”. “Default Edition” and “Default Tier” should be the set to the default scale that should be set, when running runbook outside of “scalingSchedule” that you set. Sample “scalingSchedule” that you can set for Dev, QA and UAT environment is “[{WeekDays:[1,2,3,4,5], StartTime:”07:59:59″, StopTime:”17:59:59”, Edition: “Standard”, Tier: “S0″}]”. Then when you run your run book at 8am and 6pm, database is scale up to S0 at 8am and scale down to Basic when you run it at 6pm.
  5. Then you can run the runbook manually by setting up parameters as per your environment and make sure that it runs in your environment as expected. Please make sure, when you are setting server name, set the server name without “database.windows.net”.
  6. Create schedules at Schedules blade of Azure Automation account as per your time line. Sample time line could be as below
  7. Associate schedules you just created with Runbook at Schedules blade of Runbook and set parameteres as per your environment.
    Sample schedules could be as fallows.
  8. That’s it. 🙂

Leave a Reply

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