bicep template to deploy Azure recoveryservicesvault with policytype 'Enhanced'

108 views Asked by At

this is my first question on stackoverflow, so if i did something wrong, please tell me :)

I am trying to deploy an bicep template, that creates recovery services vault and creates a policy to backup a specific VM. I am getting the following error while deploying the template:

"UserErrorThisVMBackupIsSupportedUsingEnhancedPolicy","message":"This VM backup cannot be configured using standard policy, please use Enhanced Policy"}]}]}}

How can i do that in a bicep template? Tried multiple things, checked the documentation multiple times but can't find anything.

This is my YML:

steps:
- task: AzureCLI@2
  inputs:
    azureSubscription: ${{ parameters.serviceConnection }}
    scriptType: 'bash'
    scriptLocation: 'inlineScript'
    inlineScript: |
      az deployment group create --resource-group "${{ parameters.existingVirtualMachinesResourceGroup }}" --template-file "$(Build.SourcesDirectory)/iac/templates/backup-template.bicep" --parameters existingVirtualMachines='["${{ parameters.existingVirtualMachines }}"]' existingVirtualMachinesResourceGroup="${{ parameters.existingVirtualMachinesResourceGroup }}"
  displayName: 'Deploy ARM Template'

and this is my backup policy resource:

resource backupPolicy 'Microsoft.RecoveryServices/vaults/backupPolicies@2021-03-01' = if (isNewPolicy) {
  parent: recoveryServicesVault
  name: policyName
  location: location
  properties: {
    backupManagementType: 'AzureIaasVM'
    instantRpRetentionRangeInDays: 5
    schedulePolicy: {
      scheduleRunFrequency: 'Daily'
      scheduleRunTimes: scheduleRunTimes
      schedulePolicyType: 'SimpleSchedulePolicy'
    }
    retentionPolicy: {
      dailySchedule: {
        retentionTimes: scheduleRunTimes
        retentionDuration: {
          count: 104
          durationType: 'Days'
        }
      }
      weeklySchedule: {
        daysOfTheWeek: [
          'Sunday'
          'Tuesday'
          'Thursday'
        ]
        retentionTimes: scheduleRunTimes
        retentionDuration: {
          count: 104
          durationType: 'Weeks'
        }
      }
      monthlySchedule: {
        retentionScheduleFormatType: 'Daily'
        retentionScheduleDaily: {
          daysOfTheMonth: [
            {
              date: 1
              isLast: false
            }
          ]
        }
        retentionTimes: scheduleRunTimes
        retentionDuration: {
          count: 60
          durationType: 'Months'
        }
      }
      yearlySchedule: {
        retentionScheduleFormatType: 'Daily'
        monthsOfYear: [
          'January'
          'March'
          'August'
        ]
        retentionScheduleDaily: {
          daysOfTheMonth: [
            {
              date: 1
              isLast: false
            }
          ]
        }
        retentionTimes: scheduleRunTimes
        retentionDuration: {
          count: 10
          durationType: 'Years'
        }
      }
      retentionPolicyType: 'LongTermRetentionPolicy'
    }
    timeZone: 'UTC'
  }
}

Many thanks in advance!

Tried adding properties, but the resource isn't accepting them

1

There are 1 answers

0
crazyquesadilla On

It’s funny that you posted this today, as I was just looking for the same thing! Fortunately, I was able to figure it out.

It's not too clear from the documentation, but you have to set the following properties:

  • policyType to V2
  • schedulePolicyType to SimpleSchedulePolicyV2

Also, the actual SchedulePolicy syntax is a bit different—the Microsoft documentation should help you through it.

You'll also need to use a newer API version. The following worked for me:

Microsoft.RecoveryServices/vaults/backupPolicies@2022-03-01

I took your example and made the necessary changes, but I took out the retentionPolicy property just so the code block is a little easier to handle in this post. None of that should be changing.

resource backupPolicy 'Microsoft.RecoveryServices/vaults/backupPolicies@2022-03-01' = if (isNewPolicy) {
  parent: recoveryServicesVault
  name: policyName
  location: location
  properties: {
    backupManagementType: 'AzureIaasVM'
    instantRpRetentionRangeInDays: 5
    policyType: 'V2'
    schedulePolicy: {
      scheduleRunFrequency: 'Daily'
      dailySchedule: {
        scheduleRunTimes: [
          scheduleRunTimes
        ]
      }
      schedulePolicyType: 'SimpleSchedulePolicyV2'
    }
    timeZone: 'UTC'
  }
}