如何使用Terraform模板将Azure VM添加到恢复服务库中?

时间:2019-02-07 01:05:05

标签: azure terraform

我正在开发一个terraform模板,以将新构建的Azure VM自动注册到Azure恢复服务库中。我可以在Azurerm提供程序中找到的所有资源都是

  

azurerm_recovery_services_protection_policy_vm

     

azurerm_recovery_services_protected_vm

     

azurerm_recovery_services_vault

似乎他们都无法注册VM来恢复服务库。 Terraform是否具有此功能?

1 个答案:

答案 0 :(得分:0)

您可以在Terraform中看到 azurerm_recovery_services_protected_vm ,并且参数source_vm_id显示:

  

指定要备份的虚拟机的ID

它可以引用您要在恢复服务库中备份的VM。使用 azurerm_recovery_services_protection_policy_vm 创建策略,并使用 azurerm_recovery_services_vault 创建恢复服务库。

更新

您可以通过Terraform使用 azurerm_recovery_services_protected_vm 备份具有Recovery Vault的VM。像这样的代码:

data "azurerm_virtual_machine" "azurevm" {
    name    = "vm_name"
    resource_group_name = "group_name"
}

resource "azurerm_resource_group" "rg" {
    name    = "recovery_group_name"
    location = "eastus"
}

resource "azurerm_recovery_services_vault" "vault" {
    name    = "azurerecoveryvaulti1"
    location = "${azurerm_resource_group.rg.location}"
    resource_group_name = "${azurerm_resource_group.rg.name}"
    sku     = "Standard"
}

resource "azurerm_recovery_services_protection_policy_vm" "test" {
  name                = "azurerecoveryvaultpolicy1"
  resource_group_name = "${azurerm_resource_group.rg.name}"
  recovery_vault_name = "${azurerm_recovery_services_vault.vault.name}"

  backup = {
    frequency = "Daily"
    time      = "23:00"
  }

  retention_daily = {
    count = 1
  }
}

resource "azurerm_recovery_services_protected_vm" "example" {
  resource_group_name = "${azurerm_resource_group.rg.name}"
  recovery_vault_name = "${azurerm_recovery_services_vault.vault.name}"
  source_vm_id        = "${data.azurerm_virtual_machine.azurevm.id}"
  backup_policy_id    = "${azurerm_recovery_services_protection_policy_vm.test.id}"
}

对于测试,我创建了新的Recovery Vault。您可以将现有的用于data azurerm_recovery_services_vault。创建新策略,然后使用上述的 azurerm_recovery_services_protected_vm 创建备份的虚拟机。