1
votes

I'm developing a terraform template to automatically enroll a new built Azure VM to Azure recovery service vault. All the resources I can found in Azurerm provider are

azurerm_recovery_services_protection_policy_vm

azurerm_recovery_services_protected_vm

azurerm_recovery_services_vault

Seems none of them can enroll a VM to recovery service vault. Does Terraform have this feature?

1

1 Answers

1
votes

You can see the azurerm_recovery_services_protected_vm in Terraform, and the argument source_vm_id shows:

Specifies the ID of the VM to backup

It can refer to the VM which you want to back up in the recovery service vault. Create the policy with azurerm_recovery_services_protection_policy_vm and the recovery service vault with azurerm_recovery_services_vault.

Update

You can back up the VM with Recovery vault with azurerm_recovery_services_protected_vm through Terraform. The code like this:

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}"
}

For the test, I create the new Recovery Vault. You can use the existing with data azurerm_recovery_services_vault. Create a new policy then create the back up the vm with azurerm_recovery_services_protected_vm like above.