Bylaw

Gallery › Monitoring › Azure Community Policy

Deploy Diagnostic Settings for Batch Account to Event Hub

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode Indexed Version 2.0.0

Deploys the diagnostic settings for Batch Account to stream to a regional Event Hub when any Batch Account which is missing this diagnostic settings is created or updated. Amended built-in policy to add eventHubName-parameter to stream to a specific event hub.

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

Azure Policy JSONazurepolicy.json · 229 lines
{
  "name": "3c2d201e-17ab-4f18-ba74-66733f380b85",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Deploy Diagnostic Settings for Batch Account to Event Hub",
    "description": "Deploys the diagnostic settings for Batch Account to stream to a regional Event Hub when any Batch Account which is missing this diagnostic settings is created or updated. Amended built-in policy to add eventHubName-parameter to stream to a specific event hub.",
    "metadata": {
      "version": "2.0.0",
      "category": "Monitoring"
    },
    "mode": "Indexed",
    "parameters": {
      "effect": {
        "type": "String",
        "defaultValue": "DeployIfNotExists",
        "allowedValues": [
          "DeployIfNotExists",
          "AuditIfNotExists",
          "Disabled"
        ],
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        }
      },
      "profileName": {
        "type": "string",
        "defaultValue": "setbypolicy_EH",
        "metadata": {
          "displayName": "Profile name",
          "description": "The diagnostic settings profile name"
        }
      },
      "eventHubRuleId": {
        "type": "string",
        "metadata": {
          "displayName": "Event Hub Authorization Rule Id",
          "description": "The Event Hub authorization rule Id for Azure Diagnostics. The authorization rule needs to be at Event Hub namespace level. e.g. /subscriptions/{subscription Id}/resourceGroups/{resource group}/providers/Microsoft.EventHub/namespaces/{Event Hub namespace}/authorizationrules/{authorization rule}",
          "strongType": "Microsoft.EventHub/Namespaces/AuthorizationRules",
          "assignPermissions": true
        }
      },
      "eventHubLocation": {
        "type": "String",
        "metadata": {
          "displayName": "Event Hub Location",
          "description": "The location the Event Hub resides in. Only Batch Accounts in this location will be linked to this Event Hub.",
          "strongType": "location"
        },
        "defaultValue": ""
      },
      "eventHubName": {
        "type": "String",
        "metadata": {
          "displayName": "Event Hub Name",
          "description": "Specify the name of the Event Hub"
        }
      },
      "metricsEnabled": {
        "type": "string",
        "defaultValue": "False",
        "allowedValues": [
          "True",
          "False"
        ],
        "metadata": {
          "displayName": "Enable metrics",
          "description": "Whether to enable metrics stream to the Event Hub - True or False"
        }
      },
      "logsEnabled": {
        "type": "string",
        "defaultValue": "True",
        "allowedValues": [
          "True",
          "False"
        ],
        "metadata": {
          "displayName": "Enable logs",
          "description": "Whether to enable logs stream to the Event Hub  - True or False"
        }
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Batch/batchAccounts"
          },
          {
            "anyOf": [
              {
                "value": "[parameters('eventHubLocation')]",
                "equals": ""
              },
              {
                "field": "location",
                "equals": "[parameters('eventHubLocation')]"
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Insights/diagnosticSettings",
          "name": "[parameters('profileName')]",
          "existenceCondition": {
            "allOf": [
              {
                "field": "Microsoft.Insights/diagnosticSettings/logs.enabled",
                "equals": "[parameters('logsEnabled')]"
              },
              {
                "field": "Microsoft.Insights/diagnosticSettings/metrics.enabled",
                "equals": "[parameters('metricsEnabled')]"
              },
              {
                "field": "Microsoft.Insights/diagnosticSettings/eventHubAuthorizationRuleId",
                "matchInsensitively": "[parameters('eventHubRuleId')]"
              },
              {
                "field": "Microsoft.Insights/diagnosticSettings/eventHubName",
                "matchInsensitively": "[parameters('eventHubName')]"
              }
            ]
          },
          "roleDefinitionIds": [
            "/providers/microsoft.authorization/roleDefinitions/f526a384-b230-433a-b45c-95f59c4a2dec",
            "/providers/microsoft.authorization/roleDefinitions/92aaf0da-9dab-42b6-94a3-d43ce8d16293"
          ],
          "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"
                  },
                  "location": {
                    "type": "string"
                  },
                  "eventHubRuleId": {
                    "type": "string"
                  },
                  "eventHubName": {
                    "type": "string"
                  },
                  "metricsEnabled": {
                    "type": "string"
                  },
                  "logsEnabled": {
                    "type": "string"
                  },
                  "profileName": {
                    "type": "string"
                  }
                },
                "variables": {},
                "resources": [
                  {
                    "type": "Microsoft.Batch/batchAccounts/providers/diagnosticSettings",
                    "apiVersion": "2021-05-01-preview",
                    "name": "[concat(parameters('resourceName'), '/', 'Microsoft.Insights/', parameters('profileName'))]",
                    "location": "[parameters('location')]",
                    "dependsOn": [],
                    "properties": {
                      "eventHubAuthorizationRuleId": "[parameters('eventHubRuleId')]",
                      "eventHubName": "[parameters('eventHubName')]",
                      "metrics": [
                        {
                          "category": "AllMetrics",
                          "enabled": "[parameters('metricsEnabled')]",
                          "retentionPolicy": {
                            "enabled": false,
                            "days": 0
                          }
                        }
                      ],
                      "logs": [
                        {
                          "categoryGroup": "allLogs",
                          "enabled": "[parameters('logsEnabled')]"
                        }
                      ]
                    }
                  }
                ],
                "outputs": {
                  "policy": {
                    "type": "string",
                    "value": "[concat('Diagnostic setting ', parameters('profileName'), ' for type Batch Account (Microsoft.Batch/batchAccounts), resourceName ', parameters('resourceName'), ' to EventHub ', parameters('eventHubRuleId'), ':', parameters('eventHubName'), ' configured')]"
                  }
                }
              },
              "parameters": {
                "location": {
                  "value": "[field('location')]"
                },
                "resourceName": {
                  "value": "[field('name')]"
                },
                "eventHubRuleId": {
                  "value": "[parameters('eventHubRuleId')]"
                },
                "eventHubName": {
                  "value": "[parameters('eventHubName')]"
                },
                "metricsEnabled": {
                  "value": "[parameters('metricsEnabled')]"
                },
                "logsEnabled": {
                  "value": "[parameters('logsEnabled')]"
                },
                "profileName": {
                  "value": "[parameters('profileName')]"
                }
              }
            }
          }
        }
      }
    }
  }
}
Bylaw3c2d201e-17ab-4f18-ba74-66733f380b85.bylaw.ps1 · 84 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '3c2d201e-17ab-4f18-ba74-66733f380b85' {

    Properties @{
        DisplayName = 'Deploy Diagnostic Settings for Batch Account to Event Hub'
        Description = 'Deploys the diagnostic settings for Batch Account to stream to a regional Event Hub when any Batch Account which is missing this diagnostic settings is created or updated. Amended built-in policy to add eventHubName-parameter to stream to a specific event hub.'
        Version     = '2.0.0'
        Category    = 'Monitoring'
        Mode        = 'Indexed'
    }

    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }
    Parameter ProfileName @{
        Type        = 'string'
        Default     = 'setbypolicy_EH'
        DisplayName = 'Profile name'
        Description = 'The diagnostic settings profile name'
    }
    Parameter EventHubRuleId @{
        Type        = 'string'
        DisplayName = 'Event Hub Authorization Rule Id'
        Description = 'The Event Hub authorization rule Id for Azure Diagnostics. The authorization rule needs to be at Event Hub namespace level. e.g. /subscriptions/{subscription Id}/resourceGroups/{resource group}/providers/Microsoft.EventHub/namespaces/{Event Hub namespace}/authorizationrules/{authorization rule}'
        Metadata    = [ordered]@{ strongType = 'Microsoft.EventHub/Namespaces/AuthorizationRules'; assignPermissions = $true }
    }
    Parameter EventHubLocation @{
        Default     = ''
        DisplayName = 'Event Hub Location'
        Description = 'The location the Event Hub resides in. Only Batch Accounts in this location will be linked to this Event Hub.'
        Metadata    = [ordered]@{ strongType = 'location' }
    }
    Parameter EventHubName @{
        Type        = 'String'
        DisplayName = 'Event Hub Name'
        Description = 'Specify the name of the Event Hub'
    }
    Parameter MetricsEnabled @{
        Type          = 'string'
        AllowedValues = @('True', 'False')
        Default       = 'False'
        DisplayName   = 'Enable metrics'
        Description   = 'Whether to enable metrics stream to the Event Hub - True or False'
    }
    Parameter LogsEnabled @{
        Type          = 'string'
        AllowedValues = @('True', 'False')
        Default       = 'True'
        DisplayName   = 'Enable logs'
        Description   = 'Whether to enable logs stream to the Event Hub  - True or False'
    }

    Resource 'Microsoft.Batch/batchAccounts' {

        Require 'Requirement 1' {
            Param EventHubLocation | Must -Not -Be ''
        }

        Require 'location is not the EventHubLocation parameter' {
            Field location | Must -Not -Be (Param EventHubLocation) -IfPresent
        }
    }

    DeployIfNotExists 'Microsoft.Insights/diagnosticSettings' -Role 'Azure Event Hubs Data Owner', 'Log Analytics Contributor' -Name (Param ProfileName) -Effect (Param Effect) {
        Require 'logs.enabled, metrics.enabled, eventHubAuthorizationRuleId and 1 more are as required' {
            Field logs.enabled | Must -Be (Param LogsEnabled)
            Field metrics.enabled | Must -Be (Param MetricsEnabled)
            Field eventHubAuthorizationRuleId | Must -Match (Param EventHubRuleId)
            Field eventHubName | Must -Match (Param EventHubName)
        }
        Deployment -TemplateFile './templates/3c2d201e-17ab-4f18-ba74-66733f380b85.json' -Parameters ([ordered]@{
                location       = (Arm '[field(''location'')]')
                resourceName   = (Arm '[field(''name'')]')
                eventHubRuleId = (Param EventHubRuleId)
                eventHubName   = (Param EventHubName)
                metricsEnabled = (Param MetricsEnabled)
                logsEnabled    = (Param LogsEnabled)
                profileName    = (Param ProfileName)
            })
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/Monitoring/To Event Hub/Deploy Diagnostic Settings for Batch Account to Event Hub/azurepolicy.json in Azure/Community-Policy, under the MIT licence.