Know your Azure Quota Status

If you are using an Azure Pay-As-You-Go subscription, understanding the subscription limits and quotas are very important. Example, if your subscription has a vCPUs (cores) limit of 20, and if you have an automated pipeline to deploy Virtual machines, and if you have already used the provided quota, the pipelines will fail because there is no more vCPU quota available to you.

Hence it is mandatory as an Azure Consultant for you to understand the subscription limits. The subscription limits comes in two flavors – Soft and Hard. If you hit a SOFT limit, you can contact Azure Support to increase the limit by providing a valid business justification.

As a cloud provider, Azure offers many services and are tagged under ProviderNamespaces. Example, ‘Microsoft.Compute’, ‘Microsoft.Storage’, etc. Keeping track of your Subscription Capacity for each resources is difficult. And certainly very difficult to automate it.

However, Azure has introduced Quota API, which is generally available from Jan 19, 2021. You can use it for your automated Quota management. That is, you can query your current Subscription capacity consumption and order more quota using this API.

As an example, I will show you how to use this REST API using Powershell. We will query the ‘Microsoft.Compute’ provider to check what is the limit and current usage.

  1. First, you need to make sure your subscription is registered for the ‘Microsoft.Capacity’ provider.

  2. We need to authenticate to your Azure subscription. Hence create an App Registration and provide access to the subscription. Once this is done, grab the ‘ClientID’, ‘Client Secret’, and ‘Tenent ID’
    $TenantId = "<ENTER YOUR TENANT ID>"
    $ClientId = "<ENTER YOUR CLIENT ID>"
    $ClientSecret = "<ENTER YOUR CLIENT SECRET>"
    $Resource = "https://management.core.windows.net/"
    $SubscriptionId = "<ENTER YOUR SUBSCRIPTION ID>"


  3. Get access token by using the above credentials:
    $RequestAccessTokenUri = "https://login.microsoftonline.com/$TenantId/oauth2/token"
    $body = "grant_type=client_credentials&client_id=$ClientId&client_secret=$ClientSecret&resource=$Resource"
    $Token = Invoke-RestMethod -Method Post -Uri $RequestAccessTokenUri -Body $body -ContentType 'application/x-www-form-urlencoded'


  4. Now query the Quota API for ‘Microsoft.Compute’ provider. The result will show you how much capacity is already used and what is the limit.
    $location = 'westeurope'
    $resourceProvider = 'Microsoft.Compute'
    $uri = ("https://management.azure.com/subscriptions/{0}/providers/Microsoft.Capacity/resourceProviders/{1}/locations/{2}/serviceLimits?api-version=2020-10-25" -f $SubscriptionId, $resourceProvider, $location)
    $Headers = @{}
    $Headers.Add("Authorization","$($Token.token_type) "+ " " + "$($Token.access_token)")
    $Capacity = Invoke-RestMethod -Method Get -Uri $uri -Headers $Headers


  5. Now, we can query the ‘properties’ of the result to get the current value. I will query for results that have ‘currentValue’ more than 1. So, I can enhance further to create a report of the VM sizes that I am using and what is their capacity.
    $Capacity.value.properties | where {$_.currentValue -gt 1}

    Sample Output:

Hope this helps. You can also look into other operations on how to request for quota increase.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s