Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use terraform to add a VM to the new Azure Monitoring (without OMS Agent)

When I configure Azure Monitoring using the OMS solution for VMs with this answer Enable Azure Monitor for existing Virtual machines using terraform, I notice that this feature is being deprecated and Azure prefers you move to the new monitoring solution (Not using the log analytics agent).

Azure allows me to configure VM monitoring using this GUI, but I would like to do it using terraform.

enter image description here

Is there a particular setup I have to use in terraform to achieve this? (I am using a Linux VM btw)

like image 774
Ryu S. Avatar asked Oct 17 '25 21:10

Ryu S.


1 Answers

Yes, that is correct. The omsagent has been marked as legacy and Azure now has a new monitoring agent called "Azure Monitor agent" . The solution given below is for Linux, Please check the Official Terraform docs for Windows machines.

We need three things to do the equal UI counterpart in Terraform.

  • azurerm_log_analytics_workspace
  • azurerm_monitor_data_collection_rule
  • azurerm_monitor_data_collection_rule_association

Below is the example code:

    data "azurerm_linux_virtual_machine" "vm" {
      name                = var.vm_name
      resource_group_name = var.az_resource_group_name
    }
    
    resource "azurerm_log_analytics_workspace" "workspace" {
      name                = "${var.project}-${var.env}-log-analytics"
      location            = var.az_location
      resource_group_name = var.az_resource_group_name
      sku                 = "PerGB2018"
      retention_in_days   = 30
    }
    
     resource "azurerm_virtual_machine_extension" "AzureMonitorLinuxAgent" {        
      name                       = "AzureMonitorLinuxAgent"
      publisher                  = "Microsoft.Azure.Monitor"
      type                       = "AzureMonitorLinuxAgent"
      type_handler_version       = "1.0"
      auto_upgrade_minor_version = "true"
    
      virtual_machine_id = data.azurerm_linux_virtual_machine.vm.id
    }
    
    
     resource "azurerm_monitor_data_collection_rule" "example" {
      name                = "example-rules"
      resource_group_name = var.az_resource_group_name
      location            = var.az_location
    
      destinations {
        log_analytics {
          workspace_resource_id = azurerm_log_analytics_workspace.workspace.id
          name                  = "test-destination-log"
        }
    
        azure_monitor_metrics {
          name = "test-destination-metrics"
        }
      } 
    
      data_flow {
        streams      = ["Microsoft-InsightsMetrics"]
        destinations = ["test-destination-log"]
      }
    
      data_sources {
    
        performance_counter {
          streams                       = ["Microsoft-InsightsMetrics"]
          sampling_frequency_in_seconds = 60
          counter_specifiers            = ["\\VmInsights\\DetailedMetrics"]
          name                          = "VMInsightsPerfCounters"
        }
    }
     }
    
    # associate to a Data Collection Rule
    resource "azurerm_monitor_data_collection_rule_association" "example1" {
      name                    = "example1-dcra"
      target_resource_id      = data.azurerm_linux_virtual_machine.vm.id
      data_collection_rule_id = azurerm_monitor_data_collection_rule.example.id
      description             = "example"
    }

Reference:

monitor_data_collection_rule

monitor_data_collection_rule_association

like image 104
charles Avatar answered Oct 21 '25 17:10

charles



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!