azureterraformterraform-provider-azureazure-log-analyticsazure-vm

Azure Log Analytics : Failed to apply configuration to Microsoft Monitoring Agent in Azure Windows 10 VM


I am getting the following error while installing Microsoft Monitoring Agent in Azure Windows 10 VM using Terraform

Error: Code="VMExtensionProvisioningError"

Message="VM has reported a failure when processing extension 'mmaagent'. Error message: "Failed to apply configuration to Microsoft Monitoring Agent

enter image description here

data "azurerm_log_analytics_workspace" "loganalytics" {
  name                = var.loganalytics_name
  resource_group_name = var.loganalytics_resource_group_name
}

resource "azurerm_network_interface" "virtual_machine_nic" {
  name                = "virtal-machine-nic"
  location            = azurerm_resource_group.ipz12-dat-np-applications-rg.location
  resource_group_name = azurerm_resource_group.ipz12-dat-np-applications-rg.name

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

  depends_on = [
    azurerm_resource_group.ipz12-dat-np-applications-rg
  ]  
}

resource "azurerm_windows_virtual_machine" "virtual_machine" {
  name                = "virtual-machine"
  resource_group_name = azurerm_resource_group.ipz12-dat-np-applications-rg.name
  location            = azurerm_resource_group.ipz12-dat-np-applications-rg.location
  size                = "Standard_B1ms"
  admin_username      = "user"
  admin_password      = "password"
  network_interface_ids = [
    azurerm_network_interface.virtual_machine_nic.id
  ]

  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }

  source_image_reference {
    publisher = "MicrosoftWindowsDesktop"
    offer     = "Windows-10"
    sku       = "21h1-pro"
    version   = "latest"
  }

  depends_on = [
    azurerm_network_interface.virtual_machine_nic
  ]    
}

// Log Analytics Solution
resource "azurerm_log_analytics_solution" "vminsights" {
  resource_group_name   = var.loganalytics_resource_group_name
  location              = var.location
  solution_name         = "VMInsights"

  workspace_resource_id = data.azurerm_log_analytics_workspace.loganalytics.id
  workspace_name        = data.azurerm_log_analytics_workspace.loganalytics.name

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

# This extension is needed for other extensions
resource "azurerm_virtual_machine_extension" "daa-agent" {
  name                       = "DependencyAgentWindows"
  virtual_machine_id         = azurerm_windows_virtual_machine.virtual_machine.id
  publisher                  = "Microsoft.Azure.Monitoring.DependencyAgent"
  type                       = "DependencyAgentWindows"
  type_handler_version       = "9.10"
  automatic_upgrade_enabled  = true
  auto_upgrade_minor_version = true

  settings = <<SETTINGS
    {
        "workspaceId": "${data.azurerm_log_analytics_workspace.loganalytics.id}"
    }
  SETTINGS

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

  depends_on = [
    azurerm_windows_virtual_machine.virtual_machine
  ]
}

# Add logging and monitoring extensions
resource "azurerm_virtual_machine_extension" "monitor-agent" {
  name                  = "AzureMonitorWindowsAgent"
  virtual_machine_id    = azurerm_windows_virtual_machine.virtual_machine.id
  publisher             = "Microsoft.Azure.Monitor"
  type                  = "AzureMonitorWindowsAgent"
  type_handler_version  =  "1.5"
  automatic_upgrade_enabled  = true
  auto_upgrade_minor_version = true

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

  depends_on = [  
    azurerm_virtual_machine_extension.daa-agent
  ]
}

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

  settings = <<SETTINGS
  {
    "workspaceId": "${data.azurerm_log_analytics_workspace.loganalytics.id}"
  }
SETTINGS

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

  depends_on = [  
    azurerm_windows_virtual_machine.virtual_machine
  ]
}

Solution

  • I was able to install the extension using the below terraform code

    resource "azurerm_virtual_machine_extension" "mmaagent" {
      name                 = "mmaagent"
      virtual_machine_id   = azurerm_windows_virtual_machine.virtual_machine.id
      publisher            = "Microsoft.EnterpriseCloud.Monitoring"
      type                 = "MicrosoftMonitoringAgent"
      type_handler_version = "1.0"
      auto_upgrade_minor_version = "true"
    
      settings = <<SETTINGS
      {
        "workspaceId": "${data.azurerm_log_analytics_workspace.loganalytics.workspace_id}"
      }
    SETTINGS
    
      protected_settings = <<PROTECTED_SETTINGS
      {
        "workspaceKey": "${data.azurerm_log_analytics_workspace.loganalytics.primary_shared_key}"
      }
    PROTECTED_SETTINGS
    
      depends_on = [  
        azurerm_windows_virtual_machine.virtual_machine
      ]
    }