Bylaw

Gallery › Monitoring › Azure Community Policy

Deploy Activity Log Alert for the selected category and operation name

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode All Version 1.0.0

Deploy Activity Log Alert for the selected category and operation name

ConvertTo-Bylaw converted this policy, and proved that the file compiles back to the same definition.

Azure Policy JSONazurepolicy.json · 328 lines
{
  "name": "1a30544f-d89d-42ee-9e74-7f8176772c3d",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Deploy Activity Log Alert for the selected category and operation name",
    "description": "Deploy Activity Log Alert for the selected category and operation name",
    "metadata": {
      "version": "1.0.0",
      "category": "Monitoring"
    },
    "mode": "All",
    "parameters": {
        "actionGroupResourceId": {
            "type": "string",
            "metadata": {
                "description": "Name for the Action group.",
                "displayName": "Action Group Resource ID"
            }
        },
        "activityLogAlertName": {
            "type": "string",
            "metadata": {
                "description": "Name for the Activity log alert.",
                "displayName": "Alert Name"
            }
        },
        "appendSubscriptionNameAsPrefix": {
            "type": "boolean",
            "metadata": {
                "description": "Append the Subscription Name to the parameter for the name of the Activity log alert and for the rg name.",
                "displayName": "Append Subscription Name as Prefix"
            },
            "defaultValue": false,
            "allowedValues":[
              true,
              false
            ]
        },
        "activityLogCategory": {
            "type": "string",
            "metadata": {
                "description": "Category for the Activity log alert.",
                "displayName": "Alert Category"
            },
            "allowedValues": [
                "Administrative",
                "Security",
                "ServiceHealth",
                "Alert",
                "Recommendation",
                "Policy",
                "Autoscale",
                "ResourceHealth"
            ],
            "defaultValue": "Administrative"
        },
        "activityLogOperationName": {
            "type": "string",
            "metadata": {
                "description": "Operation Name for the Activity log alert. format: Microsoft.Provider/resourceType/actionOrOperation examples: Microsoft.Sql/servers/firewallRules/write, Microsoft.Security/policies/write",
                "displayName": "Alert Operation Name"
            }
        },
        "activityLogAlertResourceGroupName": {
            "type": "string",
            "metadata": {
                "description": "This is the name of the Resource Group that will contain the Activity log alert resource.",
                "displayName": "Alert Resource Group Name"
            }
        },
        "activityLogAlertResourceGroupLocation": {
            "type": "string",
            "metadata": {
                "description": "This is the location of the Resource Group that will contain the Activity log alert resource.",
                "displayName": "Alert Resource Group Location",
                "strongType": "location"
            },
            "defaultValue": "northeurope"
        },
        "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/activityLogAlerts",
                "roleDefinitionIds": [
                    "/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
                ],
                "existenceCondition": {
                    "allOf": [
                        {
                            "field": "Microsoft.Insights/ActivityLogAlerts/enabled",
                            "equals": "true"
                        },
                        {
                            "count": {
                                "field": "Microsoft.Insights/ActivityLogAlerts/condition.allOf[*]",
                                "where": {
                                    "anyOf": [
                                        {
                                            "allOf": [
                                                {
                                                    "field": "Microsoft.Insights/ActivityLogAlerts/condition.allOf[*].field",
                                                    "equals": "category"
                                                },
                                                {
                                                    "field": "Microsoft.Insights/ActivityLogAlerts/condition.allOf[*].equals",
                                                    "equals": "[parameters('activityLogCategory')]"
                                                }
                                            ]
                                        },
                                        {
                                            "allOf": [
                                                {
                                                    "field": "Microsoft.Insights/ActivityLogAlerts/condition.allOf[*].field",
                                                    "equals": "operationName"
                                                },
                                                {
                                                    "field": "Microsoft.Insights/ActivityLogAlerts/condition.allOf[*].equals",
                                                    "equals": "[parameters('activityLogOperationName')]"
                                                }
                                            ]
                                        }
                                    ]
                                }
                            },
                            "equals": 2
                        },
                        {
                            "not": {
                                "field": "Microsoft.Insights/ActivityLogAlerts/condition.allOf[*].field",
                                "equals": "category"
                            }
                        },
                        {
                            "not": {
                                "field": "Microsoft.Insights/ActivityLogAlerts/condition.allOf[*].field",
                                "equals": "operationName"
                            }
                        }
                    ]
                },
                "deploymentScope": "subscription",
                "resourceGroupName":"[if(parameters('appendSubscriptionNameAsPrefix'), concat(subscription().displayName,'-', parameters('activityLogAlertResourceGroupName')), parameters('activityLogAlertResourceGroupName'))]",
                "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": {
                                "activityLogAlertResourceGroupName": {
                                    "type": "string"
                                },
                                "activityLogAlertResourceGroupLocation": {
                                    "type": "string"
                                },
                                "actionGroupResourceId": {
                                    "type": "string"
                                },
                                "activityLogAlertName": {
                                    "type": "string"
                                },
                                "activityLogCategory": {
                                    "type": "string"
                                },
                                "activityLogOperationName": {
                                    "type": "string"
                                },
                                "appendSubscriptionNameAsPrefix": {
                                    "type": "bool"
                                }
                            },
                            "variables": {
                                "createRg": "[concat('deployRG-', uniqueString(deployment().name))]",
                                "createAlert": "[concat('deployAlert-', uniqueString(deployment().name))]",
                                "alertRGNAme": "[if(parameters('appendSubscriptionNameAsPrefix'), concat(subscription().displayName,'-', parameters('activityLogAlertResourceGroupName')), parameters('activityLogAlertResourceGroupName'))]"
                            },
                            "resources": [
                                {
                                    "type": "Microsoft.Resources/resourceGroups",
                                    "apiVersion": "2022-09-01",
                                    "name": "[variables('alertRGNAme')]",
                                    "location": "[parameters('activityLogAlertResourceGroupLocation')]"
                                },
                                {
                                    "type": "Microsoft.Resources/deployments",
                                    "apiVersion": "2020-06-01",
                                    "name": "[variables('createAlert')]",
                                    "resourceGroup": "[variables('alertRGNAme')]",
                                    "dependsOn": [
                                        "[resourceId('Microsoft.Resources/resourceGroups', variables('alertRGNAme'))]"
                                    ],
                                    "properties": {
                                        "expressionEvaluationOptions": {
                                            "scope": "inner"
                                        },
                                        "mode": "Incremental",
                                        "parameters": {
                                            "activityLogAlertName": {
                                                "value": "[trim(parameters('activityLogAlertName'))]"
                                            },
                                            "actionGroupResourceId": {
                                                "value": "[trim(parameters('actionGroupResourceId'))]"
                                            },
                                            "activityLogCategory": {
                                                "value": "[parameters('activityLogCategory')]"
                                            },
                                            "activityLogOperationName": {
                                                "value": "[trim(parameters('activityLogOperationName'))]"
                                            },
                                            "appendSubscriptionNameAsPrefix": {
                                                "value": "[parameters('appendSubscriptionNameAsPrefix')]"
                                            }
                                        },
                                        "template": {
                                            "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                                            "contentVersion": "1.0.0.0",
                                            "parameters": {
                                                "actionGroupResourceId": {
                                                    "type": "string"
                                                },
                                                "activityLogAlertName": {
                                                    "type": "string"
                                                },
                                                "activityLogCategory": {
                                                    "type": "string"
                                                },
                                                "activityLogOperationName": {
                                                    "type": "string"
                                                },
                                                "appendSubscriptionNameAsPrefix": {
                                                    "type": "bool"
                                                }
                                            },
                                            "variables": {
                                                "alertName": "[if(parameters('appendSubscriptionNameAsPrefix'), concat(subscription().displayName,'-', parameters('activityLogAlertName')), parameters('activityLogAlertName'))]"
                                            },
                                            "resources": [
                                                {
                                                    "type": "Microsoft.Insights/activityLogAlerts",
                                                    "apiVersion": "2017-04-01",
                                                    "name": "[substring(variables('alertName') ,0 , min(createArray(length(variables('alertName')), 259)))]",
                                                    "location": "Global",
                                                    "properties": {
                                                        "enabled": true,
                                                        "scopes": [
                                                            "[subscription().id]"
                                                        ],
                                                        "condition": {
                                                            "allOf": [
                                                                {
                                                                    "field": "category",
                                                                    "equals": "[parameters('activityLogCategory')]"
                                                                },
                                                                {
                                                                    "field": "operationName",
                                                                    "equals": "[parameters('activityLogOperationName')]"
                                                                }
                                                            ]
                                                        },
                                                        "actions": {
                                                            "actionGroups": [
                                                                {
                                                                    "actionGroupId": "[parameters('actionGroupResourceId')]"
                                                                }
                                                            ]
                                                        }
                                                    }
                                                }
                                            ]
                                        }
                                    }
                                }
                            ]
                        },
                        "parameters": {
                            "activityLogAlertName": {
                                "value": "[trim(parameters('activityLogAlertName'))]"
                            },
                            "actionGroupResourceId": {
                                "value": "[trim(parameters('actionGroupResourceId'))]"
                            },
                            "activityLogCategory": {
                                "value": "[parameters('activityLogCategory')]"
                            },
                            "activityLogOperationName": {
                                "value": "[trim(parameters('activityLogOperationName'))]"
                            },
                            "appendSubscriptionNameAsPrefix": {
                                "value": "[parameters('appendSubscriptionNameAsPrefix')]"
                            },
                            "activityLogAlertResourceGroupName": {
                                "value": "[parameters('activityLogAlertResourceGroupName')]"
                            },
                            "activityLogAlertResourceGroupLocation": {
                                "value": "[parameters('activityLogAlertResourceGroupLocation')]"
                            }
                        }
                    }
                }
            }
        }
    }
  }
}
Bylaw1a30544f-d89d-42ee-9e74-7f8176772c3d.bylaw.ps1 · 105 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '1a30544f-d89d-42ee-9e74-7f8176772c3d' {

    Properties @{
        DisplayName = 'Deploy Activity Log Alert for the selected category and operation name'
        Description = 'Deploy Activity Log Alert for the selected category and operation name'
        Version     = '1.0.0'
        Category    = 'Monitoring'
        Mode        = 'All'
    }

    Parameter ActionGroupResourceId @{
        Type        = 'string'
        DisplayName = 'Action Group Resource ID'
        Description = 'Name for the Action group.'
    }
    Parameter ActivityLogAlertName @{
        Type        = 'string'
        DisplayName = 'Alert Name'
        Description = 'Name for the Activity log alert.'
    }
    Parameter AppendSubscriptionNameAsPrefix @{
        Type          = 'boolean'
        AllowedValues = @($true, $false)
        Default       = $false
        DisplayName   = 'Append Subscription Name as Prefix'
        Description   = 'Append the Subscription Name to the parameter for the name of the Activity log alert and for the rg name.'
    }
    Parameter ActivityLogCategory @{
        Type          = 'string'
        AllowedValues = @('Administrative', 'Security', 'ServiceHealth', 'Alert', 'Recommendation', 'Policy', 'Autoscale', 'ResourceHealth')
        Default       = 'Administrative'
        DisplayName   = 'Alert Category'
        Description   = 'Category for the Activity log alert.'
    }
    Parameter ActivityLogOperationName @{
        Type        = 'string'
        DisplayName = 'Alert Operation Name'
        Description = 'Operation Name for the Activity log alert. format: Microsoft.Provider/resourceType/actionOrOperation examples: Microsoft.Sql/servers/firewallRules/write, Microsoft.Security/policies/write'
    }
    Parameter ActivityLogAlertResourceGroupName @{
        Type        = 'string'
        DisplayName = 'Alert Resource Group Name'
        Description = 'This is the name of the Resource Group that will contain the Activity log alert resource.'
    }
    Parameter ActivityLogAlertResourceGroupLocation @{
        Type        = 'string'
        Default     = 'northeurope'
        DisplayName = 'Alert Resource Group Location'
        Description = 'This is the location of the Resource Group that will contain the Activity log alert resource.'
        Metadata    = [ordered]@{ strongType = 'location' }
    }
    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/activityLogAlerts' -Role 'Contributor' -ResourceGroupName (Arm '[if(parameters(''appendSubscriptionNameAsPrefix''), concat(subscription().displayName,''-'', parameters(''activityLogAlertResourceGroupName'')), parameters(''activityLogAlertResourceGroupName''))]') -Effect (Param Effect) {
        Require 'enabled, condition.allOf, field and 1 more are as required' {
            Field enabled | Must -Be 'true'
            Count 'condition.allOf[*]' -Where {
                AnyOf {
                    AllOf {
                        Field field | Must -Be 'category'
                        Field equals | Must -Be (Param ActivityLogCategory)
                    }
                    AllOf {
                        Field field | Must -Be 'operationName'
                        Field equals | Must -Be (Param ActivityLogOperationName)
                    }
                }
            } | Must -Be 2
            AnyOf {
                Field condition.allOf | Must -Not -Exist
                Count 'condition.allOf[*]' -Where {
                    Field field | Must -Not -Be 'category'
                } | Must -BeGreaterThan 0
            }
            AnyOf {
                Field condition.allOf | Must -Not -Exist
                Count 'condition.allOf[*]' -Where {
                    Field field | Must -Not -Be 'operationName'
                } | Must -BeGreaterThan 0
            }
        }
        Deployment @{
            TemplateFile = './templates/1a30544f-d89d-42ee-9e74-7f8176772c3d.json'
            Scope        = 'Subscription'
            Location     = 'eastus'
            Parameters   = [ordered]@{
                activityLogAlertName                  = (Arm '[trim(parameters(''activityLogAlertName''))]')
                actionGroupResourceId                 = (Arm '[trim(parameters(''actionGroupResourceId''))]')
                activityLogCategory                   = (Param ActivityLogCategory)
                activityLogOperationName              = (Arm '[trim(parameters(''activityLogOperationName''))]')
                appendSubscriptionNameAsPrefix        = (Param AppendSubscriptionNameAsPrefix)
                activityLogAlertResourceGroupName     = (Param ActivityLogAlertResourceGroupName)
                activityLogAlertResourceGroupLocation = (Param ActivityLogAlertResourceGroupLocation)
            }
        }
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/Monitoring/deploy-activity-log-for-selected-category-and-operation-name/azurepolicy.json in Azure/Community-Policy, under the MIT licence.