Azure VMInsights: Why It’s Useful and How to Deploy It with Bicep

Learn why Azure VMInsights is an essential tool for monitoring Azure VMs and how to deploy it using Bicep.

Azure VMInsights is a must-have tool for anyone looking to optimize visibility into virtual machine performance and health in Azure. Whether managing a single VM or scaling up to hundreds, VMInsights provides the insights needed to monitor, diagnose, and optimize your workloads.

In this blog, we’ll explore why VMInsights is invaluable and guide you through deploying it using Bicep.


Why Use VMInsights?

Managing virtual machines in the cloud comes with unique challenges. Basic metrics like CPU and memory usage aren’t enough to maintain optimal performance at scale. VMInsights addresses this gap with features such as:

1. Comprehensive Monitoring

VMInsights integrates with Azure Monitor to provide unified insights into key metrics like CPU, memory, disk, and network usage.

2. Dependency Mapping

Its dependency map visualizes interactions between VMs and external resources, making it easy to identify bottlenecks and dependencies.

3. Actionable Insights

Pre-built workbooks turn raw data into actionable insights, enabling quick identification of issues like resource contention or underperforming VMs.

4. Scalability

VMInsights scales effortlessly across production, development, and testing environments, ensuring consistent monitoring.


Prerequisites for Deployment

Before deploying VMInsights, ensure the following resources are in place:

1. Log Analytics Workspace (LAW)

A Log Analytics Workspace serves as the central hub for performance metrics and logs.

2. Data Collection Rules (DCRs)

Data Collection Rules define the data to be collected and where it is sent. Separate rules are required for Windows and Linux VMs.


Deploying VMInsights Using Bicep

Infrastructure as Code (IaC) simplifies and standardizes deployments. Below, we’ll outline the key steps to deploy VMInsights using Bicep.

High-Level Deployment Steps

  1. Set up Log Analytics Workspace Create a workspace to store monitoring data.
  2. Create Data Collection Rules Define DCRs for Windows and Linux VMs to specify data collection requirements.
  3. Deploy the VMInsights Solution Link the Log Analytics Workspace to Azure Monitor.
  4. Enable VM Extensions Install necessary extensions on VMs for dependency and performance monitoring.

Ready-to-Use Bicep Template

to save you time, and the hours I had to spend to work out how this all worked with IaC, I’ve created a repository here: log-vminsights

The contents of the repository is:

  • main.bicep - Bicep deployment file
  • Invoke-AzDeployment.ps1 - PowerShell Deployment Wrapper

I would recommend cloning the github repo:

git clone https://github.com/builtwithcaffeine/bwc-bicep-repository.git ; cd bwc-bicep-repository/operational-insights/log-vminsights

Overview of Bicep File

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
targetScope = 'subscription' // Please Update this based on deploymentScope Variable

//
// Imported Parameters

@description('Azure Location')
param location string

@description('Azure Location Short Code')
param locationShortCode string

@description('Environment Type')
param environmentType string

@description('User Deployment Name')
param deployedBy string

@description('Azure Metadata Tags')
param tags object = {
  environmentType: environmentType
  deployedBy: deployedBy
  deployedDate: utcNow('yyyy-MM-dd')
}

@description('The Data Collection Rule Name')
param windowsDataCollectionRuleName string = 'MSVMI-vminsights-windows'

@description('The Data Collection Rule Name')
param linuxDataCollectionRuleName string = 'MSVMI-vminsights-linux'

//
// Bicep Deployment Variables
param projectName string = 'vminsights'
var resourceGroupName = 'rg-${projectName}-${environmentType}-${locationShortCode}'
var logAnalyticsName = 'log-${projectName}-${environmentType}-${locationShortCode}'

//
// Azure Verified Modules - No Hard Coded Values below this line!

module createResourceGroup 'br/public:avm/res/resources/resource-group:0.4.0' = {
  name: 'create-resource-group'
  params: {
    name: resourceGroupName
    location: location
    tags: tags
  }
}

module createLogAnalyticsWorkspace 'br/public:avm/res/operational-insights/workspace:0.9.1' = {
  name: 'create-log-analytics-workspace'
  scope: resourceGroup(resourceGroupName)
  params: {
    name: logAnalyticsName
    location: location
    skuName: 'PerGB2018'
    dataRetention: 90
    tags: tags
  }
  dependsOn: [
    createResourceGroup
  ]
}

module createWindowsDataCollectionRule 'br/public:avm/res/insights/data-collection-rule:0.4.2' = {
  scope: resourceGroup(resourceGroupName)
  name: 'create-windows-data-collection-rule'
  params: {
    name: windowsDataCollectionRuleName
    location: location
    dataCollectionRuleProperties: {
      kind: 'Windows'
      description: 'Data collection rule for VM Insights.'
      dataFlows: [
        {
          streams: [
            'Microsoft-InsightsMetrics'
          ]
          destinations: [
            createLogAnalyticsWorkspace.outputs.name
          ]
        }
        {
          streams: [
            'Microsoft-ServiceMap'
          ]
          destinations: [
            createLogAnalyticsWorkspace.outputs.name
          ]
        }
      ]
      dataSources: {
        performanceCounters: [
          {
            streams: [
              'Microsoft-InsightsMetrics'
            ]
            samplingFrequencyInSeconds: 60
            counterSpecifiers: [
              '\\VmInsights\\DetailedMetrics'
            ]
            name: 'VMInsightsPerfCounters'
          }
        ]
        extensions: [
          {
            streams: [
              'Microsoft-ServiceMap'
            ]
            extensionName: 'DependencyAgent'
            extensionSettings: {}
            name: 'DependencyAgentDataSource'
          }
        ]
      }
      destinations: {
        logAnalytics: [
          {
            workspaceResourceId: createLogAnalyticsWorkspace.outputs.resourceId
            workspaceId: createLogAnalyticsWorkspace.outputs.logAnalyticsWorkspaceId
            name: createLogAnalyticsWorkspace.outputs.name
          }
        ]
      }
    }
  }
  dependsOn: [
      createLogAnalyticsWorkspace
  ]
}

module createLinuxDataCollectionRule 'br/public:avm/res/insights/data-collection-rule:0.4.2' = {
  name: 'create-linux-data-collection-rule'
  scope: resourceGroup(resourceGroupName)
  params: {
    name: linuxDataCollectionRuleName
    location: location
    dataCollectionRuleProperties: {
      kind: 'Linux'
      description: 'Data collection rule for VM Insights.'
      dataFlows: [
        {
          streams: [
            'Microsoft-InsightsMetrics'
          ]
          destinations: [
            createLogAnalyticsWorkspace.outputs.name
          ]
        }
        {
          streams: [
            'Microsoft-ServiceMap'
          ]
          destinations: [
            createLogAnalyticsWorkspace.outputs.name
          ]
        }
      ]
      dataSources: {
        performanceCounters: [
          {
            streams: [
              'Microsoft-InsightsMetrics'
            ]
            samplingFrequencyInSeconds: 60
            counterSpecifiers: [
              '\\VmInsights\\DetailedMetrics'
            ]
            name: 'VMInsightsPerfCounters'
          }
        ]
        extensions: [
          {
            streams: [
              'Microsoft-ServiceMap'
            ]
            extensionName: 'DependencyAgent'
            extensionSettings: {}
            name: 'DependencyAgentDataSource'
          }
        ]
      }
      destinations: {
        logAnalytics: [
          {
            workspaceResourceId: createLogAnalyticsWorkspace.outputs.resourceId
            workspaceId: createLogAnalyticsWorkspace.outputs.logAnalyticsWorkspaceId
            name: createLogAnalyticsWorkspace.outputs.name
          }
        ]
      }
    }
  }
  dependsOn: [
    createLogAnalyticsWorkspace
  ]
}

Once your Bicep template is ready, You can either use the Invoke-AzDeployment.ps1 wrapper deploy it

1
.\Invoke-AzDeployment.ps1 -targetScope 'sub' -subscriptionId '<subscriptionId>' -location 'westeurope' -environmentType 'dev' -deploy

Ensure the Virtual Machine is online first!

To configure on the Virtual Machine, From the settings pain click on Insights then Monitoring Configuration

Wait 30 minutes and you’ll start seeing metrics reporting.

Wrap Up

VMInsights is a powerful solution for optimizing the performance and health of your Azure VMs. By leveraging Bicep for deployment, you gain a scalable, consistent, and efficient setup process. Explore the provided repository to get started quickly and unlock the full potential of VMInsights in your Azure environment.

Share with your network!

Built with Hugo - Theme Stack designed by Jimmy