Gallery › Monitoring › Azure Community Policy
Deploy Diagnostic Settings for Subscription to Log Analytics workspace
Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode All Version 1.0.0
Deploys the diagnostic settings for a Subscription to stream to a regional Log Analytics workspace when any Subscription which is missing this diagnostic settings is created or updated.
ConvertTo-Bylaw converted this policy, and proved that the file compiles back to the same definition.
Azure Policy JSONazurepolicy.json · 141 lines
{
"name": "919a2a82-2fd6-4da2-ae06-e4c12f4e4bc3",
"type": "Microsoft.Authorization/policyDefinitions",
"properties": {
"displayName": "Deploy Diagnostic Settings for Subscription to Log Analytics workspace",
"description": "Deploys the diagnostic settings for a Subscription to stream to a regional Log Analytics workspace when any Subscription which is missing this diagnostic settings is created or updated.",
"metadata": {
"category": "Monitoring",
"version": "1.0.0"
},
"mode": "All",
"parameters": {
"profileName": {
"type": "String",
"metadata": {
"displayName": "Profile name",
"description": "The diagnostic settings profile name"
},
"defaultValue": "setbypolicy_Diagnostics"
},
"workspaceId": {
"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": "DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy"
},
"allowedValues": [
"DeployIfNotExists",
"AuditIfNotExists",
"Disabled"
],
"defaultValue": "DeployIfNotExists"
}
},
"policyRule": {
"if": {
"field": "type",
"equals": "Microsoft.Resources/subscriptions"
},
"then": {
"effect": "[parameters('effect')]",
"details": {
"type": "microsoft.insights/diagnosticSettings",
"existenceCondition": {
"allOf": [
{
"field": "Microsoft.Insights/diagnosticSettings/workspaceId",
"equals": "[parameters('workspaceId')]"
}
]
},
"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": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"profileName": {
"type": "string"
},
"workspaceId": {
"type": "string"
}
},
"variables": {},
"resources": [
{
"type": "microsoft.insights/diagnosticSettings",
"apiVersion": "2017-05-01-preview",
"name": "[parameters('profileName')]",
"properties": {
"workspaceId": "[parameters('workspaceId')]",
"logs": [
{
"category": "Administrative",
"enabled": true
},
{
"category": "Security",
"enabled": true
},
{
"category": "ServiceHealth",
"enabled": true
},
{
"category": "Alert",
"enabled": true
},
{
"category": "Recommendation",
"enabled": true
},
{
"category": "Policy",
"enabled": true
},
{
"category": "Autoscale",
"enabled": true
},
{
"category": "ResourceHealth",
"enabled": true
}
]
}
}
],
"outputs": {}
},
"parameters": {
"workspaceId": {
"value": "[parameters('workspaceId')]"
},
"profileName": {
"value": "[parameters('profileName')]"
}
}
},
"location": "eastus"
},
"deploymentScope": "subscription"
}
}
}
}
}Bylaw919a2a82-2fd6-4da2-ae06-e4c12f4e4bc3.bylaw.ps1 · 46 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '919a2a82-2fd6-4da2-ae06-e4c12f4e4bc3' {
Properties @{
DisplayName = 'Deploy Diagnostic Settings for Subscription to Log Analytics workspace'
Description = 'Deploys the diagnostic settings for a Subscription to stream to a regional Log Analytics workspace when any Subscription which is missing this diagnostic settings is created or updated.'
Category = 'Monitoring'
Version = '1.0.0'
Mode = 'All'
}
Parameter ProfileName @{
Default = 'setbypolicy_Diagnostics'
DisplayName = 'Profile name'
Description = 'The diagnostic settings profile name'
}
Parameter WorkspaceId @{
Type = 'String'
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.'
Metadata = [ordered]@{ strongType = 'omsWorkspace' }
}
Parameter Effect @{
AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
Default = 'DeployIfNotExists'
DisplayName = 'Effect'
Description = 'DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy'
}
Resource 'Microsoft.Resources/subscriptions'
DeployIfNotExists 'microsoft.insights/diagnosticSettings' -Role 'Monitoring Contributor', 'Log Analytics Contributor' -Effect (Param Effect) {
Require 'workspaceId is the WorkspaceId parameter' {
Field workspaceId | Must -Be (Param WorkspaceId)
}
Deployment @{
TemplateFile = './templates/919a2a82-2fd6-4da2-ae06-e4c12f4e4bc3.json'
Scope = 'Subscription'
Location = 'eastus'
Parameters = [ordered]@{
workspaceId = (Param WorkspaceId)
profileName = (Param ProfileName)
}
}
}
}Its deployment templates are written beside it:
templates/919a2a82-2fd6-4da2-ae06-e4c12f4e4bc3.json
Source: policyDefinitions/Monitoring/deploy-diagnostic-settings-for-subscription-to-log-analytics-workspace/azurepolicy.json in Azure/Community-Policy, under the MIT licence.
Bylaw