Max Tet
Max Tet

Reputation: 755

Create an Image from a VM using the python client

Using the CLI, there is a simple command to create am image from a VM:

az image create \
--resource-group myResourceGroup \
--name myImage --source myVM

But I am having a hard time achieving the same thing with the Python client lib. The method begin_create_or_update from azure.mgmt.compute.v2020_06_01.operations.imagesoperations seems to be the right one but misses the source parameter.

What is the correct approach?

Upvotes: 1

Views: 250

Answers (1)

Max Tet
Max Tet

Reputation: 755

So after a lot of experimentation I got it working like this.

vm = compute_client.virtual_machines.get(resource_group, instance_name)
compute_client.images.begin_create_or_update(resource_group, image_name,
        Image(location=vm.location, source_virtual_machine=SubResource(id=vm.id))).wait(120)

The trick was to provide the Image argument and within it the VM id as a SubResource.

Upvotes: 1

Related Questions