Search code examples
pythonazureazure-sdkazure-sdk-python

Azure Python SDK Compute Client isn't Using Managed Disk Parameters


I am using the compute client to create a VM (using create_or_update) and I want the VM to have a standard hdd and not a premium ssd as its os disk. I should be able to specify that in the managed disk parameters but when I do, the VM still creates with a premium SSD. Here are my VM parameters.

vm_parameters = {
        'location': vm_location,
        'os_profile': {
            'computer_name': vm_name,
            'admin_username': vm_name,
            'admin_password': vm_password,
            'custom_data': startup_script
        },
        'hardware_profile': {
            'vm_size': 'Standard_B1ls'
        },
        'storage_profile': {
            'image_reference': {
                'publisher': 'Canonical',
                'offer': 'UbuntuServer',
                'sku': '16.04.0-LTS',
                'version': 'latest'
            },
            'os_disk': {
                'caching': 'None',
                'create_option': 'FromImage',
                'disk_size_gb': 30,
                'managed_disk_parameters': {
                    'storage_account_type': 'Standard_LRS'
                }
            }
        },
        'network_profile': {
            'network_interfaces': [{
                'id': nic_info.id
            }]
        },
        'tags': {
            'expiration_date': 'expirationdatehere'
        }
    }

Just specifying the storage account type as Standard_LRS isn't changing anything. What should I do to make my VM create with a standard hdd as its os disk instead of a premium ssd?


Solution

  • According to my test, you use the wrong parameter in the vm_parameters. Please update managed_disk_parameters to managed_disk. For more details, please refer to https://learn.microsoft.com/en-us/python/api/azure-mgmt-compute/azure.mgmt.compute.v2019_03_01.models.osdisk?view=azure-python.

    For example:

    import os
    import traceback
    
    from azure.common.credentials import ServicePrincipalCredentials
    from azure.mgmt.resource import ResourceManagementClient
    from azure.mgmt.network import NetworkManagementClient
    from azure.mgmt.compute import ComputeManagementClient
    from azure.mgmt.compute.models import DiskCreateOption
    
    from msrestazure.azure_exceptions import CloudError
    
    from haikunator import Haikunator
    
    haikunator = Haikunator()
    
    AZURE_TENANT_ID= ''
    AZURE_CLIENT_ID=''
    AZURE_CLIENT_SECRET='' 
    AZURE_SUBSCRIPTION_ID=''
    
    credentials = ServicePrincipalCredentials(client_id=AZURE_CLIENT_ID,secret=AZURE_CLIENT_SECRET,tenant=AZURE_TENANT_ID)
    
    resource_client = ResourceManagementClient(credentials, AZURE_SUBSCRIPTION_ID)
    compute_client = ComputeManagementClient(credentials,AZURE_SUBSCRIPTION_ID)
    network_client = NetworkManagementClient(credentials, AZURE_SUBSCRIPTION_ID)
    GROUP_NAME='allenR'
    LOCATION='eastus'
    # Network
    VNET_NAME = 'azure-sample-vnet'
    SUBNET_NAME = 'azure-sample-subnet'
    
    
    print('\nCreate Vnet')
    async_vnet_creation = network_client.virtual_networks.create_or_update(
            GROUP_NAME,
            VNET_NAME,
            {
                'location': LOCATION,
                'address_space': {
                    'address_prefixes': ['10.0.0.0/16']
                }
            }
        )
    async_vnet_creation.wait()
    
        # Create Subnet
    print('\nCreate Subnet')
    async_subnet_creation = network_client.subnets.create_or_update(
            GROUP_NAME,
            VNET_NAME,
            SUBNET_NAME,
            {'address_prefix': '10.0.0.0/24'}
        )
    subnet_info = async_subnet_creation.result()
    
        # Create NIC
    print('\nCreate NIC')
    async_nic_creation = network_client.network_interfaces.create_or_update(
            GROUP_NAME,
            'test19191',
            {
                'location': LOCATION,
                'ip_configurations': [{
                    'name': 'test19191-ip',
                    'subnet': {
                        'id': subnet_info.id
                    }
                }]
            }
        )
    nic = async_nic_creation.result()
    print(nic.id)
    vm_parameters = {
            'location': 'eastus',
            'os_profile': {
                'computer_name': 'jimtest120yue',
                'admin_username': 'jimtest',
                'admin_password': 'Password0123!',
                #'custom_data': startup_script
            },
            'hardware_profile': {
                'vm_size': 'Standard_B1ls'
            },
            'storage_profile': {
                'image_reference': {
                    'publisher': 'Canonical',
                    'offer': 'UbuntuServer',
                    'sku': '16.04.0-LTS',
                    'version': 'latest'
                },
                'os_disk': {
                    'caching': 'ReadWrite',
                    'name' : 'jimtest120yue_disk',
                    'create_option': 'FromImage',
                    'disk_size_gb': 30,
                    'os_type': 'Linux',
                    'managed_disk': {
                        'storage_account_type': 'Standard_LRS'
                    }
    
                }
            },
            'network_profile': {
                'network_interfaces': [{
                    'id': nic.id
                }]
            },
            'tags': {
                'expiration_date': 'expirationdatehere'
            }
        }
    
    
    async_vm_creation=compute_client.virtual_machines.create_or_update('allenR','jimtest120yue',vm_parameters)
    print(async_vm_creation.result())
    
    disk = compute_client.disks.get('allenR','jimtest120yue_disk')
    print(disk.sku)
    
    

    enter image description here enter image description here