Gallery › Monitoring › Azure Community Policy
Configure Azure Activity logs to stream to specified Event Hub
Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode All Version 1.0.0
Deploys the diagnostic settings for Azure Activity to stream subscriptions audit logs to an Event Hub to monitor subscription-level events. Amended built-in policy to add eventHubName-parameter to stream to a specific event hub.
ConvertTo-Bylaw converted this policy, and proved that the file compiles back to the same definition.
Azure Policy JSONazurepolicy.json · 199 lines
{
"name": "e128cd6e-238c-4c3b-b6ef-eb4726219890",
"type": "Microsoft.Authorization/policyDefinitions",
"properties": {
"displayName": "Configure Azure Activity logs to stream to specified Event Hub",
"description": "Deploys the diagnostic settings for Azure Activity to stream subscriptions audit logs to an Event Hub to monitor subscription-level events. Amended built-in policy to add eventHubName-parameter to stream to a specific event hub.",
"metadata": {
"version": "1.0.0",
"category": "Monitoring"
},
"mode": "All",
"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"
}
},
"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"
},
"profileName": {
"type": "String",
"metadata": {
"displayName": "Profile name",
"description": "The diagnostic settings profile name"
},
"defaultValue": "setbypolicy_EH"
},
"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",
"deploymentScope": "Subscription",
"existenceScope": "Subscription",
"existenceCondition": {
"allOf": [
{
"field": "Microsoft.Insights/diagnosticSettings/logs.enabled",
"equals": "[parameters('logsEnabled')]"
},
{
"field": "Microsoft.Insights/diagnosticSettings/eventHubAuthorizationRuleId",
"equals": "[parameters('eventHubRuleId')]"
},
{
"field": "Microsoft.Insights/diagnosticSettings/eventHubName",
"equals": "[parameters('eventHubName')]"
},
{
"field": "name",
"equals": "[parameters('profileName')]"
}
]
},
"deployment": {
"location": "westeurope",
"properties": {
"mode": "incremental",
"template": {
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"eventHubRuleId": {
"type": "String"
},
"eventHubName": {
"type": "string"
},
"logsEnabled": {
"type": "string"
},
"profileName": {
"type": "String"
},
"resourceName": {
"type": "string"
}
},
"variables": {},
"resources": [
{
"name": "[parameters('profileName')]",
"type": "Microsoft.Insights/diagnosticSettings",
"apiVersion": "2021-05-01-preview",
"location": "Global",
"properties": {
"eventHubAuthorizationRuleId": "[parameters('eventHubRuleId')]",
"eventHubName": "[parameters('eventHubName')]",
"logs": [
{
"category": "Administrative",
"enabled": "[parameters('logsEnabled')]"
},
{
"category": "Security",
"enabled": "[parameters('logsEnabled')]"
},
{
"category": "ServiceHealth",
"enabled": "[parameters('logsEnabled')]"
},
{
"category": "Alert",
"enabled": "[parameters('logsEnabled')]"
},
{
"category": "Recommendation",
"enabled": "[parameters('logsEnabled')]"
},
{
"category": "Policy",
"enabled": "[parameters('logsEnabled')]"
},
{
"category": "Autoscale",
"enabled": "[parameters('logsEnabled')]"
},
{
"category": "ResourceHealth",
"enabled": "[parameters('logsEnabled')]"
}
]
}
}
],
"outputs": {
"policy": {
"type": "string",
"value": "[concat('Diagnostic setting ', parameters('profileName'), ' for type Subscription Activity Logs (Microsoft.Insights/diagnosticSettings), resourceName ', parameters('resourceName'), ' to EventHub ', parameters('eventHubRuleId'), ':', parameters('eventHubName'), ' configured')]"
}
}
},
"parameters": {
"eventHubRuleId": {
"value": "[parameters('eventHubRuleId')]"
},
"eventHubName": {
"value": "[parameters('eventHubName')]"
},
"logsEnabled": {
"value": "[parameters('logsEnabled')]"
},
"profileName": {
"value": "[parameters('profileName')]"
},
"resourceName": {
"value": "[field('name')]"
}
}
}
},
"roleDefinitionIds": [
"/providers/microsoft.authorization/roleDefinitions/f526a384-b230-433a-b45c-95f59c4a2dec",
"/providers/microsoft.authorization/roleDefinitions/92aaf0da-9dab-42b6-94a3-d43ce8d16293"
]
}
}
}
}
}Bylawe128cd6e-238c-4c3b-b6ef-eb4726219890.bylaw.ps1 · 63 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy 'e128cd6e-238c-4c3b-b6ef-eb4726219890' {
Properties @{
DisplayName = 'Configure Azure Activity logs to stream to specified Event Hub'
Description = 'Deploys the diagnostic settings for Azure Activity to stream subscriptions audit logs to an Event Hub to monitor subscription-level events. Amended built-in policy to add eventHubName-parameter to stream to a specific event hub.'
Version = '1.0.0'
Category = 'Monitoring'
Mode = 'All'
}
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 LogsEnabled @{
AllowedValues = @('True', 'False')
Default = 'True'
DisplayName = 'Enable logs'
Description = 'Whether to enable logs stream to the Event Hub - True or False'
}
Parameter ProfileName @{
Default = 'setbypolicy_EH'
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 'Azure Event Hubs Data Owner', 'Log Analytics Contributor' -ExistenceScope Subscription -Effect (Param Effect) {
Require 'logs.enabled, eventHubAuthorizationRuleId, eventHubName and 1 more are as required' {
Field logs.enabled | Must -Be (Param LogsEnabled)
Field eventHubAuthorizationRuleId | Must -Be (Param EventHubRuleId)
Field eventHubName | Must -Be (Param EventHubName)
Field name | Must -Be (Param ProfileName)
}
Deployment @{
TemplateFile = './templates/e128cd6e-238c-4c3b-b6ef-eb4726219890.json'
Scope = 'Subscription'
Location = 'westeurope'
Parameters = [ordered]@{
eventHubRuleId = (Param EventHubRuleId)
eventHubName = (Param EventHubName)
logsEnabled = (Param LogsEnabled)
profileName = (Param ProfileName)
resourceName = (Arm '[field(''name'')]')
}
}
}
}Its deployment templates are written beside it:
templates/e128cd6e-238c-4c3b-b6ef-eb4726219890.json
Source: policyDefinitions/Monitoring/configure-azure-activity-logs-to-stream-to-specified-event-hub/azurepolicy.json in Azure/Community-Policy, under the MIT licence.
Bylaw