Bicep: Add dashboard with Kusto Query
Tags: azure, bicep, dashboard, kusto
Published: 2023-01-13 09:24:20

In this article, we share bicep to create Azure Dashboard that has a chart from Log Analytics Workspace.

main.bicep

```bash
@description('The location where the resource is created.')
param location string = resourceGroup().location

@description('The name of your log analytics workspace')
param logAnalyticsName string = 'la-${uniqueString(resourceGroup().id)}'

@description('The name of your dashboard')
param dashboardName string = 'dashboard-${uniqueString(resourceGroup().id)}'

module law './log_analytics.bicep' = {
  name: 'law_deployment'
  params: {
    location: location
    logAnalyticsWorkspace: logAnalyticsName
  }
}

module dashboard './dashboard.bicep' = {
  name: 'dashboard_deployment'
  params: {
    location: location
    dashboardName: dashboardName
    logAnalyticsName: logAnalyticsName
  }
}
```

log_analytics.bicep

```bash
param location string
param logAnalyticsWorkspace string

resource law 'Microsoft.OperationalInsights/workspaces@2022-10-01' = {
  name: logAnalyticsWorkspace
  location: location
}
```
dashboard.bicep

- Specify Log Analytics Workspace in Scope section
- Specify query in Query section
- Specify X-Axis, Y-Axis and SplitBy in Dimensions section

```bash
param dashboardName string
param location string
param logAnalyticsName string

resource dashboard 'Microsoft.Portal/dashboards@2020-09-01-preview' = {
  properties: {
    lenses: [
      {
        order: 0
        parts: [
          {
            position: {
              x: 0
              y: 0
              colSpan: 6
              rowSpan: 4
            }
            metadata: {
              inputs: [
                {
                  name: 'resourceTypeMode'
                  isOptional: true
                }
                {
                  name: 'ComponentId'
                  isOptional: true
                }
                {
                  name: 'Scope'
                  value: {
                    resourceIds: [
                      '/subscriptions/${subscription().subscriptionId}/resourceGroups/${resourceGroup().name}/providers/microsoft.operationalinsights/workspaces/${logAnalyticsName}'
                    ]
                  }
                  isOptional: true
                }
                {
                  name: 'PartId'
                  value: '4246be30-72ef-406c-83b9-f22c19279bee'
                  isOptional: true
                }
                {
                  name: 'Version'
                  value: '2.0'
                  isOptional: true
                }
                {
                  name: 'TimeRange'
                  value: 'P30D'
                  isOptional: true
                }
                {
                  name: 'DashboardId'
                  isOptional: true
                }
                {
                  name: 'DraftRequestParameters'
                  isOptional: true
                }
                {
                  name: 'Query'
                  value: 'Usage | where TimeGenerated > startofday(ago(31d)) | where StartTime > startofday(ago(31d)) | where IsBillable == true | summarize TotalVolumeGB = sum(Quantity) / 1000 by bin(StartTime, 1d), Solution'
                  isOptional: true
                }
                {
                  name: 'ControlType'
                  value: 'FrameControlChart'
                  isOptional: true
                }
                {
                  name: 'SpecificChart'
                  value: 'columnchart'
                  isOptional: true
                }
                {
                  name: 'PartTitle'
                  value: 'Log Analytics Cost'
                  isOptional: true
                }
                {
                  name: 'Dimensions'
                  value: {
                    xAxis: {
                      name: 'TimeGenerated'
                      type: 'datetime'
                    }
                    yAxis: [
                      {
                        name: 'TotalVolumeGB'
                        type: 'real'
                      }
                    ]
                    splitBy: [
                      {
                        name: 'Solution'
                        type: 'string'
                      }
                    ]
                    aggregation: 'Sum'
                  }
                  isOptional: true
                }
                {
                  name: 'LegendOptions'
                  value: {
                    isEnabled: true
                    position: 'Bottom'
                  }
                  isOptional: true
                }
                {
                  name: 'IsQueryContainTimeRange'
                  value: true
                  isOptional: true
                }
              ]
              type: 'Extension/Microsoft_OperationsManagementSuite_Workspace/PartType/LogsDashboardPart'
              settings: {}
            }
          }
        ]
        metadata: {}
      }
    ]
    metadata: {}
  }
  name: dashboardName
  location: location
}
```

## How to figure out dashboard parameters

We can simply download the dashboard definitions from Azure Portal.  It is an ARM template json, but we can easily find values for each property.