Gallery › Monitoring › Azure Community Policy
Deploy SQL Managed Instance Diagnostic Setting - Storage Account
Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode all Version 1.0.0
This policy automatically deploys and enable diagnostic settings for SQL MI to a Storage Account
ConvertTo-Bylaw converted this policy, and proved that the file compiles back to the same definition.
Azure Policy JSONazurepolicy.json · 142 lines
{
"name": "1fa51b86-8acc-4bce-a5c4-149e581ddc0d",
"type": "Microsoft.Authorization/policyDefinitions",
"properties": {
"displayName": "Deploy SQL Managed Instance Diagnostic Setting - Storage Account",
"description": "This policy automatically deploys and enable diagnostic settings for SQL MI to a Storage Account",
"metadata": {
"category": "Monitoring",
"version": "1.0.0"
},
"mode": "all",
"parameters": {
"storageAccountId": {
"type": "string",
"metadata": {
"displayName": "Storage Account ID",
"description": "The resource ID of the storage account to which you would like to send Diagnostic Logs."
}
},
"retentionDays": {
"type": "string",
"metadata": {
"displayName": "Number of days to retain logs",
"description": "Retention policy ranges from 1 to 365 days. If you do not want to apply any retention policy and retain data forever, set retention (days) to 0."
}
},
"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.Sql/managedInstances"
},
"then": {
"effect": "[parameters('effect')]",
"details": {
"type": "Microsoft.Insights/diagnosticSettings",
"roleDefinitionIds": [
"/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
],
"existenceCondition": {
"allOf": [
{
"field": "Microsoft.Insights/diagnosticSettings/logs.enabled",
"equals": "True"
},
{
"field": "Microsoft.Insights/diagnosticSettings/metrics.enabled",
"equals": "True"
},
{
"field": "Microsoft.Insights/diagnosticSettings/storageAccountId",
"matchInsensitively": "[parameters('storageAccountId')]"
}
]
},
"deployment": {
"properties": {
"mode": "incremental",
"template": {
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"resourceName": {
"type": "string"
},
"storageAccountId": {
"type": "string"
},
"location": {
"type": "string"
},
"retentionDays": {
"type": "string"
}
},
"variables": {},
"resources": [
{
"type": "Microsoft.Sql/managedInstances/providers/diagnosticSettings",
"apiVersion": "2017-05-01-preview",
"name": "[concat(parameters('resourceName'), '/', 'Microsoft.Insights/setByPolicy')]",
"location": "[parameters('location')]",
"dependsOn": [],
"properties": {
"storageAccountId": "[parameters('storageAccountId')]",
"logs": [
{
"category": "ResourceUsageStats",
"enabled": true,
"retentionPolicy": {
"enabled": true,
"days": "[parameters('retentionDays')]"
}
},
{
"category": "SQLSecurityAuditEvents",
"enabled": true,
"retentionPolicy": {
"enabled": true,
"days": "[parameters('retentionDays')]"
}
}
]
}
}
],
"outputs": {}
},
"parameters": {
"storageAccountId": {
"value": "[parameters('storageAccountId')]"
},
"location": {
"value": "[field('location')]"
},
"resourceName": {
"value": "[field('name')]"
},
"retentionDays": {
"value": "[parameters('retentionDays')]"
}
}
}
}
}
}
}
}
}Bylaw1fa51b86-8acc-4bce-a5c4-149e581ddc0d.bylaw.ps1 · 44 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '1fa51b86-8acc-4bce-a5c4-149e581ddc0d' {
Properties @{
DisplayName = 'Deploy SQL Managed Instance Diagnostic Setting - Storage Account'
Description = 'This policy automatically deploys and enable diagnostic settings for SQL MI to a Storage Account'
Category = 'Monitoring'
Version = '1.0.0'
Mode = 'All'
}
Parameter StorageAccountId @{
Type = 'string'
DisplayName = 'Storage Account ID'
Description = 'The resource ID of the storage account to which you would like to send Diagnostic Logs.'
}
Parameter RetentionDays @{
Type = 'string'
DisplayName = 'Number of days to retain logs'
Description = 'Retention policy ranges from 1 to 365 days. If you do not want to apply any retention policy and retain data forever, set retention (days) to 0.'
}
Parameter Effect @{
AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
Default = 'DeployIfNotExists'
DisplayName = 'Effect'
Description = 'DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy'
}
Resource 'Microsoft.Sql/managedInstances'
DeployIfNotExists 'Microsoft.Insights/diagnosticSettings' -Role 'Contributor' -Effect (Param Effect) {
Require 'logs.enabled, metrics.enabled and storageAccountId are as required' {
Field logs.enabled | Must -Be 'True'
Field metrics.enabled | Must -Be 'True'
Field storageAccountId | Must -Match (Param StorageAccountId)
}
Deployment -TemplateFile './templates/1fa51b86-8acc-4bce-a5c4-149e581ddc0d.json' -Parameters ([ordered]@{
storageAccountId = (Param StorageAccountId)
location = (Arm '[field(''location'')]')
resourceName = (Arm '[field(''name'')]')
retentionDays = (Param RetentionDays)
})
}
}Its deployment templates are written beside it:
templates/1fa51b86-8acc-4bce-a5c4-149e581ddc0d.json
Source: policyDefinitions/Monitoring/deploy-sql-managed-instance-diagnostic-setting-storage-account/azurepolicy.json in Azure/Community-Policy, under the MIT licence.
Bylaw