Tagging of resources in Azure can be a pain, especially if you have heaps of resources deployed. Tagging is used primarily for billing purposes and tags applied to the resource group are not inherited by the resources in that resource group. What’s more, if you want to chargeback Azure consumption to the business across various business units, tagging is the best way to do it.
There’s a few of ways you can chargeback to business units by using tags:
- Download or view your Azure billing invoice and daily usage data
- Connect to Microsoft Azure Enterprise with Power BI
- Cloudyn which has showback/chargeback reports
However, if you follow the same methodology of separating Resource Groups per distributed application as per the Azure enterprise scaffold, life could all of a sudden get much easier.
If you have done this and assuming from a resource group level you have:
- grouped your Azure resources as above on a per application basis
- tagged all your Resource Groups
- grouped your Azure resources as per above on a per application basis
… then you can contunue with this blog post so you can easily chargeback Azure costs to business units on a resource group by resource group level.
This script will:
- apply all tags from a resource group to its child resources
- retain existing tags on resources that are not duplicates
- and not overwrite any duplicate tags
From here I have taken a script and have it below, you simply take this below and run this script in an Azure Automation runbook (PowerShell script) and have it scheduled to be run on a daily/weekly/monthly basis depending on how much your Resource Groups change. All it will do is as described above and update child resource tags. Even if this script runs over and over again, it won’t make any changes unless it needs to update child resources. If child resources have the same tags as the parrent resource group, it will simply skip over all resources and finish.
To run this script automatically, you will need to set this up in Azure Automation. For this, you will need an Azure Automation Account (setup by following this guide) and an Azure Automation Run As account, which can be setup by following this guide.
Create an Azure Automation runbook, paste this in and schedule it to be run.