Bylaw

Gallery › Container Instances › Azure built-in policies

Configure diagnostics for container group to log analytics workspace

Converted Policy Append Disabled Mode All Version 1.0.0

Appends the specified log analytics workspaceId and workspaceKey when any container group which is missing these fields is created or updated. Does not modify the fields of container groups created before this policy was applied until those resource groups are changed.

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

Azure Policy JSONContainerInstance_LogAnalytics_Append.json · 77 lines
{
  "properties": {
    "displayName": "Configure diagnostics for container group to log analytics workspace",
    "policyType": "BuiltIn",
    "mode": "All",
    "description": "Appends the specified log analytics workspaceId and workspaceKey when any container group which is missing these fields is created or updated. Does not modify the fields of container groups created before this policy was applied until those resource groups are changed.",
    "metadata": {
      "version": "1.0.0",
      "category": "Container Instances"
    },
    "version": "1.0.0",
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        },
        "allowedValues": [
          "Append",
          "Disabled"
        ],
        "defaultValue": "Append"
      },
      "workspaceId": {
        "type": "String",
        "metadata": {
          "displayName": "Workspace ID",
          "description": "Log Analytics workspace GUID"
        }
      },
      "workspaceKey": {
        "type": "String",
        "metadata": {
          "displayName": "Workspace Key",
          "description": "Log Analytics workspace key"
        }
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.ContainerInstance/containerGroups"
          },
          {
            "field": "Microsoft.ContainerInstance/containerGroups/diagnostics.logAnalytics.workspaceId",
            "exists": "false"
          },
          {
            "field": "Microsoft.ContainerInstance/containerGroups/diagnostics.logAnalytics.workspaceKey",
            "exists": "false"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": [
          {
            "field": "Microsoft.ContainerInstance/containerGroups/diagnostics.logAnalytics.workspaceId",
            "value": "[parameters('workspaceId')]"
          },
          {
            "field": "Microsoft.ContainerInstance/containerGroups/diagnostics.logAnalytics.workspaceKey",
            "value": "[parameters('workspaceKey')]"
          }
        ]
      }
    },
    "versions": [
      "1.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/21c469fa-a887-4363-88a9-60bfd6911a15",
  "name": "21c469fa-a887-4363-88a9-60bfd6911a15"
}
Bylaw21c469fa-a887-4363-88a9-60bfd6911a15.bylaw.ps1 · 45 lines
# Converted from ContainerInstance_LogAnalytics_Append.json by ConvertTo-Bylaw.
Policy '21c469fa-a887-4363-88a9-60bfd6911a15' {

    Properties @{
        DisplayName   = 'Configure diagnostics for container group to log analytics workspace'
        Description   = 'Appends the specified log analytics workspaceId and workspaceKey when any container group which is missing these fields is created or updated. Does not modify the fields of container groups created before this policy was applied until those resource groups are changed.'
        Version       = '1.0.0'
        Category      = 'Container Instances'
        PolicyVersion = '1.0.0'
        Mode          = 'All'
    }

    Parameter Effect @{
        AllowedValues = @('Append', 'Disabled')
        Default       = 'Append'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }
    Parameter WorkspaceId @{
        Type        = 'String'
        DisplayName = 'Workspace ID'
        Description = 'Log Analytics workspace GUID'
    }
    Parameter WorkspaceKey @{
        Type        = 'String'
        DisplayName = 'Workspace Key'
        Description = 'Log Analytics workspace key'
    }

    Resource 'Microsoft.ContainerInstance/containerGroups' {

        When {
            Field diagnostics.logAnalytics.workspaceId | Must -Not -Exist
        }

        Require 'diagnostics.logAnalytics.workspaceKey exists' {
            Field diagnostics.logAnalytics.workspaceKey | Must -Exist
        }
    }

    Append -Effect (Param Effect) {
        Field diagnostics.logAnalytics.workspaceId | Add (Param WorkspaceId)
        Field diagnostics.logAnalytics.workspaceKey | Add (Param WorkspaceKey)
    }
}

Source: built-in-policies/policyDefinitions/Container Instances/ContainerInstance_LogAnalytics_Append.json in Azure/azure-policy, under the MIT licence.