Bylaw

Gallery › Monitoring › Azure Community Policy

Configure Application Insights table data retention (analytics and total retention)

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode All Version 1.0.0

Configures analytics (retentionInDays) and total (totalRetentionInDays) retention for the workspace-based Application Insights App* tables via DeployIfNotExists; create a remediation task to fix existing tables. Requires a managed identity with Log Analytics Contributor. Part of the Log Analytics data retention guardrails set (workspace, table, and App Insights policies); initiative and one-click deploy: https://github.com/claestom/law-retention-guardrails

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

Azure Policy JSONazurepolicy.json · 135 lines
{
  "name": "e9c3f6a5-5f67-4a1e-b1c2-7d0e1f2a3b45",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Configure Application Insights table data retention (analytics and total retention)",
    "mode": "All",
    "description": "Configures analytics (retentionInDays) and total (totalRetentionInDays) retention for the workspace-based Application Insights App* tables via DeployIfNotExists; create a remediation task to fix existing tables. Requires a managed identity with Log Analytics Contributor. Part of the Log Analytics data retention guardrails set (workspace, table, and App Insights policies); initiative and one-click deploy: https://github.com/claestom/law-retention-guardrails",
    "metadata": {
      "category": "Monitoring",
      "version": "1.0.0",
      "source": "https://learn.microsoft.com/azure/azure-monitor/app/data-model-complete"
    },
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "The policy effect. DeployIfNotExists remediates non-compliant tables, AuditIfNotExists only reports them, Disabled turns the policy off."
        },
        "allowedValues": [
          "DeployIfNotExists",
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists"
      },
      "tableRetentionInDays": {
        "type": "Integer",
        "metadata": {
          "displayName": "Table analytics retention (days)",
          "description": "Analytics (interactive) retention in days for the Application Insights tables. Allowed range is 4-730. Defaults to 90 (free with workspace-based Application Insights)."
        },
        "defaultValue": 90
      },
      "tableTotalRetentionInDays": {
        "type": "Integer",
        "metadata": {
          "displayName": "Table total retention (days)",
          "description": "Total retention in days (analytics + long-term) for the Application Insights tables. Valid values are 4-730, or 1095, 1460, 1826, 2191, 2556, 2922, 3288, 3653, 4018, 4383. Defaults to 90."
        },
        "defaultValue": 90
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.OperationalInsights/workspaces/tables"
          },
          {
            "field": "name",
            "in": [
              "AppAvailabilityResults",
              "AppBrowserTimings",
              "AppDependencies",
              "AppEvents",
              "AppExceptions",
              "AppMetrics",
              "AppPageViews",
              "AppPerformanceCounters",
              "AppRequests",
              "AppSystemEvents",
              "AppTraces"
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.OperationalInsights/workspaces/tables",
          "name": "[field('fullName')]",
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/92aaf0da-9dab-42b6-94a3-d43ce8d16293"
          ],
          "existenceCondition": {
            "allOf": [
              {
                "field": "Microsoft.OperationalInsights/workspaces/tables/retentionInDays",
                "equals": "[parameters('tableRetentionInDays')]"
              },
              {
                "field": "Microsoft.OperationalInsights/workspaces/tables/totalRetentionInDays",
                "equals": "[parameters('tableTotalRetentionInDays')]"
              }
            ]
          },
          "deployment": {
            "properties": {
              "mode": "incremental",
              "parameters": {
                "tableFullName": {
                  "value": "[field('fullName')]"
                },
                "retentionInDays": {
                  "value": "[parameters('tableRetentionInDays')]"
                },
                "totalRetentionInDays": {
                  "value": "[parameters('tableTotalRetentionInDays')]"
                }
              },
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "tableFullName": {
                    "type": "string"
                  },
                  "retentionInDays": {
                    "type": "int"
                  },
                  "totalRetentionInDays": {
                    "type": "int"
                  }
                },
                "resources": [
                  {
                    "type": "Microsoft.OperationalInsights/workspaces/tables",
                    "apiVersion": "2022-10-01",
                    "name": "[parameters('tableFullName')]",
                    "properties": {
                      "retentionInDays": "[parameters('retentionInDays')]",
                      "totalRetentionInDays": "[parameters('totalRetentionInDays')]"
                    }
                  }
                ]
              }
            }
          }
        }
      }
    }
  }
}
Bylawe9c3f6a5-5f67-4a1e-b1c2-7d0e1f2a3b45.bylaw.ps1 · 50 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy 'e9c3f6a5-5f67-4a1e-b1c2-7d0e1f2a3b45' {

    Properties @{
        DisplayName = 'Configure Application Insights table data retention (analytics and total retention)'
        Description = 'Configures analytics (retentionInDays) and total (totalRetentionInDays) retention for the workspace-based Application Insights App* tables via DeployIfNotExists; create a remediation task to fix existing tables. Requires a managed identity with Log Analytics Contributor. Part of the Log Analytics data retention guardrails set (workspace, table, and App Insights policies); initiative and one-click deploy: https://github.com/claestom/law-retention-guardrails'
        Category    = 'Monitoring'
        Version     = '1.0.0'
        Mode        = 'All'
        Metadata    = [ordered]@{ source = 'https://learn.microsoft.com/azure/azure-monitor/app/data-model-complete' }
    }

    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'The policy effect. DeployIfNotExists remediates non-compliant tables, AuditIfNotExists only reports them, Disabled turns the policy off.'
    }
    Parameter TableRetentionInDays @{
        Type        = 'Integer'
        Default     = 90
        DisplayName = 'Table analytics retention (days)'
        Description = 'Analytics (interactive) retention in days for the Application Insights tables. Allowed range is 4-730. Defaults to 90 (free with workspace-based Application Insights).'
    }
    Parameter TableTotalRetentionInDays @{
        Type        = 'Integer'
        Default     = 90
        DisplayName = 'Table total retention (days)'
        Description = 'Total retention in days (analytics + long-term) for the Application Insights tables. Valid values are 4-730, or 1095, 1460, 1826, 2191, 2556, 2922, 3288, 3653, 4018, 4383. Defaults to 90.'
    }

    Resource 'Microsoft.OperationalInsights/workspaces/tables' {

        Require 'name is not one of 11 values' {
            Field name | Must -Not -BeIn @('AppAvailabilityResults', 'AppBrowserTimings', 'AppDependencies', 'AppEvents', 'AppExceptions', 'AppMetrics', 'AppPageViews', 'AppPerformanceCounters', 'AppRequests', 'AppSystemEvents', 'AppTraces') -IfPresent
        }
    }

    DeployIfNotExists 'Microsoft.OperationalInsights/workspaces/tables' -Role 'Log Analytics Contributor' -Name (Arm '[field(''fullName'')]') -Effect (Param Effect) {
        Require 'retentionInDays and totalRetentionInDays are as required' {
            Field retentionInDays | Must -Be (Param TableRetentionInDays)
            Field totalRetentionInDays | Must -Be (Param TableTotalRetentionInDays)
        }
        Deployment -TemplateFile './templates/e9c3f6a5-5f67-4a1e-b1c2-7d0e1f2a3b45.json' -Parameters ([ordered]@{
                tableFullName        = (Arm '[field(''fullName'')]')
                retentionInDays      = (Param TableRetentionInDays)
                totalRetentionInDays = (Param TableTotalRetentionInDays)
            })
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/Monitoring/configure-appinsights-table-retention/azurepolicy.json in Azure/Community-Policy, under the MIT licence.