DEV Community

Kenichiro Nakamura
Kenichiro Nakamura

Posted on

Bicep: Add dashboard with Kusto Query

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

main.bicep

@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
  }
}
Enter fullscreen mode Exit fullscreen mode

log_analytics.bicep

param location string
param logAnalyticsWorkspace string

resource law 'Microsoft.OperationalInsights/workspaces@2022-10-01' = {
  name: logAnalyticsWorkspace
  location: location
}
Enter fullscreen mode Exit fullscreen mode

dashboard.bicep

  • Specify Log Analytics Workspace in Scope section
  • Specify query in Query section
  • Specify X-Axis, Y-Axis and SplitBy in Dimensions section
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
}
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)