We found the limit the hard way. One subscription held all of our Terraform backend storage accounts, and we ran out of RBAC role assignments. Pipelines started failing with errors that didn't obviously say "you're at 4,000." That subscription is also why we later moved those 978 storage accounts. The Function I'm describing here was the immediate stopgap: watch every subscription, and open a ticket before we hit the ceiling again.
Azure subscriptions have a hard limit of 4,000 RBAC role assignments. Inherited assignments from parent management groups don't count against it. In our environment, Key Vaults are a big driver of the count. Application components get their own vaults, they run in multiple regions and four lifecycle environments, and each vault needs assignments for managed identities, service principals, and automation.
A weekly check
I wrote a Python Azure Function (v2 programming model) that runs every Tuesday at 9:00 AM UTC:
- Crawls subscriptions under our management group hierarchy
- Counts role assignments in each subscription, excluding inherited ones
- Alerts at 80% usage, which is 3,200 of 4,000
- Creates an Azure DevOps ticket with the subscription details and the remediation steps we use
The 80% threshold leaves room to keep operating while we plan. We still have a landing zone pattern that loops through Key Vaults with a single provider configuration per environment, so splitting vaults across subscriptions mid-flight is messy. It's better to notice early.
Avoiding duplicate tickets
Every ticket gets the tag ccoetools-rbac-quota-usage. Before creating a new one, the function queries Azure DevOps with WIQL. If an open ticket already exists for that subscription, it skips. If a ticket was closed in the last seven days, it also skips, so a temporary dip below the threshold doesn't immediately open another item.
Some subscriptions we don't want tickets for, either because they're already being cleaned up or because they're staying where they are. Those go in an EXCLUDED_SUBSCRIPTIONS environment variable.
Scope is also configuration, not code. MANAGEMENT_GROUP_IDS controls which hierarchies we scan.
The function infers Dev, Test, Staging, or Production from the management group name and tags the ticket accordingly, which helps with routing and priority.
lifecycle_map = {
"development": "Dev",
"test": "Tst",
"staging": "Stg",
"production": "Prd",
}
What it uses
- Azure Functions (Python v2), timer trigger
- Azure Resource Graph for discovering subscriptions
- Azure Management API for counting role assignments, including pagination
- Azure DevOps REST API for searching and creating tickets
- Managed identity, no stored credentials
A ticket includes the subscription name and ID, the current count and percentage, remediation steps (we have a script that cleans up orphaned assignments from deleted service principals), and a link for updating enterprise application templates if we need to rotate to a new subscription.
DRY_RUN logs what the function would do without creating tickets, which I used before turning it on.
The schedule is 0 0 9 * * 2. Tuesday morning is early enough in the week to act on a ticket before weekend change freezes, without landing on Monday's backlog.
It's caught subscriptions approaching the limit while we still had time to clean up assignments or plan a move. The storage migration is what we did when watching the quota wasn't going to be enough.