A better generic approach to diag policies
Maintaining azure diagnostic policies can be a bit of a slog, as existing approach often have a splattering of hardcoded values that need to be cared for.
With this in mind, I tried to look at the problem and come up with a better way of organizing the content of these policies.
- Put all metric and log categories as parameters, and if any changes to these occur. One only need to edit and add/remove categories from a parameter in the given policy.
- New diagnostic policy for azure service, is much easier using the framework below. Only need to edit and add service type (<serviceType>) and metric/log categories supported for service.
- Not needing to delete assignment to update policy definition when changes to metric/log category occurs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | { "parameters": { "logAnalytics": { "type": "String", "metadata": { "displayName": "Log Analytics workspace", "description": "Select Log Analytics workspace from dropdown list. If this workspace is outside of the scope of the assignment you must manually grant 'Log Analytics Contributor' permissions (or similar) to the policy assignment's principal ID.", "strongType": "omsWorkspace" } }, "effect": { "type": "String", "metadata": { "displayName": "Effect", "description": "Enable or disable the execution of the policy" }, "allowedValues": ["DeployIfNotExists", "Disabled"], "defaultValue": "DeployIfNotExists" }, "profileName": { "type": "String", "metadata": { "displayName": "Profile name", "description": "The diagnostic settings profile name" }, "defaultValue": "Operations" }, "metricsEnabled": { "type": "array", "metadata": { "displayName": "Metrics to enable", "description": "Whether to enable metrics stream to the Log Analytics workspace. AllMetrics or None. Future support for multiple categories" }, "allowedValues": [], "defaultValue": [] }, "metricCategoryNames": { "type": "array", "metadata": { "displayName": "Metrics category names", "description": "Diagnostic metric category names for azure service" }, "allowedValues": [], "defaultValue": [] }, "logsEnabled": { "type": "array", "metadata": { "displayName": "Logs to enable", "description": "Name of logs to enable for firewall, supports multiple selections. Use syntax: . Or just AllLogs or None." }, "allowedValues": [], "defaultValue": [] }, "logCategoryNames": { "type": "array", "metadata": { "displayName": "Log category names", "description": "Diagnostic log category names for azure service" }, "allowedValues": [], "defaultValue": [] }, "evaluationDelay": { "type": "string", "metadata": { "displayName": "Evaluation Delay", "description": "Specifies when the existence of the related resources should be evaluated. The delay is only used for evaluations that are a result of a create or update resource request. Allowed values are AfterProvisioning, AfterProvisioningSuccess, AfterProvisioningFailure, or an ISO 8601 duration between 0 and 360 minutes." }, "defaultValue": "PT5M" }, "tagNameFilter": { "type": "string", "metadata": { "displayName": "Tag name to filter on", "description": "Tag name to check for. Use value: all to not filter on tag name" }, "defaultValue": "all" }, "tagValueFilter": { "type": "string", "metadata": { "displayName": "Tag value to filter on", "description": "Tag value to check for." }, "defaultValue": "prod" } }, "policyRule": { "if": { "allOf": [ { "field": "type", "equals": "<serviceType>" }, { "anyOf": [ { "field": "[concat('tags[', parameters('tagNameFilter'), ']')]", "equals": "[parameters('tagValueFilter')]" }, { "value": "[parameters('tagNameFilter')]", "equals": "all" } ] } ] }, "then": { "effect": "[parameters('effect')]", "details": { "type": "Microsoft.Insights/diagnosticSettings", "name": "[parameters('profileName')]", "evaluationDelay": "[parameters('evaluationDelay')]", "existenceCondition": { "allOf": [ { "anyOf": [ { "field": "Microsoft.Insights/diagnosticSettings/logs.enabled", "equals": "[if(contains(parameters('logsEnabled'),'AllLogs'),'True','Skipped')]" }, { "field": "Microsoft.Insights/diagnosticSettings/logs.enabled", "equals": "[if(contains(parameters('logsEnabled'),'None'),'False','Skipped')]" }, { "count": { "field": "Microsoft.Insights/diagnosticSettings/logs[*]", "where": { "allOf": [ { "value": "[current('Microsoft.Insights/diagnosticSettings/logs[*].enabled')]", "equals": "True" }, { "value": "[current('Microsoft.Insights/diagnosticSettings/logs[*].category')]", "in": "[if(or(contains(parameters('logsEnabled'),'AllLogs'),contains(parameters('logsEnabled'),'None')),array(''),parameters('logsEnabled'))]" } ] } }, "equals": "[if(or(contains(parameters('logsEnabled'),'AllLogs'),contains(parameters('logsEnabled'),'None')),-1,length(parameters('logsEnabled')))]" } ] }, { "field": "Microsoft.Insights/diagnosticSettings/metrics.enabled", "equals": "[contains(parameters('metricsEnabled'),'AllMetrics')]" }, { "field": "Microsoft.Insights/diagnosticSettings/workspaceId", "matchInsensitively": "[parameters('logAnalytics')]" } ] }, "roleDefinitionIds": [ "/providers/microsoft.authorization/roleDefinitions/749f88d5-cbae-40b8-bcfc-e573ddc772fa", "/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" }, "logAnalytics": { "type": "String" }, "location": { "type": "String" }, "profileName": { "type": "String" }, "metricsEnabled": { "type": "Array" }, "logsEnabled": { "type": "Array" }, "logCategoryNames": { "type": "Array" }, "metricCategoryNames": { "type": "Array" } }, "variables": {}, "resources": [ { "type": "<serviceType>/providers/diagnosticSettings", "apiVersion": "2021-05-01-preview", "name": "[concat(parameters('resourceName'), '/Microsoft.Insights/', parameters('profileName'))]", "location": "[parameters('location')]", "dependsOn": [], "properties": { "workspaceId": "[parameters('logAnalytics')]", "copy": [ { "name": "metrics", "count": "[length(parameters('metricCategoryNames'))]", "input": { "category": "[parameters('metricCategoryNames')[copyIndex('metrics')]]", "timeGrain": null, "enabled": "[or(contains(parameters('metricsEnabled'),parameters('metricCategoryNames')[copyIndex('metrics')]),contains(parameters('metricsEnabled'),'AllMetrics'))]", "retentionPolicy": { "days": 0, "enabled": false } } }, { "name": "logs", "count": "[length(parameters('logCategoryNames'))]", "input": { "category": "[parameters('logCategoryNames')[copyIndex('logs')]]", "enabled": "[or(contains(parameters('logsEnabled'),parameters('logCategoryNames')[copyIndex('logs')]),contains(parameters('logsEnabled'),'AllLogs'))]" } } ] } } ], "outputs": { "policy": { "type": "string", "value": "[concat('Diagnostic setting name: ', parameters('profileName'), ', resourceName: ', parameters('resourceName'), ' to Log Analytics: ', last(split(parameters('logAnalytics'),'/')), ' configured')]" } } }, "parameters": { "logAnalytics": { "value": "[parameters('logAnalytics')]" }, "location": { "value": "[field('location')]" }, "resourceName": { "value": "[field('name')]" }, "profileName": { "value": "[parameters('profileName')]" }, "metricsEnabled": { "value": "[parameters('metricsEnabled')]" }, "logsEnabled": { "value": "[parameters('logsEnabled')]" }, "logCategoryNames": { "value": "[parameters('logCategoryNames')]" }, "metricCategoryNames": { "value": "[parameters('metricCategoryNames')]" } } } } } } } } |
Below is an example of how to use for Azure Automation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | { "parameters": { "logAnalytics": { "type": "String", "metadata": { "displayName": "Log Analytics workspace", "description": "Select Log Analytics workspace from dropdown list. If this workspace is outside of the scope of the assignment you must manually grant 'Log Analytics Contributor' permissions (or similar) to the policy assignment's principal ID.", "strongType": "omsWorkspace" } }, "effect": { "type": "String", "metadata": { "displayName": "Effect", "description": "Enable or disable the execution of the policy" }, "allowedValues": ["DeployIfNotExists", "Disabled"], "defaultValue": "DeployIfNotExists" }, "profileName": { "type": "String", "metadata": { "displayName": "Profile name", "description": "The diagnostic settings profile name" }, "defaultValue": "Operations" }, "metricsEnabled": { "type": "array", "metadata": { "displayName": "Metrics to enable", "description": "Whether to enable metrics stream to the Log Analytics workspace. AllMetrics or None. Future support for multiple categories" }, "allowedValues": ["None", "AllMetrics"], "defaultValue": ["AllMetrics"] }, "metricCategoryNames": { "type": "array", "metadata": { "displayName": "Metrics category names", "description": "Diagnostic metric category names for azure service" }, "allowedValues": ["AllMetrics"], "defaultValue": ["AllMetrics"] }, "logsEnabled": { "type": "array", "metadata": { "displayName": "Logs to enable", "description": "Name of logs to enable for firewall, supports multiple selections. Use syntax: . Or just AllLogs or None." }, "allowedValues": [ "None", "AllLogs", "JobLogs", "JobStreams", "DscNodeStatus", "AuditEvent" ], "defaultValue": ["AllLogs"] }, "logCategoryNames": { "type": "array", "metadata": { "displayName": "Log category names", "description": "Diagnostic log category names for azure service" }, "allowedValues": ["JobLogs", "JobStreams", "DscNodeStatus", "AuditEvent"], "defaultValue": ["JobLogs", "JobStreams", "DscNodeStatus", "AuditEvent"] }, "evaluationDelay": { "type": "string", "metadata": { "displayName": "Evaluation Delay", "description": "Specifies when the existence of the related resources should be evaluated. The delay is only used for evaluations that are a result of a create or update resource request. Allowed values are AfterProvisioning, AfterProvisioningSuccess, AfterProvisioningFailure, or an ISO 8601 duration between 0 and 360 minutes." }, "defaultValue": "PT5M" }, "tagNameFilter": { "type": "string", "metadata": { "displayName": "Tag name to filter on", "description": "Tag name to check for. Use value: all to not filter on tag name" }, "defaultValue": "all" }, "tagValueFilter": { "type": "string", "metadata": { "displayName": "Tag value to filter on", "description": "Tag value to check for." }, "defaultValue": "prod" } }, "policyRule": { "if": { "allOf": [ { "field": "type", "equals": "Microsoft.Automation/automationAccounts" }, { "anyOf": [ { "field": "[concat('tags[', parameters('tagNameFilter'), ']')]", "equals": "[parameters('tagValueFilter')]" }, { "value": "[parameters('tagNameFilter')]", "equals": "all" } ] } ] }, "then": { "effect": "[parameters('effect')]", "details": { "type": "Microsoft.Insights/diagnosticSettings", "name": "[parameters('profileName')]", "evaluationDelay": "[parameters('evaluationDelay')]", "existenceCondition": { "allOf": [ { "anyOf": [ { "field": "Microsoft.Insights/diagnosticSettings/logs.enabled", "equals": "[if(contains(parameters('logsEnabled'),'AllLogs'),'True','Skipped')]" }, { "field": "Microsoft.Insights/diagnosticSettings/logs.enabled", "equals": "[if(contains(parameters('logsEnabled'),'None'),'False','Skipped')]" }, { "count": { "field": "Microsoft.Insights/diagnosticSettings/logs[*]", "where": { "allOf": [ { "value": "[current('Microsoft.Insights/diagnosticSettings/logs[*].enabled')]", "equals": "True" }, { "value": "[current('Microsoft.Insights/diagnosticSettings/logs[*].category')]", "in": "[if(or(contains(parameters('logsEnabled'),'AllLogs'),contains(parameters('logsEnabled'),'None')),array(''),parameters('logsEnabled'))]" } ] } }, "equals": "[if(or(contains(parameters('logsEnabled'),'AllLogs'),contains(parameters('logsEnabled'),'None')),-1,length(parameters('logsEnabled')))]" } ] }, { "field": "Microsoft.Insights/diagnosticSettings/metrics.enabled", "equals": "[contains(parameters('metricsEnabled'),'AllMetrics')]" }, { "field": "Microsoft.Insights/diagnosticSettings/workspaceId", "matchInsensitively": "[parameters('logAnalytics')]" } ] }, "roleDefinitionIds": [ "/providers/microsoft.authorization/roleDefinitions/749f88d5-cbae-40b8-bcfc-e573ddc772fa", "/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" }, "logAnalytics": { "type": "String" }, "location": { "type": "String" }, "profileName": { "type": "String" }, "metricsEnabled": { "type": "Array" }, "logsEnabled": { "type": "Array" }, "logCategoryNames": { "type": "Array" }, "metricCategoryNames": { "type": "Array" } }, "variables": {}, "resources": [ { "type": "Microsoft.Automation/automationAccounts/providers/diagnosticSettings", "apiVersion": "2021-05-01-preview", "name": "[concat(parameters('resourceName'), '/Microsoft.Insights/', parameters('profileName'))]", "location": "[parameters('location')]", "dependsOn": [], "properties": { "workspaceId": "[parameters('logAnalytics')]", "copy": [ { "name": "metrics", "count": "[length(parameters('metricCategoryNames'))]", "input": { "category": "[parameters('metricCategoryNames')[copyIndex('metrics')]]", "timeGrain": null, "enabled": "[or(contains(parameters('metricsEnabled'),parameters('metricCategoryNames')[copyIndex('metrics')]),contains(parameters('metricsEnabled'),'AllMetrics'))]", "retentionPolicy": { "days": 0, "enabled": false } } }, { "name": "logs", "count": "[length(parameters('logCategoryNames'))]", "input": { "category": "[parameters('logCategoryNames')[copyIndex('logs')]]", "enabled": "[or(contains(parameters('logsEnabled'),parameters('logCategoryNames')[copyIndex('logs')]),contains(parameters('logsEnabled'),'AllLogs'))]" } } ] } } ], "outputs": { "policy": { "type": "string", "value": "[concat('Diagnostic setting name: ', parameters('profileName'), ', resourceName: ', parameters('resourceName'), ' to Log Analytics: ', last(split(parameters('logAnalytics'),'/')), ' configured')]" } } }, "parameters": { "logAnalytics": { "value": "[parameters('logAnalytics')]" }, "location": { "value": "[field('location')]" }, "resourceName": { "value": "[field('name')]" }, "profileName": { "value": "[parameters('profileName')]" }, "metricsEnabled": { "value": "[parameters('metricsEnabled')]" }, "logsEnabled": { "value": "[parameters('logsEnabled')]" }, "logCategoryNames": { "value": "[parameters('logCategoryNames')]" }, "metricCategoryNames": { "value": "[parameters('metricCategoryNames')]" } } } } } } } } |
Happy tinkering!