'Terraform enable VM Insights

Did someone managed to enable via terraforms Insights for a VM?

i'm able to create a VM, enable logging, but not enable insights..

i've seen this question: but don't find a clear answer.. How to enable azure vm application insights monitoring agent using terraform

Here is my full terraform script that i'm using for tests, i'm running it directly on the cloud shell from azure.

    # Configure the Azure provider
provider "azurerm" {
    # The "feature" block is required for AzureRM provider 2.x.
    features {}
}
variable "prefix" {
  default = "tfvmex"
}

resource "azurerm_resource_group" "main" {
  name     = "${var.prefix}-resources"
  location = "West Europe"
}

resource "azurerm_virtual_network" "main" {
  name                = "${var.prefix}-network"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.main.location
  resource_group_name = azurerm_resource_group.main.name
}

resource "azurerm_subnet" "internal" {
  name                 = "internal"
  resource_group_name  = azurerm_resource_group.main.name
  virtual_network_name = azurerm_virtual_network.main.name
  address_prefixes     = ["10.0.2.0/24"]
}

resource "azurerm_network_interface" "main" {
  name                = "${var.prefix}-nic"
  location            = azurerm_resource_group.main.location
  resource_group_name = azurerm_resource_group.main.name

  ip_configuration {
    name                          = "testconfiguration1"
    subnet_id                     = azurerm_subnet.internal.id
    private_ip_address_allocation = "Dynamic"
  }
}

resource "azurerm_virtual_machine" "main" {
  name                  = "${var.prefix}-vm"
  location              = azurerm_resource_group.main.location
  resource_group_name   = azurerm_resource_group.main.name
  network_interface_ids = [azurerm_network_interface.main.id]
  vm_size               = "Standard_DS1_v2"

  # Uncomment this line to delete the OS disk automatically when deleting the VM
  # delete_os_disk_on_termination = true

  # Uncomment this line to delete the data disks automatically when deleting the VM
  # delete_data_disks_on_termination = true

  storage_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "16.04-LTS"
    version   = "latest"
  }
  storage_os_disk {
    name              = "myosdisk1"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Standard_LRS"
  }
  os_profile {
    computer_name  = "hostname"
    admin_username = "testadmin"
    admin_password = "Password1234!"
  }
  os_profile_linux_config {
    disable_password_authentication = false
  }
  tags = {
    environment = "staging"
  }
}

resource "azurerm_storage_account" "main" {
  name                     = "omstesttest22"
  resource_group_name      = azurerm_resource_group.main.name
  location                 = "westus"
  account_tier             = "Standard"
  account_replication_type = "GRS"

  tags = {
    environment = "staging"
  }
}

resource "azurerm_log_analytics_workspace" "law02" {
  name                = "${var.prefix}-logAnalytics"
 location            = azurerm_resource_group.main.location
  resource_group_name = azurerm_resource_group.main.name
 sku                 = "PerGB2018"
  retention_in_days   = 30
}



resource "azurerm_log_analytics_solution" "example" {
  solution_name         = "ContainerInsights"
  location              = azurerm_resource_group.main.location
  resource_group_name   = azurerm_resource_group.main.name
  workspace_resource_id = azurerm_log_analytics_workspace.law02.id
  workspace_name        = azurerm_log_analytics_workspace.law02.name

  plan {
    publisher = "Microsoft"
    product   = "OMSGallery/ContainerInsights"
  }
}

#===================================================================
# Set Monitoring and Log Analytics Workspace
#===================================================================
resource "azurerm_virtual_machine_extension" "oms_mma02" {
  name                       = "test-OMSExtension"
virtual_machine_id         =  azurerm_virtual_machine.main.id
  publisher                  = "Microsoft.EnterpriseCloud.Monitoring"
  type                       = "OmsAgentForLinux"
  type_handler_version       = "1.12"
  auto_upgrade_minor_version = true

  settings = <<SETTINGS
    {
      "workspaceId" : "${azurerm_log_analytics_workspace.law02.workspace_id}"
    }
  SETTINGS

  protected_settings = <<PROTECTED_SETTINGS
    {
      "workspaceKey" : "${azurerm_log_analytics_workspace.law02.primary_shared_key}"
    }
  PROTECTED_SETTINGS
}

Hope it was clear. Thanks!



Solution 1:[1]

From the document, VM insights require the following two agents to be installed on each virtual machine to be monitored.

  • Log Analytics agent. Collects events and performance data from the virtual machine or virtual machine scale set and delivers it to the Log Analytics workspace. Deployment methods for the Log Analytics agent on Azure resources use the VM extension for Windows and Linux.
  • Dependency agent. Collects discovered data about processes running on the virtual machine and external process dependencies, which are used by the Map feature in VM insights. The Dependency agent relies on the Log Analytics agent to deliver its data to Azure Monitor. Deployment methods for the Dependency agent on Azure resources use the VM extension for Windows and Linux.

After my validation, you can add the DependencyAgent extension to your existing code.

resource "azurerm_virtual_machine_extension" "da" {
  name                       = "DAExtension"
  virtual_machine_id         =  azurerm_virtual_machine.main.id
  publisher                  = "Microsoft.Azure.Monitoring.DependencyAgent"
  type                       = "DependencyAgentLinux"
  type_handler_version       = "9.5"
  auto_upgrade_minor_version = true

}

enter image description here

For more information, read Configure Log Analytics workspace for VM insights and Enable VM insights guest health (preview)

Solution 2:[2]

To deploy it using Terraform:

Deploy a log analytics workspace and a VMInsights solution associated with the workspace.

resource "azurerm_log_analytics_workspace" "law" {
  name                      = "LogAnalyticsWorkspace"
  location                  = "Your location"
  resource_group_name       = "Your resource group"
  sku                       = "PerGB2018"
  retention_in_days         = "your retention in days"
  internet_ingestion_enabled= true
  internet_query_enabled    = false
  tags                      = "Your tags"
}

resource "azurerm_log_analytics_solution" "vminsights" {
  solution_name         = "VMInsights"
  location              = "Your location"
  resource_group_name   = "Your resource group"
  workspace_resource_id = azurerm_log_analytics_workspace.law.id
  workspace_name        = azurerm_log_analytics_workspace.law.name
  tags                  = "Your tags"

  plan {
    publisher = "Microsoft"
    product   = "OMSGallery/VMInsights"
  }
}

Deploy VM with as usual with OMSAgent and DependencyAgentWindows extensions:

resource "azurerm_windows_virtual_machine" "vm" {
   ......
   ......
}

OMS for Windows: https://docs.microsoft.com/en-us/azure/virtual-machines/extensions/oms-windows

resource "azurerm_virtual_machine_extension" "omsext" {
  name                  = "OMSExtension" 
  virtual_machine_id    = azurerm_windows_virtual_machine.vm.id
  publisher             = "Microsoft.EnterpriseCloud.Monitoring"
  type                  = "MicrosoftMonitoringAgent"
  type_handler_version  = "1.0"
  auto_upgrade_minor_version = true

  settings = <<SETTINGS
    {
        "workspaceId": "${azurerm_log_analytics_workspace.law.id}"
    }
  SETTINGS
  protected_settings = <<PROTECTED_SETTINGS
    {
      "workspaceKey": "${azurerm_log_analytics_workspace.law.primary_shared_key}"
    }
  PROTECTED_SETTINGS  

  tags                       = "Your tags"
}

DA Agent for Windows: https://docs.microsoft.com/en-us/azure/virtual-machines/extensions/agent-dependency-windows

resource "azurerm_virtual_machine_extension" "DAAgent" {
  name                       = "DAAgentExtension"
  virtual_machine_id         = azurerm_windows_virtual_machine.vm.id
  publisher                  = "Microsoft.Azure.Monitoring.DependencyAgent"
  type                       = "DependencyAgentWindows"
  type_handler_version       = "9.10"
  auto_upgrade_minor_version = true
  tags                       = "Your tags"
}

Solution 3:[3]

Solution 4:[4]

please use the product "OMSGallery/VMInsights" (instead of "OMSGallery/ContainerInsights")

resource "azurerm_log_analytics_solution" "..." {
  solution_name         = "..."
  location              = ...
  resource_group_name   = ...
  workspace_resource_id = ...
  workspace_name        = ...

  plan {
    publisher = "Microsoft"
    product   = "OMSGallery/VMInsights"
 }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nancy Xiong
Solution 2
Solution 3 Bright Ran-MSFT
Solution 4