Bylaw

Gallery › Backup › Azure built-in policies

Azure Backup should be enabled for Virtual Machines

Converted Policy AuditIfNotExists Disabled Mode Indexed Version 3.0.0

Ensure protection of your Azure Virtual Machines by enabling Azure Backup. Azure Backup is a secure and cost effective data protection solution for Azure.

ConvertTo-Bylaw converted this policy, and proved that the file compiles back to the same definition.

Azure Policy JSONVirtualMachines_EnableAzureBackup_Audit.json · 60 lines
{
  "properties": {
    "displayName": "Azure Backup should be enabled for Virtual Machines",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Ensure protection of your Azure Virtual Machines by enabling Azure Backup. Azure Backup is a secure and cost effective data protection solution for Azure.",
    "metadata": {
      "version": "3.0.0",
      "category": "Backup"
    },
    "version": "3.0.0",
    "parameters": {
      "effect": {
        "type": "string",
        "defaultValue": "AuditIfNotExists",
        "allowedValues": [
          "AuditIfNotExists",
          "Disabled"
        ],
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        }
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Compute/virtualMachines"
          },
          {
            "field": "id",
            "notContains": "/resourceGroups/databricks-rg-"
          },
          {
            "field": "Microsoft.Compute/imagePublisher",
            "notEquals": "azureopenshift"
          },
          {
            "field": "Microsoft.Compute/imagePublisher",
            "notEquals": "AzureDatabricks"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.RecoveryServices/backupprotecteditems"
        }
      }
    },
    "versions": [
      "3.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/013e242c-8828-4970-87b3-ab247555486d",
  "name": "013e242c-8828-4970-87b3-ab247555486d"
}
Bylaw013e242c-8828-4970-87b3-ab247555486d.bylaw.ps1 · 34 lines
# Converted from VirtualMachines_EnableAzureBackup_Audit.json by ConvertTo-Bylaw.
Policy '013e242c-8828-4970-87b3-ab247555486d' {

    Properties @{
        DisplayName   = 'Azure Backup should be enabled for Virtual Machines'
        Description   = 'Ensure protection of your Azure Virtual Machines by enabling Azure Backup. Azure Backup is a secure and cost effective data protection solution for Azure.'
        Version       = '3.0.0'
        Category      = 'Backup'
        PolicyVersion = '3.0.0'
        Mode          = 'Indexed'
    }

    Parameter Effect @{
        Type          = 'string'
        AllowedValues = @('AuditIfNotExists', 'Disabled')
        Default       = 'AuditIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }

    Resource 'Microsoft.Compute/virtualMachines' {

        When {
            Field id | Must -Not -ContainText '/resourceGroups/databricks-rg-'
            Field 'Microsoft.Compute/imagePublisher' | Must -Not -Be 'azureopenshift'
        }

        Require 'imagePublisher is AzureDatabricks' {
            Field 'Microsoft.Compute/imagePublisher' | Must -Be 'AzureDatabricks'
        }
    }

    AuditIfNotExists 'Microsoft.RecoveryServices/backupprotecteditems' -Effect (Param Effect)
}

Source: built-in-policies/policyDefinitions/Backup/VirtualMachines_EnableAzureBackup_Audit.json in Azure/azure-policy, under the MIT licence.