I am trying to create a spot instance on azure from azure API through python code. The following code successfully creates the spot instance machine on Azure but when I try to connect through RDP, the credentials given in the API does not work.
import secrets
from datetime import datetime
from azure.identity import ClientSecretCredential
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.network import NetworkManagementClient
from azure.mgmt.compute.models import (
VirtualMachine,
HardwareProfile,
StorageProfile,
InstanceViewTypes,
OSDisk,
ManagedDiskParameters,
DiskCreateOptionTypes,
OSProfile,
NetworkProfile,
NetworkInterfaceReference,
VirtualMachinePriorityTypes,
VirtualMachineEvictionPolicyTypes,
BillingProfile
)
from azure.mgmt.network.models import PublicIPAddress, IPAllocationMethod, NetworkSecurityGroup, SecurityRule
from constance import config
from instance.client_utils.ssh_client import SSHClient
from instance.models import VMInstance
from syntric_cr.settings import (
AZURE_CLIENT_ID,
AZURE_CLIENT_SECRET,
AZURE_TENANT,
AZURE_SUBSCRIPTION_ID,
AZURE_CAPTURE_IMAGE,
AZURE_RESOURCE_GROUP,
AZURE_RDP_USERNAME,
AZURE_LOCATION, AZURE_EXISTING_NIC_NAME, AZURE_EXISTING_NSG_NAME,
)
def create_spot_instance(self):
current_timestamp = round(datetime.timestamp(datetime.now()))
vm_name = f"spot-{current_timestamp}"
vm_username = AZURE_RDP_USERNAME
vm_password = secrets.token_urlsafe(13)
vm_location = AZURE_LOCATION
vm_image_id = self.get_image_id(AZURE_RESOURCE_GROUP, AZURE_CAPTURE_IMAGE)
new_nic = self.creating_nic_and_ip_from_existing_vm(vm_name, vm_location)
vm_params = VirtualMachine(
location=vm_location,
hardware_profile=HardwareProfile(vm_size=config.AZURE_VIRTUAL_MACHINE_SIZE),
storage_profile=StorageProfile(
image_reference={'id': vm_image_id},
os_disk=OSDisk(
name=vm_name,
create_option=DiskCreateOptionTypes.from_image,
managed_disk=ManagedDiskParameters(storage_account_type='Standard_LRS')
)
),
os_profile=OSProfile(
computer_name=vm_name,
admin_username='hello',
admin_password='hello_123'
),
network_profile=NetworkProfile(
network_interfaces=[
NetworkInterfaceReference(id=new_nic.id)
]
),
priority=VirtualMachinePriorityTypes.spot,
eviction_policy=VirtualMachineEvictionPolicyTypes.deallocate,
billing_profile=BillingProfile(max_price=config.AZURE_MAX_SPOT_INSTANCE_BUDGET)
)
try:
self.service.virtual_machines.begin_create_or_update(AZURE_RESOURCE_GROUP, vm_name, vm_params)
return VMInstance.objects.create(
instance=vm_name, project=AZURE_RESOURCE_GROUP, provider='AZURE', status=False,
script_run_on_start=False, username=vm_username, password=vm_password)
except Exception as ex:
return
The username 'hello' and the password 'hello_123' does not work for the RDP access when the spot instance is created. I have to then manually reset the password in order to make it work
I tried the code above and copied this from the official Azure docs
By default hello_123 is not supported as a password due to username and password limitations while deploying Azure VM, I tried creating a Spot VM with hello_123 password and the password was not selected due to the naming conventions and a Warning - The value must be between 12 and 123 characters long, Refer below:-
I tried creating a Spot VM with Python SDK by following the username and password naming conventions as mentioned in this Document:-
Username- FAQ about Windows VMs in Azure - Azure Virtual Machines | Microsoft Learn
Password- FAQ about Windows VMs in Azure - Azure Virtual Machines | Microsoft Learn
My Python code to Deploy a SpotVM:-
Output:-
VM got created on Portal and I attached NSG and Public IP to the VM and tried to RDP into it with the credentials given in the code and it was successful, Refer below:-
RDP:-
Reference:-
Azure python sdk, how to deploy a vm and it's a Azure Spot instance - Stack Overflow By Jim Xu