Gallery › App Service › Azure Community Policy
Configure Diagnostic Settings for all Web App Service Plan SKUs
Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode All Version 1.0.0
Custom Policy Definition that references the App Service Plan of an App Service and deploys the appropriate diagnostic settings
ConvertTo-Bylaw converted this policy, and proved that the file compiles back to the same definition.
Azure Policy JSONazurepolicy.json · 283 lines
{
"name": "b910b71b-efca-439e-a9a5-96c067b19c64",
"type": "Microsoft.Authorization/policyDefinitions",
"properties": {
"displayName": "Configure Diagnostic Settings for all Web App Service Plan SKUs",
"description": "Custom Policy Definition that references the App Service Plan of an App Service and deploys the appropriate diagnostic settings",
"metadata": {
"category": "App Service",
"version": "1.0.0"
},
"mode": "All",
"parameters": {
"effect": {
"type": "String",
"metadata": {
"displayName": "Effect",
"description": "Enable or disable the execution of the policy"
},
"allowedValues": [
"DeployIfNotExists",
"AuditIfNotExists",
"Disabled"
],
"defaultValue": "DeployIfNotExists"
},
"logsEnabled": {
"type": "String",
"metadata": {
"displayName": "Logs Enabled",
"description": "If True Logs will be enabled"
},
"allowedValues": [
"True",
"False"
],
"defaultValue": "True"
},
"metricsEnabled": {
"type": "String",
"metadata": {
"displayName": "Metrics Enabled",
"description": "If True Metrics will be enabled"
},
"allowedValues": [
"True",
"False"
],
"defaultValue": "True"
},
"profileName": {
"type": "String",
"metadata": {
"displayName": "Profile Name",
"description": "This is the name of your diagnostics settings resource"
},
"defaultValue": "EnabledByPolicy"
},
"logAnalytics": {
"type": "String",
"metadata": {
"displayName": "Log Analytics workspace",
"description": "Select Log Analytics workspace from the 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",
"assignPermissions": true
}
}
},
"policyRule": {
"if": {
"allOf": [
{
"equals": "Microsoft.Web/sites",
"field": "type"
},
{
"field": "kind",
"notContains": "functionapp"
},
{
"field": "kind",
"notContains": "linux"
}
]
},
"then": {
"effect": "[parameters('effect')]",
"details": {
"type": "Microsoft.Insights/diagnosticSettings",
"name": "[parameters('profileName')]",
"roleDefinitionIds": [
"/providers/microsoft.authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
],
"existenceCondition": {
"allOf": [
{
"not": {
"count": {
"field": "Microsoft.Insights/diagnosticSettings/logs[*]",
"where": {
"notEquals": "[parameters('logsEnabled')]",
"field": "Microsoft.Insights/diagnosticSettings/logs[*].enabled"
}
},
"greater": 0
}
},
{
"not": {
"count": {
"field": "Microsoft.Insights/diagnosticSettings/metrics[*]",
"where": {
"notEquals": "[parameters('metricsEnabled')]",
"field": "Microsoft.Insights/diagnosticSettings/metrics[*].enabled"
}
},
"greater": 0
}
}
]
},
"deployment": {
"properties": {
"mode": "incremental",
"parameters": {
"location": {
"value": "[toLower(replace(field('location'), ' ',''))]"
},
"logsEnabled": {
"value": "[parameters('logsEnabled')]"
},
"metricsEnabled": {
"value": "[parameters('metricsEnabled')]"
},
"profileName": {
"value": "[parameters('profileName')]"
},
"resourceName": {
"value": "[field('name')]"
},
"serverFarm": {
"value": "[field('Microsoft.Web/sites/serverFarmId')]"
},
"logAnalytics": {
"value": "[parameters('logAnalytics')]"
}
},
"template": {
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"type": "string"
},
"logsEnabled": {
"type": "string"
},
"metricsEnabled": {
"type": "string"
},
"profileName": {
"type": "string"
},
"resourceName": {
"type": "string"
},
"serverFarm": {
"type": "string"
},
"logAnalytics": {
"type": "string"
}
},
"resources": [
{
"apiVersion": "2018-02-01",
"type": "Microsoft.Resources/deployments",
"name": "nestedDeployment",
"properties": {
"mode": "Incremental",
"expressionEvaluationOptions": {
"scope": "inner"
},
"parameters": {
"location": {
"value": "[parameters('location')]"
},
"logsEnabled": {
"value": "[parameters('logsEnabled')]"
},
"metricsEnabled": {
"value": "[parameters('metricsEnabled')]"
},
"profileName": {
"value": "[parameters('profileName')]"
},
"resourceName": {
"value": "[parameters('resourceName')]"
},
"sku": {
"value": "[reference(parameters('serverFarm'), '2022-03-01','Full').sku.tier]"
},
"logAnalytics": {
"value": "[parameters('logAnalytics')]"
}
},
"template": {
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"type": "string"
},
"logsEnabled": {
"type": "string"
},
"metricsEnabled": {
"type": "string"
},
"profileName": {
"type": "string"
},
"resourceName": {
"type": "string"
},
"sku": {
"type": "string"
},
"logAnalytics": {
"type": "string"
}
},
"variables": {
"logNamesOrderSpecific": [
"AppServiceHTTPLogs",
"AppServiceConsoleLogs",
"AppServiceAppLogs",
"AppServiceAuditLogs",
"AppServiceIPSecAuditLogs",
"AppServicePlatformLogs",
"AppServiceFileAuditLogs",
"AppServiceAntivirusScanAuditLogs"
]
},
"resources": [
{
"apiVersion": "2021-05-01-preview",
"dependsOn": [],
"location": "[parameters('location')]",
"name": "[concat(parameters('resourceName'), '/', 'Microsoft.Insights/', parameters('profileName'))]",
"properties": {
"workspaceId": "[parameters('logAnalytics')]",
"copy": [
{
"name": "logs",
"count": "[if(contains(parameters('sku'), 'Premium'),8,6)]",
"input": {
"category": "[variables('logNamesOrderSpecific')[copyIndex('logs')]]",
"enabled": "[parameters('logsEnabled')]"
}
}
],
"metrics": [
{
"category": "AllMetrics",
"enabled": "[parameters('metricsEnabled')]"
}
]
},
"type": "Microsoft.Web/sites/providers/diagnosticSettings"
}
]
}
}
}
]
}
}
}
}
}
}
}
}Bylawb910b71b-efca-439e-a9a5-96c067b19c64.bylaw.ps1 · 72 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy 'b910b71b-efca-439e-a9a5-96c067b19c64' {
Properties @{
DisplayName = 'Configure Diagnostic Settings for all Web App Service Plan SKUs'
Description = 'Custom Policy Definition that references the App Service Plan of an App Service and deploys the appropriate diagnostic settings'
Category = 'App Service'
Version = '1.0.0'
Mode = 'All'
}
Parameter Effect @{
AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
Default = 'DeployIfNotExists'
DisplayName = 'Effect'
Description = 'Enable or disable the execution of the policy'
}
Parameter LogsEnabled @{
AllowedValues = @('True', 'False')
Default = 'True'
DisplayName = 'Logs Enabled'
Description = 'If True Logs will be enabled'
}
Parameter MetricsEnabled @{
AllowedValues = @('True', 'False')
Default = 'True'
DisplayName = 'Metrics Enabled'
Description = 'If True Metrics will be enabled'
}
Parameter ProfileName @{
Default = 'EnabledByPolicy'
DisplayName = 'Profile Name'
Description = 'This is the name of your diagnostics settings resource'
}
Parameter LogAnalytics @{
Type = 'String'
DisplayName = 'Log Analytics workspace'
Description = 'Select Log Analytics workspace from the 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'; assignPermissions = $true }
}
Resource 'Microsoft.Web/sites' {
When {
Field kind | Must -Not -ContainText 'functionapp'
}
Require 'kind contains linux' {
Field kind | Must -ContainText 'linux'
}
}
DeployIfNotExists 'Microsoft.Insights/diagnosticSettings' -Role 'Contributor' -Name (Param ProfileName) -Effect (Param Effect) {
Require 'logs, enabled and metrics are as required' {
Count 'logs[*]' -Where {
Field enabled | Must -Not -Be (Param LogsEnabled)
} | Must -Be 0
Count 'metrics[*]' -Where {
Field enabled | Must -Not -Be (Param MetricsEnabled)
} | Must -Be 0
}
Deployment -TemplateFile './templates/b910b71b-efca-439e-a9a5-96c067b19c64.json' -Parameters ([ordered]@{
location = (Arm '[toLower(replace(field(''location''), '' '',''''))]')
logsEnabled = (Param LogsEnabled)
metricsEnabled = (Param MetricsEnabled)
profileName = (Param ProfileName)
resourceName = (Arm '[field(''name'')]')
serverFarm = (Arm '[field(''Microsoft.Web/sites/serverFarmId'')]')
logAnalytics = (Param LogAnalytics)
})
}
}Its deployment templates are written beside it:
templates/b910b71b-efca-439e-a9a5-96c067b19c64.json
Source: policyDefinitions/App Service/configure-diagnostic-settings-for-all-web-app-service-plan-skus/azurepolicy.json in Azure/Community-Policy, under the MIT licence.
Bylaw