Gallery › Monitoring › Azure Community Policy
Configure diagnostic settings for storage accounts to Event Hub
Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode Indexed Version 1.0.0
Deploys the diagnostic settings for storage accounts to stream resource logs to an Event Hub when any storage account 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 · 408 lines
{
"name": "cfa2080f-bcd1-4967-8afd-b0a74615f407",
"type": "Microsoft.Authorization/policyDefinitions",
"properties": {
"displayName": "Configure diagnostic settings for storage accounts to Event Hub",
"description": "Deploys the diagnostic settings for storage accounts to stream resource logs to an Event Hub when any storage account which is missing this diagnostic settings is created or updated.",
"metadata": {
"version": "1.0.0",
"category": "Monitoring"
},
"mode": "Indexed",
"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
}
},
"eventHubLocation": {
"type": "String",
"metadata": {
"displayName": "Event Hub Location",
"description": "The location the Event Hub resides in. Only Logic Apps in this location will be linked to this Event Hub.",
"strongType": "location"
},
"defaultValue": ""
},
"servicesToDeploy": {
"type": "Array",
"metadata": {
"displayName": "Storage services to deploy",
"description": "List of Storage services to deploy"
},
"allowedValues": [
"storageAccounts",
"blobServices",
"fileServices",
"tableServices",
"queueServices"
],
"defaultValue": [
"storageAccounts",
"blobServices",
"fileServices",
"tableServices",
"queueServices"
]
},
"diagnosticsSettingNameToUse": {
"type": "String",
"metadata": {
"displayName": "Setting name",
"description": "Name of the diagnostic settings."
},
"defaultValue": "storageAccountsDiagnosticsLogsToEventHub"
},
"StorageDelete": {
"type": "String",
"metadata": {
"displayName": "StorageDelete - Enabled",
"description": "Whether to stream StorageDelete logs to the Log Analytics workspace - True or False"
},
"allowedValues": [
"True",
"False"
],
"defaultValue": "True"
},
"StorageWrite": {
"type": "String",
"metadata": {
"displayName": "StorageWrite - Enabled",
"description": "Whether to stream StorageWrite logs to the Log Analytics workspace - True or False"
},
"allowedValues": [
"True",
"False"
],
"defaultValue": "True"
},
"StorageRead": {
"type": "String",
"metadata": {
"displayName": "StorageRead - Enabled",
"description": "Whether to stream StorageRead logs to the Log Analytics workspace - True or False"
},
"allowedValues": [
"True",
"False"
],
"defaultValue": "True"
},
"Transaction": {
"type": "String",
"metadata": {
"displayName": "Transaction - Enabled",
"description": "Whether to stream Transaction logs to the Log Analytics workspace - True or False"
},
"allowedValues": [
"True",
"False"
],
"defaultValue": "True"
},
"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.Storage/storageAccounts"
},
{
"anyOf": [
{
"value": "[parameters('eventHubLocation')]",
"equals": ""
},
{
"field": "location",
"equals": "[parameters('eventHubLocation')]"
}
]
}
]
},
"then": {
"effect": "[parameters('effect')]",
"details": {
"type": "Microsoft.Insights/diagnosticSettings",
"roleDefinitionIds": [
"/providers/microsoft.authorization/roleDefinitions/749f88d5-cbae-40b8-bcfc-e573ddc772fa",
"/providers/microsoft.authorization/roleDefinitions/f526a384-b230-433a-b45c-95f59c4a2dec"
],
"existenceCondition": {
"allOf": [
{
"field": "Microsoft.Insights/diagnosticSettings/metrics.enabled",
"equals": "True"
},
{
"field": "Microsoft.Insights/diagnosticSettings/logs.enabled",
"equals": "True"
},
{
"field": "Microsoft.Insights/diagnosticSettings/eventHubAuthorizationRuleId",
"equals": "[parameters('eventHubRuleId')]"
}
]
},
"deployment": {
"properties": {
"mode": "incremental",
"template": {
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"servicesToDeploy": {
"type": "array"
},
"diagnosticsSettingNameToUse": {
"type": "string"
},
"resourceName": {
"type": "string"
},
"eventHubRuleId": {
"type": "string"
},
"location": {
"type": "string"
},
"Transaction": {
"type": "string"
},
"StorageRead": {
"type": "string"
},
"StorageWrite": {
"type": "string"
},
"StorageDelete": {
"type": "string"
}
},
"variables": {},
"resources": [
{
"condition": "[contains(parameters('servicesToDeploy'), 'blobServices')]",
"type": "Microsoft.Storage/storageAccounts/blobServices/providers/diagnosticSettings",
"apiVersion": "2017-05-01-preview",
"name": "[concat(parameters('resourceName'), '/default/', 'Microsoft.Insights/', parameters('diagnosticsSettingNameToUse'))]",
"location": "[parameters('location')]",
"dependsOn": [],
"properties": {
"eventHubAuthorizationRuleId": "[parameters('eventHubRuleId')]",
"metrics": [
{
"category": "Transaction",
"enabled": "[parameters('Transaction')]",
"retentionPolicy": {
"days": 0,
"enabled": false
},
"timeGrain": null
}
],
"logs": [
{
"category": "StorageRead",
"enabled": "[parameters('StorageRead')]"
},
{
"category": "StorageWrite",
"enabled": "[parameters('StorageWrite')]"
},
{
"category": "StorageDelete",
"enabled": "[parameters('StorageDelete')]"
}
]
}
},
{
"condition": "[contains(parameters('servicesToDeploy'), 'fileServices')]",
"type": "Microsoft.Storage/storageAccounts/fileServices/providers/diagnosticSettings",
"apiVersion": "2017-05-01-preview",
"name": "[concat(parameters('resourceName'), '/default/', 'Microsoft.Insights/', parameters('diagnosticsSettingNameToUse'))]",
"location": "[parameters('location')]",
"dependsOn": [],
"properties": {
"eventHubAuthorizationRuleId": "[parameters('eventHubRuleId')]",
"metrics": [
{
"category": "Transaction",
"enabled": "[parameters('Transaction')]",
"retentionPolicy": {
"days": 0,
"enabled": false
},
"timeGrain": null
}
],
"logs": [
{
"category": "StorageRead",
"enabled": "[parameters('StorageRead')]"
},
{
"category": "StorageWrite",
"enabled": "[parameters('StorageWrite')]"
},
{
"category": "StorageDelete",
"enabled": "[parameters('StorageDelete')]"
}
]
}
},
{
"condition": "[contains(parameters('servicesToDeploy'), 'tableServices')]",
"type": "Microsoft.Storage/storageAccounts/tableServices/providers/diagnosticSettings",
"apiVersion": "2017-05-01-preview",
"name": "[concat(parameters('resourceName'), '/default/', 'Microsoft.Insights/', parameters('diagnosticsSettingNameToUse'))]",
"location": "[parameters('location')]",
"dependsOn": [],
"properties": {
"eventHubAuthorizationRuleId": "[parameters('eventHubRuleId')]",
"metrics": [
{
"category": "Transaction",
"enabled": "[parameters('Transaction')]",
"retentionPolicy": {
"days": 0,
"enabled": false
},
"timeGrain": null
}
],
"logs": [
{
"category": "StorageRead",
"enabled": "[parameters('StorageRead')]"
},
{
"category": "StorageWrite",
"enabled": "[parameters('StorageWrite')]"
},
{
"category": "StorageDelete",
"enabled": "[parameters('StorageDelete')]"
}
]
}
},
{
"condition": "[contains(parameters('servicesToDeploy'), 'queueServices')]",
"type": "Microsoft.Storage/storageAccounts/queueServices/providers/diagnosticSettings",
"apiVersion": "2017-05-01-preview",
"name": "[concat(parameters('resourceName'), '/default/', 'Microsoft.Insights/', parameters('diagnosticsSettingNameToUse'))]",
"location": "[parameters('location')]",
"dependsOn": [],
"properties": {
"eventHubAuthorizationRuleId": "[parameters('eventHubRuleId')]",
"metrics": [
{
"category": "Transaction",
"enabled": "[parameters('Transaction')]",
"retentionPolicy": {
"days": 0,
"enabled": false
},
"timeGrain": null
}
],
"logs": [
{
"category": "StorageRead",
"enabled": "[parameters('StorageRead')]"
},
{
"category": "StorageWrite",
"enabled": "[parameters('StorageWrite')]"
},
{
"category": "StorageDelete",
"enabled": "[parameters('StorageDelete')]"
}
]
}
},
{
"condition": "[contains(parameters('servicesToDeploy'), 'storageAccounts')]",
"type": "Microsoft.Storage/storageAccounts/providers/diagnosticSettings",
"apiVersion": "2017-05-01-preview",
"name": "[concat(parameters('resourceName'), '/', 'Microsoft.Insights/', parameters('diagnosticsSettingNameToUse'))]",
"location": "[parameters('location')]",
"dependsOn": [],
"properties": {
"eventHubAuthorizationRuleId": "[parameters('eventHubRuleId')]",
"metrics": [
{
"category": "Transaction",
"enabled": "[parameters('Transaction')]",
"retentionPolicy": {
"days": 0,
"enabled": false
},
"timeGrain": null
}
]
}
}
],
"outputs": {}
},
"parameters": {
"diagnosticsSettingNameToUse": {
"value": "[parameters('diagnosticsSettingNameToUse')]"
},
"eventHubRuleId": {
"value": "[parameters('eventHubRuleId')]"
},
"location": {
"value": "[field('location')]"
},
"resourceName": {
"value": "[field('name')]"
},
"Transaction": {
"value": "[parameters('Transaction')]"
},
"StorageDelete": {
"value": "[parameters('StorageDelete')]"
},
"StorageWrite": {
"value": "[parameters('StorageWrite')]"
},
"StorageRead": {
"value": "[parameters('StorageRead')]"
},
"servicesToDeploy": {
"value": "[parameters('servicesToDeploy')]"
}
}
}
}
}
}
}
}
}Bylawcfa2080f-bcd1-4967-8afd-b0a74615f407.bylaw.ps1 · 100 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy 'cfa2080f-bcd1-4967-8afd-b0a74615f407' {
Properties @{
DisplayName = 'Configure diagnostic settings for storage accounts to Event Hub'
Description = 'Deploys the diagnostic settings for storage accounts to stream resource logs to an Event Hub when any storage account which is missing this diagnostic settings is created or updated.'
Version = '1.0.0'
Category = 'Monitoring'
Mode = 'Indexed'
}
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 EventHubLocation @{
Default = ''
DisplayName = 'Event Hub Location'
Description = 'The location the Event Hub resides in. Only Logic Apps in this location will be linked to this Event Hub.'
Metadata = [ordered]@{ strongType = 'location' }
}
Parameter ServicesToDeploy @{
Type = 'Array'
AllowedValues = @('storageAccounts', 'blobServices', 'fileServices', 'tableServices', 'queueServices')
Default = @('storageAccounts', 'blobServices', 'fileServices', 'tableServices', 'queueServices')
DisplayName = 'Storage services to deploy'
Description = 'List of Storage services to deploy'
}
Parameter DiagnosticsSettingNameToUse @{
Default = 'storageAccountsDiagnosticsLogsToEventHub'
DisplayName = 'Setting name'
Description = 'Name of the diagnostic settings.'
}
Parameter StorageDelete @{
AllowedValues = @('True', 'False')
Default = 'True'
DisplayName = 'StorageDelete - Enabled'
Description = 'Whether to stream StorageDelete logs to the Log Analytics workspace - True or False'
JsonName = 'StorageDelete'
}
Parameter StorageWrite @{
AllowedValues = @('True', 'False')
Default = 'True'
DisplayName = 'StorageWrite - Enabled'
Description = 'Whether to stream StorageWrite logs to the Log Analytics workspace - True or False'
JsonName = 'StorageWrite'
}
Parameter StorageRead @{
AllowedValues = @('True', 'False')
Default = 'True'
DisplayName = 'StorageRead - Enabled'
Description = 'Whether to stream StorageRead logs to the Log Analytics workspace - True or False'
JsonName = 'StorageRead'
}
Parameter Transaction @{
AllowedValues = @('True', 'False')
Default = 'True'
DisplayName = 'Transaction - Enabled'
Description = 'Whether to stream Transaction logs to the Log Analytics workspace - True or False'
JsonName = 'Transaction'
}
Parameter Effect @{
AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
Default = 'DeployIfNotExists'
DisplayName = 'Effect'
Description = 'DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy'
}
Resource 'Microsoft.Storage/storageAccounts' {
Require 'Requirement 1' {
Param EventHubLocation | Must -Not -Be ''
}
Require 'location is not the EventHubLocation parameter' {
Field location | Must -Not -Be (Param EventHubLocation) -IfPresent
}
}
DeployIfNotExists 'Microsoft.Insights/diagnosticSettings' -Role 'Monitoring Contributor', 'Azure Event Hubs Data Owner' -Effect (Param Effect) {
Require 'metrics.enabled, logs.enabled and eventHubAuthorizationRuleId are as required' {
Field metrics.enabled | Must -Be 'True'
Field logs.enabled | Must -Be 'True'
Field eventHubAuthorizationRuleId | Must -Be (Param EventHubRuleId)
}
Deployment -TemplateFile './templates/cfa2080f-bcd1-4967-8afd-b0a74615f407.json' -Parameters ([ordered]@{
diagnosticsSettingNameToUse = (Param DiagnosticsSettingNameToUse)
eventHubRuleId = (Param EventHubRuleId)
location = (Arm '[field(''location'')]')
resourceName = (Arm '[field(''name'')]')
Transaction = (Param Transaction)
StorageDelete = (Param StorageDelete)
StorageWrite = (Param StorageWrite)
StorageRead = (Param StorageRead)
servicesToDeploy = (Param ServicesToDeploy)
})
}
}Its deployment templates are written beside it:
templates/cfa2080f-bcd1-4967-8afd-b0a74615f407.json
Source: policyDefinitions/Monitoring/configure-diagnostic-settings-for-storage-accounts-to-event-hub/azurepolicy.json in Azure/Community-Policy, under the MIT licence.
Bylaw