Azure resources automated scaling as configuration
While Azure Cloud is a great solution for your application and stuff, there's still lack of essential features for building cost-efficient environments. What I mean here?
In software development process, you will need at least two non-production environments:
Those will not be used so frequently as production tier, mostly by development\QA teams. And it's obvious you can save some money for your company by scaling Azure resources down or turning them off completely during night or weekend.
For Virtual Machines, Azure provides Auto Shutdown feature, but there's no auto-startup feature. For SQL databases there's no something similar at all. Every company develops it's own solution to achieve such automated scaling for Azure resources, and I want to share my approach as well.
With the word automated I understand - scheduled.
There are number of advantages of my solution:
- Server-less. The code is running on Azure Application Services as Function App.
- Configuration management approach. Everything is controlled by the code and configuration files. If you implement CI/CD pipelines for the Function, you will be able to track everything in Git repo.
In current state, my FunctionApp allow the following scheduled operations:
- Shutdown\stratup of Azure Virtual Machines
- Scale up\scale down of Azure SQL Databases (standalone)
- Scale up\scale down of Azure PowerBI Embedded capacities
Function App consist of separate functions scheduled to run every 15 minutes (you're able to modify the schedule - it's basic cron expression). Each function is responsible for each type of operation - working with VMs, SQL databases or PowerBI capacities.
Each function has it's own json registry file where you define your desired state of resources. Examples (note they're commented-json files):
As you can see, the schedule property is a JSON-string representing an array, which means that you can specify multiple schedules for each resource. For example - you can make your SQL database to be S0 on Saturday and S1 on Sunday, while the default state (all other days of the week) will be S6.
For quick testing, or if you're not going to put this on CI/CD tracks, you will need:
- Powershell 7
- Azure subscription with App service plan and function app resources created
- Note that for timer triggered function apps in Azure, your service plan has to be of non-free tier (support Always On feature).
- Visual Studio Code with Azure Functions for Visual Studio Code extension
You can get the solution on my Github: https://github.com/vicioussn/azure-scaling-configuration. Some info about solution structure:
- Registry files: https://github.com/vicioussn/azure-scaling-configuration/tree/master/registry
- Helpers: https://github.com/vicioussn/azure-scaling-configuration/tree/master/helpers
- Functions themselves - are in three other folders in repository
After you deploy the code to Azure Function App - remember to update application settings (example - is in the https://github.com/vicioussn/azure-scaling-configuration/blob/master/local.settings.json).
If you have any questions\suggestions - you're free to open issues on Github or post comments here.