How to Handle VM Restores in Terraform with Azure Backup?

0
33
Asked By CloudySkies92 On

Hey everyone! I'm curious if any of you are using Terraform for deploying Virtual Machines along with Azure Backup. I'm running into a bit of an issue with VM restores. When I use the "Replace existing" option to restore a VM, Terraform reports a drift on the VM disks since Azure creates new disks during the restoration. I've found that the only workaround is to re-import these new disks into the Terraform state, but I'm not a fan of that method. I'm wondering if anyone else has faced this problem and how you manage VM restores in your setup. Is there a best practice I might be overlooking? Thanks for your help!

4 Answers

Answered By TechSavvyJosh On

It totally makes sense. As suggested, you might want to set a lifecycle block to ignore changes for the OS disk. This way, Terraform will overlook any changes made during restores.

NewbieDev85 -

Could you clarify how that works? If I ignore lifecycle changes on the OS disk, does that mean I can't manage it via Terraform anymore? I’m still getting the hang of Terraform.

DataDude16 -

Just to add, drifting issues seem to only impact data disks. The OS disk appears to be linked to the VM, so I've not encountered issues there.

Answered By CodeNinja47 On

We had a similar experience. Here's what worked for us: First, ensure your azurerm provider isn't set to create OS disks—Azure should handle that. When restoring the OS disk, make sure to check that everything matches (like zones and encryption). Shutdown the VM, swap in the restored OS disk, and then fire up the VM. This way, when you run Terraform plan/apply, the state file gets updated with the new OS disk metadata without needing to recreate the VM. If you're dealing with data disks, it’s a bit trickier. We added a variable for `existing_disks` to manage data disks outside of Terraform. When you set this up, you can just automate the attachment of these disks, but note that you'd have to delete them manually if you remove the infrastructure. Hope this helps!

CuriousTester14 -

Thanks for the tips! So, you manage the new disks outside of Terraform? I focus primarily on backups, but I’ll pass this on to my team. It sounds like the drift issues mainly affect data disks, but the OS disk remains linked to the VM, so that’s good to know.

Answered By ConsultantAce21 On

Yeah, I eventually stopped trying to make Terraform fully manage restored VMs. For the OS disk, I let Azure handle it and don't force it through Terraform after a restore. For the data disks, I manage those outside of Terraform and just attach them with code. State imports seemed like a good idea but were usually fragile when backups come into play. This hybrid approach has made things easier in daily operations.

BackupGenius78 -

I usually rely on Azure Backup for the restoration, which automatically attaches the new disks to the VM.

Answered By OverwhelmedDev43 On

This situation has been a real headache for me too! I’ve gone down several paths trying to reimport, but nothing has worked smoothly. Most users I’ve talked to end up managing restored VMs outside of Terraform, using comments or tags for documentation. I’ll probably try the lifecycle ignore changes for the OS disk next.

TacticalCoder11 -

Just to clarify, you don't actually need to manage restored VMs outside of Terraform because default behavior doesn’t tie an OS disk to Terraform. It's better to use the az-api provider for tags.

CloudySkies92 -

That’s interesting! I thought managing them outside was necessary. I’ll definitely need to try that approach.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.