Azure Cost management
Cloud services offer great flexibility but come with the drawback of potentially high costs, especially with pay-as-you-go pricing. Recently, I encountered a scenario with numerous orphan resources. When I got rid of them it saved enormous amount. It is a common occurrence where due to misconfiguration of infrastructure generation script new resources are created over the time. Such possible issues led us to thinking how we can provide the visibility of costs and resources to all the team members.
Ideas
- Generating monthly resources report.
- Adding cost alerts.
These are the default methods for dealing with such issues. They do have their own merits and demerits. But in our case, it didn't really make sense as most of our resources and environments are development and testing environments. Where new resources are being created and destroyed frequently. This poses a unique challenge where it doesn't really make sense to add cost alerts to multiple resources. Adding cost alerts at the subscription level might not have been effective due to the unpredictable workflow and varying resource sets.
Generating a monthly report fails to address the use case, as the resources created may not be persistent or linearly growing.
Solution
Keeping all this in mind. And most of the organization being heavily invested in the Microsoft Business tools. One of these tools is Microsoft teams which a lot of department and employee uses for their daily communications needs. Leveraging this I decided to come up with a solution that will generate a daily report at the end of the day and push in teams channel associated with it. This was primarily intended to make sure all members working on that azure subscription are notified and aware of the resources they created and what is the cost associated with them. While Teams is used as an example here, the solution can easily be adapted to other tools like Slack.
Development phase 1: (Discovering what we want)
The phase one of the development focused on the getting a prototype ready for the demonstration. It included the format and the costs that we wanted to show in teams. As every testing started and ended with resource group generation and termination respectively a neat organization feature provided by Azure came handy where all the related resources can be stored in a single resource group. We decided on a simple table where all the resource groups are listed in a subscription and a monthly and daily cost is showed for each resource group. with a daily and monthly total cost in the top.
# Azure Costs Report [2023-08-22] Last 24 hours: 14,846.16 JPY, Total cost this month: 399,002.93 JPY | Resource Group | Cost (Last 24 Hours) | Cost (Month to Date) | | -------------- | -------------------- | -------------------- | | [Redacted] | 6,819.16 JPY | 151,496.92 JPY | | [Redacted] | 3,745.15 JPY | 106,760.04 JPY | | [Redacted] | 894.73 JPY | 27,233.58 JPY | | [Redacted] | 762.08 JPY | 23,043.09 JPY | | [Redacted] | 547.12 JPY | 17,682.12 JPY | | [Redacted] | 544.38 JPY | 15,643.30 JPY | | [Redacted] | 465.56 JPY | 12,880.44 JPY | | [Redacted] | 0.00 JPY | 12,696.02 JPY | | [Redacted] | 293.17 JPY | 8,141.49 JPY | | [Redacted] | 82.49 JPY | 7,501.96 JPY | | [Redacted] | 203.77 JPY | 4,671.38 JPY | | [Redacted] | 90.72 JPY | 2,577.62 JPY | | [Redacted] | 59.51 JPY | 1,646.33 JPY |
Development phase 2: (Which resources to use)
While deciding which resources to use most of them were pretty straightforward choices. Due to our setup scenario being a typical azure infrastructure. we didn't have to contemplate much.
Resource Group: As stated, before every project starts and end with resource group, we decided to create a resource group where all the resources we will need can be created inside this resource group making it easier to delete and maintain.
Azure Functions: As we had a requirement of generating these reports once a day it made no sense having a Virtual Machine running all the time. Serverless Functions seemed like best solution for this scenario. (Which works great for small use case, but we later realized it's not a perfect solution. Though it is true for everything. We can discuss that in some other post.)
Teams Webhook: We can create a channel and an Incoming Webhook for the same which can be used to send messaged in the channel using HTTPS POST called from the function.
Here is our architecture that we decided in the end.
Unintended benefits
- Collaboration: As a single person it is much easier to overlook these costs. and if they are untracked it might go unnoticed when your organization is spending millions of yen every month in azure resources. Given that we manage a large volume of customer resources, it's easy to overlook development and unnecessary costs. But when these costs are shared among team members there are multiple people looking at these costs and if something is sticking out which is unusual or not needed. they can be easily pointed out.
- Accountability: As everyone can see the cost it also makes every individual more aware of things that they are doing. There is no Consequences if they costs is high it is understandable that a lot of new workforce joins the organization, and these slip ups are going to happen, but it makes them think about the cost at the back of their mind.
Afterword
So far, we have learned the need of developing such a tool and why it might be important for a team or a small-scale company. The implementation and diving in deep for its code will be followed in the next part.