Search code examples
azurebackupterraformsnapshotrecovery

Terraform Azurerm azurerm_recovery_services_protected_vm “Set number of instant recovery snapshot(s)”


I have successfully created daily and weekly backup policies using Terraform and both work fine. The Azure Portal however shows a red mark under "Instant Restore" on the policy blade saying "Retain instant recovery snapshot(s) for" and the value appears as 2 days. I need to change this value to 5; however, I don't see an option to alter it in Terraform. I was wondering if I should use "azurerm_snapshot" resource type to change it or if there is a workaround available in TF for it.

resource "azurerm_recovery_services_protection_policy_vm" "backup_policy_weekly" {
  name                = "${var.RG4VM}-weekly-bkp-policy"
  resource_group_name = "${var.RG4VM}"
  recovery_vault_name = "${azurerm_recovery_services_vault.backup_vault.name}"
  depends_on          = ["azurerm_recovery_services_vault.backup_vault"]
   timezone            = "UTC"

  backup {
    frequency = "Weekly"
    time      = "18:30"
    weekdays  = ["Friday"]
  }

  retention_weekly {
    count    = "2"
    weekdays = ["Friday"]
  }

  retention_monthly {
    count    = "1"
    weekdays = ["Friday"]
    weeks    = ["Last"]
  }
}

Expected: Snapshot set to 5, as it is the minimum value Actual: 2

Thank you/Asghar


Solution

  • For your issue, I just can say the property is not supported by Terraform. You can see it in the Azure REST API for Recovery Policy as property instantRpRetentionRangeInDays and use the request body like this:

    {
      "properties": {
        "backupManagementType": "AzureIaasVM",
        "schedulePolicy": {
          "schedulePolicyType": "SimpleSchedulePolicy",
          "scheduleRunFrequency": "Weekly",
          "scheduleRunDays": [
            "Friday"
          ],
          "scheduleRunTimes": [
            "2018-07-30T18:30:00Z"
          ],
          "scheduleWeeklyFrequency": 0
        },
        "retentionPolicy": {
          "retentionPolicyType": "LongTermRetentionPolicy",
          "weeklySchedule": {
            "daysOfTheWeek": [
              "Friday"
            ],
            "retentionTimes": [
              "2018-07-30T18:30:00Z"
            ],
            "retentionDuration": {
              "count": 5,
              "durationType": "Weeks"
            }
          }
        },
        "instantRpRetentionRangeInDays": 5,
        "timeZone": "UTC",
        "protectedItemsCount": 0
      }
    }
    

    Or you also can use the Azure Template and it also shows in it. But you cannot find the property in the Terraform. So I suggest you can use the Azure REST API or Template to achieve it.