Search code examples
python-3.xazureazure-virtual-machineazure-rest-apiazure-python-sdk

Is there any azure api which returns the compatibility of VM Size to disk supported


I am writing a code to create dynamic VM's on azure based on certain values of CPU/Memory selection, so I have written code to hit

"https://management.azure.com/subscriptions/{SUB_ID}/providers/Microsoft.Compute/skus?api-version=2021-07-01&$filter=location eq '{location}'"

it returns the details of VM size and disk types but does not return any information on which VM size supports which disk because of which I sometimes face error like this

Requested operation cannot be performed because the VM size Basic_A3 does not support the storage account type Premium_LRS of disk 'OsDisk664f469b138d46d7a221790b'

is there any way to get the compatibility of VM size to disk so that I can handle it in code and avoid provisioning failures


Solution

  • is there any way to get the compatibility of VM size to disk so that I can handle it in code and avoid provisioning failures

    Basic_A3 is being retired and is also not supported for Premium disks. Please refer to the MS DOC for more details.

    enter image description here

    Here is the Python code to check the list of available VM SKUs that support Premium disks in a specific region. To check for VM SKUs that support Standard disks, you can change the condition capability['value'] == 'False'

        import requests
        import json
        
        # Define the Bearer token
        bearer_token = ""
        
        # Define the URL and parameters
        url = 'https://management.azure.com/subscriptions/SUB_ID/providers/Microsoft.Compute/skus'
        params = {
            'api-version': '2021-07-01',
            '$filter': "location eq 'westus'"
        }
        
        # Define the headers, including the Authorization header with the Bearer token
        headers = {
            'Authorization': f'Bearer {bearer_token}'
        }
        
        # Send the GET request with headers
        response = requests.get(url, headers=headers, params=params)
        
        # Check if the response is successful
        if response.status_code == 200:
            data = response.json()
            
            # Filter for virtual machines with PremiumIO capability set to True
            virtual_machines_with_premium_io = []
            
            for item in data['value']:
                if item['resourceType'] == 'virtualMachines':
                    capabilities = item.get('capabilities', [])
                    for capability in capabilities:
                        if capability['name'] == 'PremiumIO' and capability['value'] == 'True':
                            virtual_machines_with_premium_io.append(item)
                            break
            
            # Print the filtered result
            print(json.dumps(virtual_machines_with_premium_io, indent=2))
        else:
            print(f"Failed to retrieve data: {response.status_code}")
    

    Lists all available VM SKUs for the specified region that are supported Premium

    enter image description here

    You can also check the VM SKUs that are supported for both Premium and Standard disks in the Azure portal.

    enter image description here

    Reference: Lists all available Resource SKUs for the specified region