Bylaw

Gallery › Monitoring › Azure Community Policy

Deploy Diagnostic Settings for Activity Log to Log Analytics Workspace

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode All Version 1.0.0

Deploys the diagnostic settings for Activity Log 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 · 150 lines
{
  "name": "24480d31-fc9b-4a99-be70-01b3765c29de",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Deploy Diagnostic Settings for Activity Log to Log Analytics Workspace",
    "description": "Deploys the diagnostic settings for Activity Log to stream to a regional Log Analytics workspace when any Subscription which is missing this diagnostic settings is created or updated.",
    "metadata": {
      "version": "1.0.0",
      "category": "Monitoring"
    },
    "mode": "All",
    "parameters": {
      "logAnalytics": {
        "type": "String",
        "metadata": {
          "displayName": "Log Analytics workspace",
          "description": "Select Log Analytics workspace from dropdown list",
          "strongType": "omsWorkspace",
          "assignPermissions": true
        }
      },
      "profileName": {
        "type": "String",
        "metadata": {
          "displayName": "Profile name",
          "description": "The diagnostic settings profile name"
        },
        "defaultValue": "setbypolicy_logAnalytics"
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "DeployIfNotExists",
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Resources/subscriptions"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Insights/diagnosticSettings",
          "ExistenceScope": "Subscription",
          "DeploymentScope": "Subscription",
          "ExistenceCondition": {
            "allOf": [
              {
                "field": "Microsoft.Insights/diagnosticSettings/workspaceId",
                "equals": "[parameters('logAnalytics')]"
              },
              {
                "field": "name",
                "equals": "[parameters('profileName')]"
              }
            ]
          },
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/8e3af657-a8ff-443c-a75c-2fe8c4bcb635"
          ],
          "deployment": {
            "location": "eastus",
            "properties": {
              "mode": "incremental",
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "logAnalytics": {
                    "type": "String"
                  },
                  "profileName": {
                    "type": "String"
                  }
                },
                "variables": {},
                "resources": [
                  {
                    "type": "Microsoft.Insights/diagnosticSettings",
                    "apiVersion": "2017-05-01-preview",
                    "name": "[parameters('profileName')]",
                    "location": "global",
                    "properties": {
                      "workspaceId": "[parameters('logAnalytics')]",
                      "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
                        }
                      ]
                    }
                  }
                ]
              },
              "parameters": {
                "logAnalytics": {
                  "value": "[parameters('logAnalytics')]"
                },
                "profileName": {
                  "value": "[parameters('profileName')]"
                }
              }
            }
          }
        }
      }
    }
  }
}
Bylaw24480d31-fc9b-4a99-be70-01b3765c29de.bylaw.ps1 · 47 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '24480d31-fc9b-4a99-be70-01b3765c29de' {

    Properties @{
        DisplayName = 'Deploy Diagnostic Settings for Activity Log to Log Analytics Workspace'
        Description = 'Deploys the diagnostic settings for Activity Log to stream to a regional Log Analytics workspace when any Subscription which is missing this diagnostic settings is created or updated.'
        Version     = '1.0.0'
        Category    = 'Monitoring'
        Mode        = 'All'
    }

    Parameter LogAnalytics @{
        Type        = 'String'
        DisplayName = 'Log Analytics workspace'
        Description = 'Select Log Analytics workspace from dropdown list'
        Metadata    = [ordered]@{ strongType = 'omsWorkspace'; assignPermissions = $true }
    }
    Parameter ProfileName @{
        Default     = 'setbypolicy_logAnalytics'
        DisplayName = 'Profile name'
        Description = 'The diagnostic settings profile name'
    }
    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 'Owner' -ExistenceScope Subscription -Effect (Param Effect) {
        Require 'workspaceId is the LogAnalytics parameter and name is the ProfileName parameter' {
            Field workspaceId | Must -Be (Param LogAnalytics)
            Field name | Must -Be (Param ProfileName)
        }
        Deployment @{
            TemplateFile = './templates/24480d31-fc9b-4a99-be70-01b3765c29de.json'
            Scope        = 'Subscription'
            Location     = 'eastus'
            Parameters   = [ordered]@{
                logAnalytics = (Param LogAnalytics)
                profileName  = (Param ProfileName)
            }
        }
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/Monitoring/deploy-diagnostic-settings-for-activity-log-to-log-analytics-workspace/azurepolicy.json in Azure/Community-Policy, under the MIT licence.