Hello,
Does anyone know how to implement this mention in the documentation on backup_protected_vm ?
I am exactly in the case where I want to delete a vm without deleting the backup. I don’t see at all how to modify this property right after the creation of the backup in the code.
First remove the property source_vm_id
from the resource block. Apply it.
Then remove the VM resource. Apply again.
Ok but what you propose is clearly not usable as it is.
I have a module that allows me to create a vm with different elements and activate its backup. I’m not going to modify the terraform code of the module every time I want to delete a vm.
This clearly goes against reusable code logic.
Just stating what’s in the documentation.
Perhaps you can think of some logic that forces that property to be null
when the VM resource doesn’t exist anymore (not sure if that would work either but it should be easy to try - ultimately it depends on what “removed” means in that sentence).
I did this by removing this piece of code from the vm builder module. In this way, I was able to modify the property when the vm is deleted from the list of vm used by the module.
variable "vm_windows_backup_list" {
type = map(object({
backup_policy_name = string
}))
}
locals {
backup_windows_list = flatten([
for vm_key, vm in var.vm_windows_backup_list : {
vm_name = vm_key
vm_id = contains(keys(module.vm_windows), vm_key) ? module.vm_windows[vm_key].virtual_machine_id : ""
backup_policy_name = vm.backup_policy_name
}
])
backup_windows_map = { for item in local.backup_windows_list : "backup_${item.vm_name}" => item }
}
########################## BACKUP SET UP ##########################
#Get Backup policy
data "azurerm_backup_policy_vm" "backup_policy" {
for_each = local.backup_windows_map
name = each.value.backup_policy_name
resource_group_name = var.recovery_vault.resource_group_name
recovery_vault_name = var.recovery_vault.name
depends_on = [
module.vm_windows
]
}
# Add VM in Backup Recovery Vault
resource "azurerm_backup_protected_vm" "backup_vm" {
for_each = local.backup_windows_map
resource_group_name = var.recovery_vault.resource_group_name
recovery_vault_name = var.recovery_vault.name
source_vm_id = each.value.vm_id
backup_policy_id = data.azurerm_backup_policy_vm.backup_policy[each.key].id
}
1 Like