Gallery › Monitoring › Azure Community Policy
Deploy Diagnostic Settings for Activity Log to storage account
Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode All Version 1.0.0
Deploys the diagnostic settings for Activity Log to stream to a storage account 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 · 140 lines
{
"name": "8ee2ba6e-218f-4040-845a-c7c540335cf5",
"type": "Microsoft.Authorization/policyDefinitions",
"properties": {
"displayName": "Deploy Diagnostic Settings for Activity Log to storage account",
"description": "Deploys the diagnostic settings for Activity Log to stream to a storage account when any Subscription which is missing this diagnostic settings is created or updated.",
"metadata": {
"version": "1.0.0",
"category": "Monitoring"
},
"mode": "All",
"parameters": {
"logsEnabled": {
"type": "String",
"metadata": {
"displayName": "Enable logs",
"description": "Whether to enable logs stream to the storage account - True or False"
},
"allowedValues": [
"True",
"False"
],
"defaultValue": "True"
},
"storageAccountId": {
"type": "String",
"metadata": {
"displayName": "Storage Account ResourceId",
"description": "The resource ID of the storage account to which you would like to send Diagnostic Logs"
}
},
"profileName": {
"type": "String",
"metadata": {
"displayName": "Diagnostic Setting Profile Name",
"description": "Provide the name of the diagnistic setting profile to validate."
}
},
"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",
"deploymentScope": "Subscription",
"existenceScope": "Subscription",
"name": "[parameters('profileName')]",
"existenceCondition": {
"allOf": [
{
"count": {
"field": "Microsoft.Insights/diagnosticSettings/logs[*]",
"where": {
"field": "Microsoft.Insights/diagnosticSettings/logs[*].enabled",
"equals": "[parameters('logsEnabled')]"
}
},
"equals": 1
},
{
"field": "Microsoft.Insights/diagnosticSettings/storageAccountId",
"equals": "[parameters('storageAccountId')]"
}
]
},
"roleDefinitionIds": [
"/providers/microsoft.authorization/roleDefinitions/92aaf0da-9dab-42b6-94a3-d43ce8d16293"
],
"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": {
"storageAccountId": {
"type": "string"
},
"profileName": {
"type": "string"
}
},
"variables": {},
"resources": [
{
"type": "Microsoft.Insights/diagnosticSettings",
"apiVersion": "2021-05-01-preview",
"name": "[parameters('profileName')]",
"location": "Global",
"properties": {
"storageAccountId": "[parameters('storageAccountId')]",
"logs": [
{
"category": "Administrative",
"enabled": true
}
],
"metrics": []
}
}
],
"outputs": {}
},
"parameters": {
"storageAccountId": {
"value": "[parameters('storageAccountId')]"
},
"profileName": {
"value": "[parameters('profileName')]"
}
}
}
}
}
}
}
}
}Bylaw8ee2ba6e-218f-4040-845a-c7c540335cf5.bylaw.ps1 · 54 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '8ee2ba6e-218f-4040-845a-c7c540335cf5' {
Properties @{
DisplayName = 'Deploy Diagnostic Settings for Activity Log to storage account'
Description = 'Deploys the diagnostic settings for Activity Log to stream to a storage account when any Subscription which is missing this diagnostic settings is created or updated.'
Version = '1.0.0'
Category = 'Monitoring'
Mode = 'All'
}
Parameter LogsEnabled @{
AllowedValues = @('True', 'False')
Default = 'True'
DisplayName = 'Enable logs'
Description = 'Whether to enable logs stream to the storage account - True or False'
}
Parameter StorageAccountId @{
Type = 'String'
DisplayName = 'Storage Account ResourceId'
Description = 'The resource ID of the storage account to which you would like to send Diagnostic Logs'
}
Parameter ProfileName @{
Type = 'String'
DisplayName = 'Diagnostic Setting Profile Name'
Description = 'Provide the name of the diagnistic setting profile to validate.'
}
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 'Log Analytics Contributor' -Name (Param ProfileName) -ExistenceScope Subscription -Effect (Param Effect) {
Require 'logs, enabled and storageAccountId are as required' {
Count 'logs[*]' -Where {
Field enabled | Must -Be (Param LogsEnabled)
} | Must -Be 1
Field storageAccountId | Must -Be (Param StorageAccountId)
}
Deployment @{
TemplateFile = './templates/8ee2ba6e-218f-4040-845a-c7c540335cf5.json'
Scope = 'Subscription'
Location = 'eastus'
Parameters = [ordered]@{
storageAccountId = (Param StorageAccountId)
profileName = (Param ProfileName)
}
}
}
}Its deployment templates are written beside it:
templates/8ee2ba6e-218f-4040-845a-c7c540335cf5.json
Source: policyDefinitions/Monitoring/deploy-diagnostic-settings-for-activity-log-to-storage-account/azurepolicy.json in Azure/Community-Policy, under the MIT licence.
Bylaw