Gallery › Monitoring › Azure built-in policies
Enable logging by category group for Virtual networks (microsoft.network/virtualnetworks) to Log Analytics
Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode Indexed Version 1.1.0
Resource logs should be enabled to track activities and events that take place on your resources and give you visibility and insights into any changes that occur. This policy deploys a diagnostic setting using a category group to route logs to a Log Analytics workspace for Virtual networks (microsoft.network/virtualnetworks).
ConvertTo-Bylaw converted this policy, and proved that the file compiles back to the same definition.
Azure Policy JSONDS_LA_network-virtualnetworks_DINE.json · 219 lines
{
"properties": {
"displayName": "Enable logging by category group for Virtual networks (microsoft.network/virtualnetworks) to Log Analytics",
"description": "Resource logs should be enabled to track activities and events that take place on your resources and give you visibility and insights into any changes that occur. This policy deploys a diagnostic setting using a category group to route logs to a Log Analytics workspace for Virtual networks (microsoft.network/virtualnetworks).",
"mode": "Indexed",
"policyType": "BuiltIn",
"version": "1.1.0",
"metadata": {
"category": "Monitoring",
"version": "1.1.0"
},
"parameters": {
"effect": {
"type": "String",
"metadata": {
"displayName": "Effect",
"description": "Enable or disable the execution of the policy"
},
"allowedValues": [
"DeployIfNotExists",
"AuditIfNotExists",
"Disabled"
],
"defaultValue": "DeployIfNotExists"
},
"diagnosticSettingName": {
"type": "String",
"metadata": {
"displayName": "Diagnostic Setting Name"
},
"defaultValue": "setByPolicy-LogAnalytics"
},
"categoryGroup": {
"type": "String",
"metadata": {
"displayName": "Category Group",
"description": "Diagnostic category group - none, audit, or allLogs."
},
"allowedValues": [
"audit",
"allLogs"
],
"defaultValue": "audit"
},
"resourceLocationList": {
"type": "array",
"metadata": {
"displayName": "Resource Location List",
"description": "Resource Location List to send logs to nearby Log Analytics. A single entry \"*\" selects all locations (default)."
},
"defaultValue": [
"*"
]
},
"logAnalytics": {
"type": "string",
"metadata": {
"displayName": "Log Analytics Workspace",
"description": "Log Analytics Workspace",
"strongType": "omsWorkspace",
"assignPermissions": true
}
},
"metricsEnabled": {
"type": "Boolean",
"metadata": {
"displayName": "Enable metrics",
"description": "Whether to enable supported diagnostic metrics for Virtual Networks."
},
"allowedValues": [
true,
false
],
"defaultValue": true
}
},
"policyRule": {
"if": {
"allOf": [
{
"field": "type",
"equals": "Microsoft.Network/virtualNetworks"
},
{
"anyOf": [
{
"value": "[first(parameters('resourceLocationList'))]",
"equals": "*"
},
{
"field": "location",
"in": "[parameters('resourceLocationList')]"
}
]
}
]
},
"then": {
"effect": "[parameters('effect')]",
"details": {
"type": "Microsoft.Insights/diagnosticSettings",
"evaluationDelay": "AfterProvisioning",
"existenceCondition": {
"allOf": [
{
"count": {
"field": "Microsoft.Insights/diagnosticSettings/logs[*]",
"where": {
"allOf": [
{
"field": "Microsoft.Insights/diagnosticSettings/logs[*].enabled",
"equals": "[equals(parameters('categoryGroup'), 'allLogs')]"
},
{
"field": "Microsoft.Insights/diagnosticSettings/logs[*].categoryGroup",
"equals": "allLogs"
}
]
}
},
"equals": 1
},
{
"field": "Microsoft.Insights/diagnosticSettings/workspaceId",
"equals": "[parameters('logAnalytics')]"
}
]
},
"roleDefinitionIds": [
"/providers/Microsoft.Authorization/roleDefinitions/92aaf0da-9dab-42b6-94a3-d43ce8d16293"
],
"deployment": {
"properties": {
"mode": "incremental",
"template": {
"$schema": "http://schema.management.azure.com/schemas/2019-08-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"diagnosticSettingName": {
"type": "string"
},
"logAnalytics": {
"type": "string"
},
"categoryGroup": {
"type": "String"
},
"resourceName": {
"type": "string"
},
"metricsEnabled": {
"type": "bool"
}
},
"variables": {},
"resources": [
{
"type": "Microsoft.Network/virtualNetworks/providers/diagnosticSettings",
"apiVersion": "2021-05-01-preview",
"name": "[concat(parameters('resourceName'), '/', 'Microsoft.Insights/', parameters('diagnosticSettingName'))]",
"properties": {
"workspaceId": "[parameters('logAnalytics')]",
"logAnalyticsDestinationType": "Dedicated",
"logs": [
{
"categoryGroup": "allLogs",
"enabled": "[equals(parameters('categoryGroup'), 'allLogs')]"
}
],
"metrics": [
{
"category": "AllMetrics",
"enabled": "[parameters('metricsEnabled')]",
"retentionPolicy": {
"enabled": false,
"days": 0
}
}
]
}
}
],
"outputs": {
"policy": {
"type": "string",
"value": "[concat('Diagnostic setting ', parameters('diagnosticSettingName'), ' for type Virtual networks (microsoft.network/virtualnetworks), resourceName ', parameters('resourceName'), ' to Log Analytics ', parameters('logAnalytics'), ' configured')]"
}
}
},
"parameters": {
"diagnosticSettingName": {
"value": "[parameters('diagnosticSettingName')]"
},
"logAnalytics": {
"value": "[parameters('logAnalytics')]"
},
"categoryGroup": {
"value": "[parameters('categoryGroup')]"
},
"resourceName": {
"value": "[field('name')]"
},
"metricsEnabled": {
"value": "[parameters('metricsEnabled')]"
}
}
}
}
}
}
},
"versions": [
"1.1.0",
"1.0.0"
]
},
"name": "3234ff41-8bec-40a3-b5cb-109c95f1c8ce",
"id": "/providers/Microsoft.Authorization/policyDefinitions/3234ff41-8bec-40a3-b5cb-109c95f1c8ce"
}Bylaw3234ff41-8bec-40a3-b5cb-109c95f1c8ce.bylaw.ps1 · 76 lines
# Converted from DS_LA_network-virtualnetworks_DINE.json by ConvertTo-Bylaw.
Policy '3234ff41-8bec-40a3-b5cb-109c95f1c8ce' {
Properties @{
DisplayName = 'Enable logging by category group for Virtual networks (microsoft.network/virtualnetworks) to Log Analytics'
Description = 'Resource logs should be enabled to track activities and events that take place on your resources and give you visibility and insights into any changes that occur. This policy deploys a diagnostic setting using a category group to route logs to a Log Analytics workspace for Virtual networks (microsoft.network/virtualnetworks).'
Category = 'Monitoring'
Version = '1.1.0'
PolicyVersion = '1.1.0'
Mode = 'Indexed'
}
Parameter Effect @{
AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
Default = 'DeployIfNotExists'
DisplayName = 'Effect'
Description = 'Enable or disable the execution of the policy'
}
Parameter DiagnosticSettingName @{
Default = 'setByPolicy-LogAnalytics'
DisplayName = 'Diagnostic Setting Name'
}
Parameter CategoryGroup @{
AllowedValues = @('audit', 'allLogs')
Default = 'audit'
DisplayName = 'Category Group'
Description = 'Diagnostic category group - none, audit, or allLogs.'
}
Parameter ResourceLocationList @{
Type = 'array'
Default = @('*')
DisplayName = 'Resource Location List'
Description = 'Resource Location List to send logs to nearby Log Analytics. A single entry "*" selects all locations (default).'
}
Parameter LogAnalytics @{
Type = 'string'
DisplayName = 'Log Analytics Workspace'
Description = 'Log Analytics Workspace'
Metadata = [ordered]@{ strongType = 'omsWorkspace'; assignPermissions = $true }
}
Parameter MetricsEnabled @{
Type = 'Boolean'
AllowedValues = @($true, $false)
Default = $true
DisplayName = 'Enable metrics'
Description = 'Whether to enable supported diagnostic metrics for Virtual Networks.'
}
Resource 'Microsoft.Network/virtualNetworks' {
Require 'Requirement 1' {
Arm '[first(parameters(''resourceLocationList''))]' | Must -Not -Be '*'
}
Require 'location is not one of the ResourceLocationList parameter' {
Field location | Must -Not -BeIn (Param ResourceLocationList) -IfPresent
}
}
DeployIfNotExists 'Microsoft.Insights/diagnosticSettings' -Role 'Log Analytics Contributor' -EvaluationDelay AfterProvisioning -Effect (Param Effect) {
Require 'logs, enabled, categoryGroup and 1 more are as required' {
Count 'logs[*]' -Where {
Field enabled | Must -Be (Arm '[equals(parameters(''categoryGroup''), ''allLogs'')]')
Field categoryGroup | Must -Be 'allLogs'
} | Must -Be 1
Field workspaceId | Must -Be (Param LogAnalytics)
}
Deployment -TemplateFile './templates/3234ff41-8bec-40a3-b5cb-109c95f1c8ce.json' -Parameters ([ordered]@{
diagnosticSettingName = (Param DiagnosticSettingName)
logAnalytics = (Param LogAnalytics)
categoryGroup = (Param CategoryGroup)
resourceName = (Arm '[field(''name'')]')
metricsEnabled = (Param MetricsEnabled)
})
}
}Its deployment templates are written beside it:
templates/3234ff41-8bec-40a3-b5cb-109c95f1c8ce.json
Source: built-in-policies/policyDefinitions/Monitoring/DS_LA_network-virtualnetworks_DINE.json in Azure/azure-policy, under the MIT licence.
Bylaw