Cost monitoring and analysis
If you are an Azure administrator, you are probably responsible for monitoring and managing the cost of all services hosted in the cloud. There are many factors related to this particular case—the types of used resources, the scale of your projects, or different discounts that you may apply, depending on the contract you have with Microsoft. Azure offers different options to make your life easier—starting from easy-to-read dashboards to cost alerts, which help you to monitor the current usage. In this section, you will learn how to use those tools and understand their outcome.
Before you really get started with hosted services, you can estimate the cost of the architecture using the following calculators:
- Pricing calculator: An Azure cost calculator, which can be found at https://azure.microsoft.com/en-us/pricing/calculator/, it is a tool that you can use to estimate how much each Azure service will cost. Of course, these calculations are only estimates as it is really hard to plan everything upfront. Nonetheless, treat it as the first step in planning funds for your architecture.
- Total Cost of Ownership (TCO) calculator: This is another Azure calculator, which is available at https://azure.microsoft.com/en-us/pricing/tco/calculator/. Using the cloud is not only about using cloud services, but also about changing the responsibilities and moving expenses from one place to another. This calculator helps you to understand the total cost of your architecture including managing server infrastructure, updates, licenses, and many more.
The preceding tools are great to understand the expected cost of the whole cloud architecture that we are about to manage. However, they require that you know how each service is configured and what features will be enabled. Doing this upfront may be tricky, so they are not always an ideal solution for managing the cost. This is why we will have to take a look at the real usage and calculated cost to be able to control it.
Cost management is enabled by default on all subscriptions—all you need here is to access the correct section in the Azure portal:
- To access the cost management option, search for it in the search field at the top of the portal:
From this point, you can access different blades such as Cost analysis, Budgets, and Cloudyn. When you enter the subscription screen, you should be able to see a screen similar to mine:
Let’s focus quickly on the information displayed here. We have two categories, which inform us about the current cost of the subscription:
- Cost by resource: This chart displays the total cost of the subscription divided by the resources. As you can see, in my case, almost 90% of the cost is generated by a resource named kamzcosmos (which is probably an Azure Cosmos DB instance).
- Spending rate and forecast: This is an interesting chart that gives you an insight into the forecast of your spending. It also allows you to see how dynamically the cost changes.
When you go to the Cost Management, you will see a new blade where multiple features are available:
The available options will be briefly described in the following.
To get a better overview of how much each resource costs (or a resource group or a location), you can use Cost analysis to get a personalized view of different spending categories. Besides the main chart representing the accumulated view, you will have access to three additional charts, which you can alter to get a different categorization of resources:
There are many interesting categories that you can use to understand the cost—you can divide services using tags, their tiers, invoice number, or even their GUIDs. If you have many resources, this becomes especially helpful as it allows you to use advanced filtering and better distinction.
Microsoft Azure allows you to create budgets, which you can use to control the cost of the cloud services. To use this feature, you have to go to the Budgets blade and click on the + Add button. Doing so will display a form that you can use to set a budget with an alert, which will trigger if the current cost of your subscription exceeds the threshold:
In the preceding example (Figure 1.35), I have created a budget of 90 USD with an alert that will trigger if I spent at least 90 USD.
There is an additional feature of budgets that, from your perspective, should be very interesting. As you have probably noticed, you can divide your budget into many categories, each triggering another kind of action group. Action groups can be managed by clicking on the Manage action groups button:
They allow you to enhance your budget with an additional level of automation using services such as Azure Functions, Azure Logic Apps, or Azure Automation to take a specific action in addition to sending an alert. Here, you can find an example with a runbook, which will stop all virtual machines in a resource group:
Once a budget is created, you can see it in the main window of the feature:
Here, you can find an example mail triggered by defined alert rules. Note that it contains all of the necessary details you need to understand what is happening—when the budget started, what is its maximal value, and the current state:
Such an email can be really valuable, especially when limiting expenses is crucial for a business to run smoothly. The important thing here is that you should not rely on a single channel of communication only—the email message could get lost or your mailbox might have gone down—if the budget alert is really important, always implement a backup plan for it.
In most cases, the Azure portal features should fit most of your needs. One more thing worth mentioning is Azure Advisor, which you can find in the Cost Management blade:
By clicking on it, you may find helpful tips related to the cost optimization for your subscription. If you have many different resources, it may be worth checking once in a while whether you have missed some occasions for saving extra money by tweaking provisioned resources.
When you set alerts via budgets in the cost management of your subscription, you will get an email each time you reach the threshold. As in most cases, you will not be the only administrator; a group of people will be notified to take a look and check which resources are utilizing the budget the most. You will find this feature really helpful, especially if you have a strict requirement when it comes to cloud cost. By adding action groups, you can plan automated saving based on the rules you define. We can think of an example here:
- When you reach 75%, you send an email to all administrators.
- When you reach 85%, you can run a script that will scale down Dev/Test environments.
- When you reach 90%, you send an SMS to all administrators, send an email to all engineers, and shut down Dev/Test environments.
With such flexibility, you can think of several scenarios that will be appropriate to your current workloads and the characteristics of your systems.
You just learned about budgets and how to configure them to monitor your resources. Let’s now continue with other automation solutions that may help you to keep an eye on the Azure services and applications you manage.