azure-policy icon indicating copy to clipboard operation
azure-policy copied to clipboard

Azure policy to deny creation of second diagnostic setting if the first one already exists.

Open gmounica39 opened this issue 1 year ago • 1 comments

Is there a way for Azure policy to deny creation of second diagnostic setting if the first one already exists?

I tried below, but it doesn't seem to work for my use case

{ "mode": "All", "policyRule": { "if": { "field": "type", "equals": "Microsoft.Insights/diagnosticSettings" }, "then": { "effect": "deny" } }, "parameters": {} }

I don't think we could do the audit policy too since we can't use Microsoft.Insights/diagnosticSettings[*] in the count field.

{ "mode": "All", "policyRule": { "if": { "allOf": [ { "field": "type", "equals": "Microsoft.Insights/diagnosticSettings" }, { "count": { "field": "Microsoft.Insights/diagnosticSettings[*]", "where": { "allOf": [ { "field": "Microsoft.Insights/diagnosticSettings/workspaceId", "exists": "true" } ] } }, "greater": 1 } ] }, "then": { "effect": "audit" } }, "parameters": {} }

This is erroring out saying

The 'field' property 'Microsoft.Insights/diagnosticSettings[*]' of the policy rule does not exist as an alias under provider 'Microsoft.Insights'.

gmounica39 avatar Nov 07 '23 21:11 gmounica39

Diagnostic settings profiles are created on a resource, and it is certainly possible to have more than one of them. You can use an existenceCondition in an AuditIfNotExists policy to check for a specific profile name on a specific resource type.

s4parke avatar Mar 28 '24 18:03 s4parke