4
votes

I'm using the "new" azure sdk for python: https://github.com/Azure/azure-sdk-for-python

Linked is a usage example to serve as documentation: https://azure-sdk-for-python.readthedocs.org/en/latest/resourcemanagementcomputenetwork.html

In this example, they create an instance from a public image, providing an image publisher, offer, SKU and version. I'd like to create an instance from a custom image (present in "My Images" on the azure portal), for which I only have an image name, no publisher or SKU.

Is this supported? How should I proceed?

Note: I'd like to avoid using the azure CLI command if possible, only relying on the python library.

Thanks!

2

2 Answers

1
votes

In case anyone else runs into this issue, the SourceImage is actually for the older method (ASM). For ARM, the following will initialize the StorageProfile to provide a reference to a custom image:

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,
        virtual_hard_disk=azure.mgmt.compute.VirtualHardDisk(
            uri='https://{0}.blob.core.windows.net/vhds/{1}.vhd'.
            format(STORAGE_NAME, OS_DISK_NAME),
        ),
        operating_system_type='Linux',
        source_image=azure.mgmt.compute.VirtualHardDisk(
            uri='https://{0}.blob.core.windows.net/{1}/{2}'.format(
                STORAGE_NAME, CUSTOM_IMAGE_PATH, CUSTOM_IMAGE_VHD),
        ),
    ),
)

The two very important things above are 'operating_system_type' and the way the source_image is created.

0
votes

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).

enter image description here

So the sample code only modified in partial (remove the image_referencepart) 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
                    ),
                ),
            )
        ),
    ),
)