Auto assign invoice sections via tag
Hi together!
Today I want to share a billing management thing with you, applicable for all MCA customers!
Basics
If you're in an enterprise company (or maybe mid-size ones), you'll have the need to separate Azure costs between the departments and subsidiaries, who are cause them.
As Microsoft also recognized that need, as IT-costs in the cloud has to be planned completely differently, they applied a specific scheme for billing:

Billing account
A billing account is the highest level of billing within an organization. Basically, it even represents the organization itself, with all its subsidiaries and departments.
Billing Profiles
As large organizations sometimes have multiple invoice recipients, Microsoft divides them into billing profiles.
Each billing profile has its own payment method and invoice address. Normally, it correlates strongly with the legal structure of an organization.

Each box would get its own dedicated billing profile, which is because every company (=legal person) has its own accounting obligation and needs a dedicated invoice.
Invoice Sections
Now, we've reached our level in the billing hierarchy for today! To group all subscriptions on the invoice into multiple sections for each department, you'll need invoice sections. (Except you want to give a report to the accounting department monthly ;) )
A customer of mine requested an automated tag-based assignment of subscriptions to this invoice section. In addition, new ones should be created if they don't exist.
An automatic deletion is not available from the Azure Management API yet (* insert sad face)
Let's get technical
First, I thought about an Azure Automation Account in combination with the Az.Billing module.

Yeah, so... Microsoft decided to let you get all invoice sections but forgot to insert the New-InvoiceSection feature. Thanks for that! :D
As this leads me into handling with APIs, I personally refuse to use PowerShell for that use-case, as it is a configuration tool, not a programming language.
So I've started to build up an Azure Function for that:
To be honest.. it was a hard one.. just wasn't wanted to be :D
The function iterates through all billing profiles in your account and looks for the tag "InvoiceSection" in subscriptions.
For each subscription, it will create the associated InvoiceSection (if it does not exist) and assign the subscription to it.
If there is no InvoiceSection Tag, the function skips the subscription.
Testing
In the GitHub repo, you can find a "Deploy to Azure Button", which deploys the function app to your tenant.
Just a little side story: Interestingly, the API decided to require a Billing Account Name, while the value is in fact the Billing Account Id. (You see why the thing was a bit hard :D)
You can find the Billing Account Id in the Billing Account properties:

Unfortunately, it's not possible to deploy the function itself as code, because you'll need to install the dependencies for the project.
Therefore, you'll need to clone the GitHub project and open it in your VS-Code. Install the Azure and Python extensions and log in to your tenant.

(you can choose between log in via device code or interactively)
After that, you can start giving your subscriptions an Invoice Section Tag:

Then, we're ready!
The function is triggered by a timer, so you'll need to fire a test by either:
- Using the GUI, under Functions -> AssignInvoiceSection -> Code + Test
- Using the Admin API: POST https://<FunctionName>.azurewebsites.net/admin/functions/AssignInvoiceSections
- Make sure you include the APP KEY(!) in the Authorization Header (x-functions-key)

Result
After the function has done its work, we can celebrate that all subscriptions are assigned to their Invoice Sections according to their tags.

Conclusio
The customer is happy about separating all costs efficiently between departments! I'm a bit sad that Microsoft has not implemented invoice sections in CSP billed subscriptions yet... hopefully this will follow one day!
Cheers!