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"
print(
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(
RESOURCE_GROUP_NAME,
VM_NAME,
{
"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": [
{
"id": nic_result.id,
}
]
},
},
)
vm_result = poller.result()
print(f"Provisioned virtual machine {vm_result.name}")
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!