Is there a way to create a disk from a snapshot using the Azure Python SDK?

43151375 20 Reputation points
2023-07-18T12:13:32.78+00:00

Is there a way to create a disk from a snapshot using the Azure python SDK? In the DiskOperations class begin_create_or_update has no parameter for a snapshot.

Azure Disk Storage
Azure Disk Storage
A high-performance, durable block storage designed to be used with Azure Virtual Machines and Azure VMware Solution.
661 questions
0 comments No comments
{count} votes

Accepted answer
  1. shiva patpi 13,261 Reputation points Microsoft Employee
    2023-07-18T16:40:30.3533333+00:00

    @43151375

    You can create the parameters using below block , that API Begin_Create_or_Update just requires the RG , DiskName, DiskParameters.

    Disk Parameters can be created using below block !

    snapshot_resource_id = f"/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/Microsoft.Compute/snapshots/{snapshot_name}"
    disk_params = {
        'location': 'EastUS',
        'creation_data': {
            'create_option': 'Copy',
            'source_resource_id': snapshot_resource_id
        },
        'disk_size_gb': disk_size_gb,
        'os_type': 'Linux'  
    }
    disk_result = compute_client.disks.begin_create_or_update(resource_group_name, disk_name, disk_params).result()
    
    
    

    //////////// Final piece of tested code to create disk from snapshots looks like below://///

    from azure.identity import DefaultAzureCredential
    from azure.mgmt.compute import ComputeManagementClient
    
    # Set your subscription ID and resource group name
    subscription_id = 'subid'
    resource_group_name = 'testsnapshot'
    
    # Set the snapshot details
    snapshot_name = 'testsnapshot'
    disk_name = 'testdiskfromtestsnapshot'
    disk_size_gb = 100  # Specify the desired size of the new disk in GB
    
    # Initialize the Azure credentials
    credential = DefaultAzureCredential()
    
    # Create a ComputeManagementClient object
    compute_client = ComputeManagementClient(credential, subscription_id)
    
    # Get the snapshot resource ID
    snapshot_resource_id = f"/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/Microsoft.Compute/snapshots/{snapshot_name}"
    
    # Create the disk
    disk_params = {
        'location': 'EastUS',
        'creation_data': {
            'create_option': 'Copy',
            'source_resource_id': snapshot_resource_id
        },
        'disk_size_gb': disk_size_gb,
        'os_type': 'Linux'  
    }
    
    disk_result = compute_client.disks.begin_create_or_update(resource_group_name, disk_name, disk_params).result()
    
    
    
    

    Regards,

    Shiva.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.