Gallery › Monitoring › Azure Community Policy
Deploy Diagnostic Settings for Recovery Services vault to Event Hub
Uses Condition Policy AuditIfNotExists DeployIfNotExists Disabled Mode Indexed Version 1.0.0
Deploys the diagnostic settings for Recovery Services vaults to stream to a Event Hub when any Recovery Services vaults which is missing this diagnostic settings is created or updated. The policy will set the diagnostic with all metrics and category enabled. Amended built-in policy to add eventHubName-parameter to stream to a specific event hub.
ConvertTo-Bylaw converted this policy, and proved it compiles back to the same definition. Conditions the language can't express yet are written as raw JSON with Condition.
{
"name": "0f84b279-00a5-401f-8d5e-84e293f6dcad",
"type": "Microsoft.Authorization/policyDefinitions",
"properties": {
"displayName": "Deploy Diagnostic Settings for Recovery Services vault to Event Hub",
"description": "Deploys the diagnostic settings for Recovery Services vaults to stream to a Event Hub when any Recovery Services vaults which is missing this diagnostic settings is created or updated. The policy will set the diagnostic with all metrics and category enabled. Amended built-in policy to add eventHubName-parameter to stream to a specific event hub.",
"metadata": {
"version": "1.0.0",
"category": "Monitoring"
},
"mode": "Indexed",
"parameters": {
"eventHubRuleId": {
"type": "String",
"metadata": {
"displayName": "Event Hub Authorization Rule Id",
"description": "The Event Hub authorization rule Id for Azure Diagnostics. The authorization rule needs to be at Event Hub namespace level. e.g. /subscriptions/{subscription Id}/resourceGroups/{resource group}/providers/Microsoft.EventHub/namespaces/{Event Hub namespace}/authorizationrules/{authorization rule}",
"strongType": "Microsoft.EventHub/Namespaces/AuthorizationRules",
"assignPermissions": true
}
},
"eventHubName": {
"type": "String",
"metadata": {
"displayName": "Event Hub Name",
"description": "Specify the name of the Event Hub"
}
},
"effect": {
"type": "String",
"metadata": {
"displayName": "Effect",
"description": "Enable or disable the execution of the policy"
},
"allowedValues": [
"DeployIfNotExists",
"AuditIfNotExists",
"Disabled"
],
"defaultValue": "DeployIfNotExists"
},
"profileName": {
"type": "String",
"metadata": {
"displayName": "Profile name",
"description": "The diagnostic settings profile name"
},
"defaultValue": "setbypolicy_EH"
},
"metricsEnabled": {
"type": "String",
"metadata": {
"displayName": "Enable metrics",
"description": "Whether to enable metrics stream to the Event Hub - True or False"
},
"allowedValues": [
"True",
"False"
],
"defaultValue": "False"
},
"logsEnabled": {
"type": "String",
"metadata": {
"displayName": "Enable logs",
"description": "Whether to enable logs stream to the Event Hub - True or False"
},
"allowedValues": [
"True",
"False"
],
"defaultValue": "True"
},
"eventHubLocation": {
"type": "String",
"metadata": {
"displayName": "Event Hub Location",
"description": "Resource Location must be in the same location as the Event Hub Namespace.",
"strongType": "location"
},
"allowedValues": [
"swedencentral",
"westeurope"
],
"defaultValue": "westeurope"
},
"tagName": {
"type": "String",
"metadata": {
"displayName": "Exclusion Tag Name",
"description": "Name of the tag to use for excluding vaults from this policy. This should be used along with the Exclusion Tag Value parameter."
},
"defaultValue": ""
},
"tagValue": {
"type": "String",
"metadata": {
"displayName": "Exclusion Tag Value",
"description": "Value of the tag to use for excluding vaults from this policy. This should be used along with the Exclusion Tag Name parameter."
},
"defaultValue": ""
}
},
"policyRule": {
"if": {
"allOf": [
{
"field": "type",
"equals": "Microsoft.RecoveryServices/vaults"
},
{
"anyOf": [
{
"value": "[parameters('eventHubLocation')]",
"equals": ""
},
{
"field": "location",
"equals": "[parameters('eventHubLocation')]"
}
]
},
{
"not": {
"field": "[concat('tags[',parameters('tagName'), ']')]",
"equals": "[parameters('tagValue')]"
}
}
]
},
"then": {
"effect": "[parameters('effect')]",
"details": {
"type": "Microsoft.Insights/diagnosticSettings",
"name": "[parameters('profileName')]",
"existenceCondition": {
"allOf": [
{
"count": {
"field": "Microsoft.Insights/diagnosticSettings/logs[*]",
"where": {
"allOf": [
{
"field": "Microsoft.Insights/diagnosticSettings/logs[*].Category",
"in": [
"CoreAzureBackup",
"AddonAzureBackupJobs",
"AddonAzureBackupAlerts",
"AddonAzureBackupPolicy",
"AddonAzureBackupStorage",
"AddonAzureBackupProtectedInstance",
"AzureBackupReport"
]
},
{
"field": "Microsoft.Insights/diagnosticSettings/logs[*].Enabled",
"equals": "[parameters('logsEnabled')]"
}
]
}
},
"equals": 7
},
{
"field": "Microsoft.Insights/diagnosticSettings/metrics.enabled",
"equals": "[parameters('metricsEnabled')]"
},
{
"field": "Microsoft.Insights/diagnosticSettings/eventHubAuthorizationRuleId",
"matchInsensitively": "[parameters('eventHubRuleId')]"
},
{
"field": "Microsoft.Insights/diagnosticSettings/eventHubName",
"matchInsensitively": "[parameters('eventHubName')]"
}
]
},
"roleDefinitionIds": [
"/providers/microsoft.authorization/roleDefinitions/f526a384-b230-433a-b45c-95f59c4a2dec",
"/providers/microsoft.authorization/roleDefinitions/92aaf0da-9dab-42b6-94a3-d43ce8d16293"
],
"deployment": {
"properties": {
"mode": "Incremental",
"template": {
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"resourceName": {
"type": "String"
},
"eventHubRuleId": {
"type": "string"
},
"eventHubName": {
"type": "string"
},
"location": {
"type": "String"
},
"profileName": {
"type": "String"
},
"metricsEnabled": {
"type": "String"
},
"logsEnabled": {
"type": "String"
}
},
"variables": {},
"resources": [
{
"type": "Microsoft.RecoveryServices/vaults/providers/diagnosticSettings",
"apiVersion": "2021-05-01-preview",
"name": "[concat(parameters('resourceName'), '/', 'Microsoft.Insights/', parameters('profileName'))]",
"location": "[parameters('location')]",
"dependsOn": [],
"properties": {
"eventHubName": "[parameters('eventHubName')]",
"eventHubAuthorizationRuleId": "[parameters('eventHubRuleId')]",
"metrics": [
{
"category": "Health",
"enabled": "[parameters('metricsEnabled')]",
"retentionPolicy": {
"days": 0,
"enabled": false
},
"timeGrain": null
}
],
"logs": [
{
"categoryGroup": "allLogs",
"enabled": "[parameters('logsEnabled')]"
}
]
}
}
],
"outputs": {
"policy": {
"type": "string",
"value": "[concat('Diagnostic setting ', parameters('profileName'), ' for type Recover Services vaults (Microsoft.RecoveryServices/vaults), resourceName ', parameters('resourceName'), ' to EventHub ', parameters('eventHubRuleId'), ':', parameters('eventHubName'), ' configured')]"
}
}
},
"parameters": {
"eventHubRuleId": {
"value": "[parameters('eventHubRuleId')]"
},
"eventHubName": {
"value": "[parameters('eventHubName')]"
},
"location": {
"value": "[field('location')]"
},
"resourceName": {
"value": "[field('name')]"
},
"profileName": {
"value": "[parameters('profileName')]"
},
"metricsEnabled": {
"value": "[parameters('metricsEnabled')]"
},
"logsEnabled": {
"value": "[parameters('logsEnabled')]"
}
}
}
}
}
}
}
}
}# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '0f84b279-00a5-401f-8d5e-84e293f6dcad' {
Properties @{
DisplayName = 'Deploy Diagnostic Settings for Recovery Services vault to Event Hub'
Description = 'Deploys the diagnostic settings for Recovery Services vaults to stream to a Event Hub when any Recovery Services vaults which is missing this diagnostic settings is created or updated. The policy will set the diagnostic with all metrics and category enabled. Amended built-in policy to add eventHubName-parameter to stream to a specific event hub.'
Version = '1.0.0'
Category = 'Monitoring'
Mode = 'Indexed'
}
Parameter EventHubRuleId @{
Type = 'String'
DisplayName = 'Event Hub Authorization Rule Id'
Description = 'The Event Hub authorization rule Id for Azure Diagnostics. The authorization rule needs to be at Event Hub namespace level. e.g. /subscriptions/{subscription Id}/resourceGroups/{resource group}/providers/Microsoft.EventHub/namespaces/{Event Hub namespace}/authorizationrules/{authorization rule}'
Metadata = [ordered]@{ strongType = 'Microsoft.EventHub/Namespaces/AuthorizationRules'; assignPermissions = $true }
}
Parameter EventHubName @{
Type = 'String'
DisplayName = 'Event Hub Name'
Description = 'Specify the name of the Event Hub'
}
Parameter Effect @{
AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
Default = 'DeployIfNotExists'
DisplayName = 'Effect'
Description = 'Enable or disable the execution of the policy'
}
Parameter ProfileName @{
Default = 'setbypolicy_EH'
DisplayName = 'Profile name'
Description = 'The diagnostic settings profile name'
}
Parameter MetricsEnabled @{
AllowedValues = @('True', 'False')
Default = 'False'
DisplayName = 'Enable metrics'
Description = 'Whether to enable metrics stream to the Event Hub - True or False'
}
Parameter LogsEnabled @{
AllowedValues = @('True', 'False')
Default = 'True'
DisplayName = 'Enable logs'
Description = 'Whether to enable logs stream to the Event Hub - True or False'
}
Parameter EventHubLocation @{
AllowedValues = @('swedencentral', 'westeurope')
Default = 'westeurope'
DisplayName = 'Event Hub Location'
Description = 'Resource Location must be in the same location as the Event Hub Namespace.'
Metadata = [ordered]@{ strongType = 'location' }
}
Parameter TagName @{
Default = ''
DisplayName = 'Exclusion Tag Name'
Description = 'Name of the tag to use for excluding vaults from this policy. This should be used along with the Exclusion Tag Value parameter.'
}
Parameter TagValue @{
Default = ''
DisplayName = 'Exclusion Tag Value'
Description = 'Value of the tag to use for excluding vaults from this policy. This should be used along with the Exclusion Tag Name parameter.'
}
Resource 'Microsoft.RecoveryServices/vaults' {
When {
AnyOf {
Param EventHubLocation | Must -Be ''
Field location | Must -Be (Param EventHubLocation)
}
}
Require '[concat(''''tags['''',parameters(''''tagName''''), '''']'''')] is as required' {
Condition -Violation '{ "field": "[concat(''tags['',parameters(''tagName''), '']'')]", "notEquals": "[parameters(''tagValue'')]" }'
}
}
DeployIfNotExists 'Microsoft.Insights/diagnosticSettings' -Role 'Azure Event Hubs Data Owner', 'Log Analytics Contributor' -Name (Param ProfileName) -Effect (Param Effect) {
Require 'logs, Category, Enabled and 3 more are as required' {
Count 'logs[*]' -Where {
Field Category | Must -BeIn @('CoreAzureBackup', 'AddonAzureBackupJobs', 'AddonAzureBackupAlerts', 'AddonAzureBackupPolicy', 'AddonAzureBackupStorage', 'AddonAzureBackupProtectedInstance', 'AzureBackupReport')
Field Enabled | Must -Be (Param LogsEnabled)
} | Must -Be 7
Field metrics.enabled | Must -Be (Param MetricsEnabled)
Field eventHubAuthorizationRuleId | Must -Match (Param EventHubRuleId)
Field eventHubName | Must -Match (Param EventHubName)
}
Deployment -TemplateFile './templates/0f84b279-00a5-401f-8d5e-84e293f6dcad.json' -Parameters ([ordered]@{
eventHubRuleId = (Param EventHubRuleId)
eventHubName = (Param EventHubName)
location = (Arm '[field(''location'')]')
resourceName = (Arm '[field(''name'')]')
profileName = (Param ProfileName)
metricsEnabled = (Param MetricsEnabled)
logsEnabled = (Param LogsEnabled)
})
}
}Its deployment templates are written beside it:
templates/0f84b279-00a5-401f-8d5e-84e293f6dcad.json
Source: policyDefinitions/Monitoring/To Event Hub/Deploy Diagnostic Settings for Recovery Services vault to Event Hub/azurepolicy.json in Azure/Community-Policy, under the MIT licence.
Bylaw