Skip to content

Support for keeping OS Disk on VM Destruction #7365

Description

@danmanrique

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Description

In the scenario of having an existing VM, I need the ability to remove the VM, keep the OS disk, and create a new VM with the destroyed VM's OS disk. This is available in the RP and works effectively in Azure CLI. I tried the setting: delete_os_disk_on_termination = false on Terraform v0.12.26, provider.azurerm v2.0.0.

$originalVM = Get-AzVM -ResourceGroupName $resourceGroup -Name $vmName

Remove-AzVM -ResourceGroupName $resourceGroup -Name $vmName

$newVM = New-AzVMConfig -VMName $originalVM.Name -VMSize $originalVM.HardwareProfile.VmSize -AvailabilitySetId $availSet.Id

Set-AzVMOSDisk -VM $newVM -CreateOption Attach -ManagedDiskId $originalVM.StorageProfile.OsDisk.ManagedDisk.Id -Name $originalVM.StorageProfile.OsDisk.Name -Linux

foreach ($disk in $originalVM.StorageProfile.DataDisks) {
Add-AzVMDataDisk -VM $newVM -Name $disk.Name
-ManagedDiskId $disk.ManagedDisk.Id -Caching $disk.Caching
-Lun $disk.Lun -DiskSizeInGB $disk.DiskSizeGB
-CreateOption Attach
}

New or Affected Resource(s)

azurerm_linux_virtual_machine

Potential Terraform Configuration

# Copy-paste your Terraform configurations here - for large Terraform configs,
# please use a service like Dropbox and share a link to the ZIP file. For
# security, you can also encrypt the files using our GPG public key.

References

  • #0000

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions