Azure SDK for Python support create a VM with a custom image.
If a custom image has been present in "My Images" on the Azure Portal, you can create VM with the arguments OS_DISK_NAME
&STORAGE_NAME
of the VHD image on the storage by using Azure Python SDK.
The Azure Python SDK APIs wrap the same function REST APIs. Refer to the REST API doc "Create or update a virtual machine" https://msdn.microsoft.com/en-us/library/azure/mt163591.aspx, to create a VM with image only need the osDisk
storage profile element (see the snapshot as below).
So the sample code only modified in partial (remove the image_reference
part) as below:
# 4. Create the virtual machine
result = compute_client.virtual_machines.create_or_update(
GROUP_NAME,
azure.mgmt.compute.VirtualMachine(
location=REGION,
name=VM_NAME,
os_profile=azure.mgmt.compute.OSProfile(
admin_username=ADMIN_USERNAME,
admin_password=ADMIN_PASSWORD,
computer_name=COMPUTER_NAME,
),
hardware_profile=azure.mgmt.compute.HardwareProfile(
virtual_machine_size=azure.mgmt.compute.VirtualMachineSizeTypes.standard_a0
),
network_profile=azure.mgmt.compute.NetworkProfile(
network_interfaces=[
azure.mgmt.compute.NetworkInterfaceReference(
reference_uri=nic_id,
),
],
),
storage_profile=azure.mgmt.compute.StorageProfile(
os_disk=azure.mgmt.compute.OSDisk(
caching=azure.mgmt.compute.CachingTypes.none,
create_option=azure.mgmt.compute.DiskCreateOptionTypes.from_image,
name=OS_DISK_NAME, // Your VHD name
virtual_hard_disk=azure.mgmt.compute.VirtualHardDisk(
uri='https://{0}.blob.core.windows.net/vhds/{1}.vhd'.format(
STORAGE_NAME, // your storage account name
OS_DISK_NAME, // Your VHD name
),
),
)
),
),
)