Resource Manager template samples for VM insights

This article includes sample Azure Resource Manager templates to enable VM insights on virtual machines. Each sample includes a template file and a parameters file with sample values to provide to the template.

Note

See Azure Resource Manager samples for Azure Monitor for a list of samples that are available and guidance on deploying them in your Azure subscription.

Configure workspace

The following sample enables VM insights for a Log Analytics workspace.

Template file

main.bicep

@description('Resource ID of the workspace.')
param workspaceResourceId string

@description('Location of the workspace.')
param workspaceLocation string

module VMISolutionDeployment './nested_VMISolutionDeployment.bicep' = {
  name: 'VMISolutionDeployment'
  scope: resourceGroup(split(workspaceResourceId, '/')[2], split(workspaceResourceId, '/')[4])
  params: {
    workspaceLocation: workspaceLocation
    workspaceResourceId: workspaceResourceId
  }
}

nested_VMISolutionDeployment.bicep

@description('Location of the workspace.')
param workspaceLocation string

@description('Resource ID of the workspace.')
param workspaceResourceId string

resource solution 'Microsoft.OperationsManagement/solutions@2015-11-01-preview' = {
  location: workspaceLocation
  name: 'VMInsights(${split(workspaceResourceId, '/')[8]})'
  properties: {
    workspaceResourceId: workspaceResourceId
  }
  plan: {
    name: 'VMInsights(${split(workspaceResourceId, '/')[8]})'
    product: 'OMSGallery/VMInsights'
    promotionCode: ''
    publisher: 'Microsoft'
  }
}

Parameter file

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "workspaceResourceId": {
      "value": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/my-resource-group/providers/microsoft.operationalinsights/workspaces/my-workspace"
    },
    "workspaceLocation": {
      "value": "chinaeast2"
    }
  }
}

Onboard an Azure virtual machine

The following sample adds an Azure virtual machine to VM insights.

Template file

@description('VM Resource ID.')
param VmResourceId string

@description('The Virtual Machine Location.')
param VmLocation string

@description('OS Type, Example: Linux / Windows')
param osType string

@description('Workspace Resource ID.')
param WorkspaceResourceId string

var VmName_var = split(VmResourceId, '/')[8]
var DaExtensionName = ((toLower(osType) == 'windows') ? 'DependencyAgentWindows' : 'DependencyAgentLinux')
var DaExtensionType = ((toLower(osType) == 'windows') ? 'DependencyAgentWindows' : 'DependencyAgentLinux')
var DaExtensionVersion = '9.5'
var MmaExtensionName = ((toLower(osType) == 'windows') ? 'MMAExtension' : 'OMSExtension')
var MmaExtensionType = ((toLower(osType) == 'windows') ? 'MicrosoftMonitoringAgent' : 'OmsAgentForLinux')
var MmaExtensionVersion = ((toLower(osType) == 'windows') ? '1.0' : '1.4')


resource vm 'Microsoft.Compute/virtualMachines@2021-11-01' = {
  name: VmName_var
  location: VmLocation
}

resource daExtension 'Microsoft.Compute/virtualMachines/extensions@2021-11-01' = {
  parent: vm
  name: DaExtensionName
  location: VmLocation
  properties: {
    publisher: 'Microsoft.Azure.Monitoring.DependencyAgent'
    type: DaExtensionType
    typeHandlerVersion: DaExtensionVersion
    autoUpgradeMinorVersion: true
  }
}

resource mmaExtension 'Microsoft.Compute/virtualMachines/extensions@2021-11-01' = {
  parent: vm
  name: MmaExtensionName
  location: VmLocation
  properties: {
    publisher: 'Microsoft.EnterpriseCloud.Monitoring'
    type: MmaExtensionType
    typeHandlerVersion: MmaExtensionVersion
    autoUpgradeMinorVersion: true
    settings: {
      workspaceId: reference(WorkspaceResourceId, '2021-12-01-preview').customerId
      azureResourceId: VmResourceId
      stopOnMultipleConnections: true
    }
    protectedSettings: {
      workspaceKey: listKeys(WorkspaceResourceId, '2021-12-01-preview').primarySharedKey
    }
  }
}

Parameter file

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "vmResourceId": {
      "value": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/my-resource-group/providers/Microsoft.Compute/virtualMachines/my-linux-vm"
    },
    "vmLocation": {
      "value": "chinaeast2"
    },
    "osType": {
      "value": "linux"
    },
    "workspaceResourceId": {
      "value": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/my-resource-group/providers/microsoft.operationalinsights/workspaces/my-workspace"
    }
  }
}

Onboard an Azure virtual machine scale set

The following sample adds an Azure virtual machine scale set to VM insights.

Template file

@description('VM Resource ID.')
param VmssResourceId string

@description('The Virtual Machine Location.')
param VmssLocation string

@description('OS Type, Example: Linux / Windows')
param osType string

@description('Workspace Resource ID.')
param WorkspaceResourceId string

var VmssName_var = split(VmssResourceId, '/')[8]
var DaExtensionName = ((toLower(osType) == 'windows') ? 'DependencyAgentWindows' : 'DependencyAgentLinux')
var DaExtensionType = ((toLower(osType) == 'windows') ? 'DependencyAgentWindows' : 'DependencyAgentLinux')
var DaExtensionVersion = '9.5'
var MmaExtensionName = ((toLower(osType) == 'windows') ? 'MMAExtension' : 'OMSExtension')
var MmaExtensionType = ((toLower(osType) == 'windows') ? 'MicrosoftMonitoringAgent' : 'OmsAgentForLinux')
var MmaExtensionVersion = ((toLower(osType) == 'windows') ? '1.0' : '1.4')

resource vmss 'Microsoft.Compute/virtualMachineScaleSets@2021-11-01' = {
  name: VmssName_var
  location: VmssLocation
  properties:{}
}

resource daExtension 'Microsoft.Compute/virtualMachineScaleSets/extensions@2018-10-01' = {
  parent: vmss
  name: DaExtensionName
  properties: {
    publisher: 'Microsoft.Azure.Monitoring.DependencyAgent'
    type: DaExtensionType
    typeHandlerVersion: DaExtensionVersion
    autoUpgradeMinorVersion: true
  }
}

resource mmaExtension 'Microsoft.Compute/virtualMachineScaleSets/extensions@2018-10-01' = {
  parent: vmss
  name: MmaExtensionName
  properties: {
    publisher: 'Microsoft.EnterpriseCloud.Monitoring'
    type: MmaExtensionType
    typeHandlerVersion: MmaExtensionVersion
    autoUpgradeMinorVersion: true
    settings: {
      workspaceId: reference(WorkspaceResourceId, '2021-12-01-preview').customerId
      azureResourceId: VmssResourceId
      stopOnMultipleConnections: true
    }
    protectedSettings: {
      workspaceKey: listKeys(WorkspaceResourceId, '2021-12-01-preview').primarySharedKey
    }
  }
}

Parameter file

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "VmssResourceId": {
      "value": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/my-resource-group/providers/Microsoft.Compute/virtualMachines/my-windows-vmss"
    },
    "VmssLocation": {
      "value": "chinaeast2"
    },
    "OsType": {
      "value": "windows"
    },
    "WorkspaceResourceId": {
      "value": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/my-resource-group/providers/microsoft.operationalinsights/workspaces/my-workspace"
    }
  }
}

Next steps