使用Terraform处理Azure虚拟机删除

oknwwptz  于 2023-05-01  发布在  其他
关注(0)|答案(1)|浏览(142)

有人知道如何在azure上实现terraform文档中提到的backup_protected_vm吗?

我正是在我想要删除虚拟机而不删除其备份的情况下。我完全看不出如何在代码中创建备份后立即修改此属性。

#Get Backup policy
data "azurerm_backup_policy_vm" "backup_policy" {
  name                = var.backup_policy_name
  resource_group_name = var.recovery_vault.resource_group_name
  recovery_vault_name = var.recovery_vault.name
}

# Add VM in Backup Recovery Vault
resource "azurerm_backup_protected_vm" "backup_vm" {
  resource_group_name = var.recovery_vault.resource_group_name
  recovery_vault_name = var.recovery_vault.name
  source_vm_id        = azurerm_windows_virtual_machine.vm.id
  backup_policy_id    = data.azurerm_backup_policy_vm.backup_policy.id

  depends_on = [
    azurerm_virtual_machine_extension.extension_diskencryption
  ]
}
jmp7cifd

jmp7cifd1#

如果我正确理解了你要做的事情,下面是你如何处理它:
要允许删除虚拟机而不删除其备份,可以在azurerm_backup_protected_vm资源块中使用lifecycle元参数。ignore_changes将阻止Terraform在source_vm_id更改或删除时重新创建资源。
类似这样的东西应该可以做到这一点:

resource "azurerm_backup_protected_vm" "backup_vm" {
  resource_group_name = var.recovery_vault.resource_group_name
  recovery_vault_name = var.recovery_vault.name
  source_vm_id        = azurerm_windows_virtual_machine.vm.id
  backup_policy_id    = data.azurerm_backup_policy_vm.backup_policy.id

  depends_on = [
    azurerm_virtual_machine_extension.extension_diskencryption
  ]

  lifecycle {
    ignore_changes = [
      source_vm_id,
    ]
  }
}

相关问题