With the previous SDK (Microsoft.Azure.Management.Compute.Fluent) I was able to do something like this:
ISnapshot snapshot = azure.Snapshots.GetById(snapshotId);
Console.WriteLine("Creating os disk for vm...");
IDisk disk = azure.Disks.Define(vmOSDiskName)
.WithRegion(vmRegion)
.WithExistingResourceGroup(vmResourceGroupName)
.WithWindowsFromSnapshot(snapshot)
.WithSizeInGB(128)
.WithSku(DiskSkuTypes.SStandardSSDLRS)
.Create();
if (disk != null)
{
Console.WriteLine("Creating virtual machine from Snapshot...");
IVirtualMachine vm = await azure.VirtualMachines.Define(vmName)
.WithRegion(vmRegion)
.WithExistingResourceGroup(vmResourceGroupName)
.WithExistingPrimaryNetworkInterface(networkInterface)
.WithSpecializedOSDisk(disk, OperatingSystemTypes.Windows)
.WithSize(VirtualMachineSizeTypes.StandardE2sV3)
.CreateAsync();
return vm;
}
else
{
Console.WriteLine("Unable to create os disk for vm...");
}
And even with PowerShell I can utilize the same snapshot in this manner:
$snapshot = Get-AzSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName
Write-Host “Creating Disk Config”
$diskConfig = New-AzDiskConfig -AccountType StandardSSD_LRS -Location $snapshot.Location -SourceResourceId $snapshot.Id -CreateOption Copy
Write-Host “Creating OS Disk”
$disk = New-AzDisk -Disk $diskConfig -ResourceGroupName $resourceGroupName -DiskName $osDiskName
#Initialize virtual machine configuration
Write-Host “Creating VM Config”
$VirtualMachine = New-AzVMConfig -VMName $virtualMachineName -VMSize $virtualMachineSize
Write-Host “Updating VM config with OS Disk”
$VirtualMachine = Set-AzVMOSDisk -VM $VirtualMachine -ManagedDiskId $disk.Id -CreateOption Attach -Windows
Write-Host “Creating virtual machine and powering on”
New-AzVM -VM $VirtualMachine -ResourceGroupName $resourceGroupName -Location $snapshot.Location
I am not finding examples with Azure.ResourceManager.Compute that would allow me to leverage existing snapshots.
Does Azure.ResourceManager.Compute have the capability to do that and are there code samples of that?
I am trying to migrate from Microsoft.Azure.Management.Compute.Fluent, which is deprecated, to Azure.ResourceManager.Compute, but the samples and documentation are lacking.
user2557299 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.