Search code examples
terraformterraform-provider-azureterraform0.12+terraform-template-fileterraform-provider-openstack

Enable Azure Monitor for existing Virtual machines using terraform


I am trying to enable azure monitor feature for existing virtual machines which checks for health and performance of the VM using terraform but I am not able to find the right documentation for the same. Can you help me for the same because I want detailed monitoring for azure?


Solution

  • For enabling VMinsights on Existing Vm’s you need to have the data source for the VM and then deploy Storage account,log analytics workspace ,log analytics solution,log analytics agent for OS of the VM and a depending agent for the OS of the VM.

    provider "azurerm" {
      features {}
    }
    data "azurerm_virtual_machine" "example" {
      name                = "test1"
      resource_group_name = "testgroup"# where your VM resides in your subscription
    }
    
    output "virtual_machine_id" {
      value = data.azurerm_virtual_machine.example.id
    }
    
    resource "azurerm_storage_account" "main" {
      name                     = "vminsightstest1234"
      resource_group_name      = data.azurerm_virtual_machine.example.resource_group_name # where your VM resides in your subscription
      location                 = data.azurerm_virtual_machine.example.location # which region your VM resides 
      account_tier             = "Standard"
      account_replication_type = "GRS"
    }
    
    resource "azurerm_log_analytics_workspace" "LAW" {
      name                = "vminsights-logAnalytics"
     location            = data.azurerm_virtual_machine.example.location #which region your VM resides 
      resource_group_name = data.azurerm_virtual_machine.example.resource_group_name # where your VM resides in your subscription
     sku                 = "PerGB2018"
      retention_in_days   = 30
    }
    
    resource "azurerm_log_analytics_solution" "example" {
      solution_name         = "ContainerInsights"
      location              = data.azurerm_virtual_machine.example.location # which region your VM resides 
      resource_group_name   = data.azurerm_virtual_machine.example.resource_group_name # where your VM resides in your subscription
      workspace_resource_id = azurerm_log_analytics_workspace.LAW.id
      workspace_name        = azurerm_log_analytics_workspace.LAW.name
      plan {
        publisher = "Microsoft"
        product   = "OMSGallery/ContainerInsights"
      }
    }
    # Agent for Linux
    resource "azurerm_virtual_machine_extension" "OMS" {
      name                       = "test-OMSExtension"
      virtual_machine_id         =  data.azurerm_virtual_machine.example.id
      publisher                  = "Microsoft.EnterpriseCloud.Monitoring"
      type                       = "OmsAgentForLinux"
      type_handler_version       = "1.13"
      auto_upgrade_minor_version = true
    
      settings = <<SETTINGS
        {
          "workspaceId" : "${azurerm_log_analytics_workspace.LAW.workspace_id}"
        }
      SETTINGS
    
      protected_settings = <<PROTECTED_SETTINGS
        {
          "workspaceKey" : "${azurerm_log_analytics_workspace.LAW.primary_shared_key}"
        }
      PROTECTED_SETTINGS
    }
    
    # Dependency Agent for Linux
    resource "azurerm_virtual_machine_extension" "da" {
      name                       = "DAExtension"
      virtual_machine_id         =  data.azurerm_virtual_machine.example.id
      publisher                  = "Microsoft.Azure.Monitoring.DependencyAgent"
      type                       = "DependencyAgentLinux"
      type_handler_version       = "9.5"
      auto_upgrade_minor_version = true
    
    }
    #Agent for Windows
    resource "azurerm_virtual_machine_extension" "MMA" {
      name                       = "test-MMAextension"
      virtual_machine_id         =  data.azurerm_virtual_machine.example.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.workspace_id}"
        }
      SETTINGS
    
      protected_settings = <<PROTECTED_SETTINGS
        {
          "workspaceKey" : "${azurerm_log_analytics_workspace.LAW.primary_shared_key}"
        }
      PROTECTED_SETTINGS
    }
    
    # Dependency Agent for Windows
    resource "azurerm_virtual_machine_extension" "da" {
      name                       = "DAExtension"
      virtual_machine_id         =  data.azurerm_virtual_machine.example.id
      publisher                  = "Microsoft.Azure.Monitoring.DependencyAgent"
      type                       = "DependencyAgentWindows"
      type_handler_version       = "9.5"
      auto_upgrade_minor_version = true
    
    }
    

    Note: Add Monitoring Agent and dependency agent as per your OS requirement.

    The above code I have tested in my lab for a Windows VM that I had created.

    enter image description here

    enter image description here

    enter image description here

    enter image description here