Bylaw

Gallery › Monitoring › Azure built-in policies

Deploy Diagnostic Settings for Logic Apps to Event Hub

Converted Policy DeployIfNotExists Disabled Mode Indexed Version 2.0.0

Deploys the diagnostic settings for Logic Apps to stream to a regional Event Hub when any Logic Apps which is missing this diagnostic settings is created or updated.

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

Azure Policy JSONLogicApps_DeployDiagnosticLog_Deploy_EventHub.json · 205 lines
{
  "properties": {
    "displayName": "Deploy Diagnostic Settings for Logic Apps to Event Hub",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Deploys the diagnostic settings for Logic Apps to stream to a regional Event Hub when any Logic Apps which is missing this diagnostic settings is created or updated.",
    "metadata": {
      "version": "2.0.0",
      "category": "Monitoring"
    },
    "version": "2.0.0",
    "parameters": {
      "effect": {
        "type": "string",
        "defaultValue": "DeployIfNotExists",
        "allowedValues": [
          "DeployIfNotExists",
          "Disabled"
        ],
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        }
      },
      "profileName": {
        "type": "string",
        "defaultValue": "setbypolicy_eventHub",
        "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 Logic Apps in this location will be linked to this Event Hub.",
          "strongType": "location"
        },
        "defaultValue": ""
      },
      "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.Logic/workflows"
          },
          {
            "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')]"
              }
            ]
          },
          "roleDefinitionIds": [
            "/providers/microsoft.authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
          ],
          "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"
                  },
                  "metricsEnabled": {
                    "type": "string"
                  },
                  "logsEnabled": {
                    "type": "string"
                  },
                  "profileName": {
                    "type": "string"
                  }
                },
                "variables": {},
                "resources": [
                  {
                    "type": "Microsoft.Logic/workflows/providers/diagnosticSettings",
                    "apiVersion": "2017-05-01-preview",
                    "name": "[concat(parameters('resourceName'), '/', 'Microsoft.Insights/', parameters('profileName'))]",
                    "location": "[parameters('location')]",
                    "dependsOn": [],
                    "properties": {
                      "eventHubAuthorizationRuleId": "[parameters('eventHubRuleId')]",
                      "metrics": [
                        {
                          "category": "AllMetrics",
                          "enabled": "[parameters('metricsEnabled')]",
                          "retentionPolicy": {
                            "enabled": false,
                            "days": 0
                          }
                        }
                      ],
                      "logs": [
                        {
                          "category": "WorkflowRuntime",
                          "enabled": "[parameters('logsEnabled')]"
                        }
                      ]
                    }
                  }
                ],
                "outputs": {}
              },
              "parameters": {
                "location": {
                  "value": "[field('location')]"
                },
                "resourceName": {
                  "value": "[field('name')]"
                },
                "eventHubRuleId": {
                  "value": "[parameters('eventHubRuleId')]"
                },
                "metricsEnabled": {
                  "value": "[parameters('metricsEnabled')]"
                },
                "logsEnabled": {
                  "value": "[parameters('logsEnabled')]"
                },
                "profileName": {
                  "value": "[parameters('profileName')]"
                }
              }
            }
          }
        }
      }
    },
    "versions": [
      "2.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/a1dae6c7-13f3-48ea-a149-ff8442661f60",
  "name": "a1dae6c7-13f3-48ea-a149-ff8442661f60"
}
Bylawa1dae6c7-13f3-48ea-a149-ff8442661f60.bylaw.ps1 · 78 lines
# Converted from LogicApps_DeployDiagnosticLog_Deploy_EventHub.json by ConvertTo-Bylaw.
Policy 'a1dae6c7-13f3-48ea-a149-ff8442661f60' {

    Properties @{
        DisplayName   = 'Deploy Diagnostic Settings for Logic Apps to Event Hub'
        Description   = 'Deploys the diagnostic settings for Logic Apps to stream to a regional Event Hub when any Logic Apps which is missing this diagnostic settings is created or updated.'
        Version       = '2.0.0'
        Category      = 'Monitoring'
        PolicyVersion = '2.0.0'
        Mode          = 'Indexed'
    }

    Parameter Effect @{
        Type          = 'string'
        AllowedValues = @('DeployIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }
    Parameter ProfileName @{
        Type        = 'string'
        Default     = 'setbypolicy_eventHub'
        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 Logic Apps in this location will be linked to this Event Hub.'
        Metadata    = [ordered]@{ strongType = 'location' }
    }
    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.Logic/workflows' {

        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 'Contributor' -Name (Param ProfileName) -Effect (Param Effect) {
        Require 'logs.enabled is the LogsEnabled parameter and metrics.enabled is the MetricsEnabled parameter' {
            Field logs.enabled | Must -Be (Param LogsEnabled)
            Field metrics.enabled | Must -Be (Param MetricsEnabled)
        }
        Deployment -TemplateFile './templates/a1dae6c7-13f3-48ea-a149-ff8442661f60.json' -Parameters ([ordered]@{
                location       = (Arm '[field(''location'')]')
                resourceName   = (Arm '[field(''name'')]')
                eventHubRuleId = (Param EventHubRuleId)
                metricsEnabled = (Param MetricsEnabled)
                logsEnabled    = (Param LogsEnabled)
                profileName    = (Param ProfileName)
            })
    }
}

Its deployment templates are written beside it:

Source: built-in-policies/policyDefinitions/Monitoring/LogicApps_DeployDiagnosticLog_Deploy_EventHub.json in Azure/azure-policy, under the MIT licence.