How to attach a VM Scale Set to a VM?

Prem Jha 25 Reputation points

I am using API Version 2018-06-01 Azure Python SDK, and I want to use the VM Scale Set option under Availability options and assign it while I am creating a Virtual Machine. I tried the following code snippet but it created VM without having Scale Set attached to it.
What is the correct way of doing it please help me out.

Python2 code:
ret = {

'location': datacenter,

'os_profile': {

    'computer_name': vmname,

    'admin_username': username,

    'admin_password': password,

    "linuxConfiguration": {

        "ssh": {

            "publicKeys": [{

                "path": "/home/%s/.ssh/authorized_keys" % username,

                "keyData": sshkey





'hardware_profile': {

    'vm_size': size


'virtual_machine_scale_set': {

    'id': '/subscriptions/<subscription_id>/resourceGroups/<resource_group_name>/providers/Microsoft.Compute/virtualMachineScaleSets/<vmss_name>'


'storage_profile': {

    'image_reference': {

        'id': vm_reference.get('imageId') if 'imageId' in vm_reference and vm_reference['imageId'] is not None else None,

        'publisher': vm_reference.get('publisher'),

        'offer': vm_reference.get('offer'),

        'sku': vm_reference.get('sku'),

        'version': vm_reference.get('version')


    'os_disk': os_disk,


'network_profile': {

    'network_interfaces': [{

        'id': nic_id,




return ret

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,265 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. deherman-MSFT 34,021 Reputation points Microsoft Employee

    @Prem Jha

    I used the example code here and was able to create a VM and add it to an exisiting VMSS without issue:

    # Step 6: Provision the virtual machine
    # Obtain the management object for virtual machines
    compute_client = ComputeManagementClient(credential, subscription_id)
    VM_NAME = "ExampleVM"
    USERNAME = "azureuser"
    PASSWORD = "pw"
        f"Provisioning virtual machine {VM_NAME}; this operation might \
    take a few minutes."
    # Provision the VM specifying only minimal arguments, which defaults
    # to an Ubuntu 18.04 VM on a Standard DS1 v2 plan with a public IP address
    # and a default virtual network/subnet.
    poller = compute_client.virtual_machines.begin_create_or_update(
            "location": LOCATION,
    		'virtual_machine_scale_set': {
    			'id': '/subscriptions/sub-id/resourceGroups/rg/providers/Microsoft.Compute/virtualMachineScaleSets/myvmss'
            "storage_profile": {
                "image_reference": {
                    "publisher": "Canonical",
                    "offer": "0001-com-ubuntu-server-jammy",
                    "sku": "22_04-LTS-Gen2",
                    "version": "latest",
            "hardware_profile": {"vm_size": "Standard_D2s_v5"},
            "os_profile": {
                "computer_name": VM_NAME,
                "admin_username": USERNAME,
                "admin_password": PASSWORD,
            "network_profile": {
                "network_interfaces": [
    vm_result = poller.result()
    print(f"Provisioned virtual machine {}")

    There are some limitations so make sure that your VMSS and VM qualify:

    • The scale set must use Flexible orchestration mode.
    • The scale set must have a platformFaultDomainCount of 1.
    • The VM and scale set must be in the same resource group.
    • The VM and target scale set must both be zonal, or they must both be regional. You can't attach a zonal VM to a regional scale set.
    • The VM can't be in a self-defined availability set.
    • The VM can't be in a ProximityPlacementGroup.
    • The VM can't be in an Azure Dedicated Host.
    • The VM must have a managed disk.
    • The scale set must have properties.singlePlacementGroup set to False.

    Please check these. If you are still having issues please let me know and we can work with you directly to further investigate.

    If you still have questions, please let us know in the "comments" and we would be happy to help you. Comment is the fastest way of notifying the experts.

    If the answer has been helpful, we appreciate hearing from you and would love to help others who may have the same question. Accepting answers helps increase visibility of this question for other members of the Microsoft Q&A community.

    Thank you for helping to improve Microsoft Q&A! User's image