Setting up VM Insights using Terraform

A while back I asked how to configure VM insights using Terraform. While DCR rules are well documented in Terraform docs and also how to associate them with virtual machines. However, VMInsights is a different beast.

VM Insights provides a couple of things

  • Process Map
  • Connection list
  • Performance Monitoring

And also a neat set of logs to see Network Connections between virtual machines as you can see in the screenshot below.

Since not all the parts were documented and since VM Insights requires its own DCR to work functionally, I decided to cheat a bit to get the configuration I needed. The first thing I did was configure a VMInsight DCR manually.

Note: You do not need to do this step, but it is a useful step in how you can get the correct information for other Terraform resources which might not be as well documented.

Once it was provisioned, I went into the DCR and clicked into JSON View, this game the different parameters I needed to configure VM Insights.

So after getting the required configuration, I ended up with the following Terraform code. Just remember to change the text in bold to either use with variables or as part of a module (also the LA workspace ID)

resource "azurerm_monitor_data_collection_rule" "vminsights" {
  name                = var.dcr_name
  resource_group_name = var.rg_name
  location            = var.location
  tags                = var.tags

  destinations {
    log_analytics {
      name                  = "VMInsightsPerf-Logs-Dest"
      workspace_resource_id = la_workspace_id
    }
  }

  data_flow {
    destinations = [
      "VMInsightsPerf-Logs-Dest"
    ]
    streams = [
      "Microsoft-InsightsMetrics"
    ]
  }

  data_flow {
    destinations = [
      "VMInsightsPerf-Logs-Dest"
    ]
    streams = [
      "Microsoft-ServiceMap"
    ]
  }

  data_sources {
    extension {
      extension_name = "DependencyAgent"
      name           = "DependencyAgentDataSource"
      streams        = ["Microsoft-ServiceMap"]
    }

    performance_counter {
      counter_specifiers            = ["\\VmInsights\\DetailedMetrics"]
      name                          = "VMInsightsPerfCounters"
      sampling_frequency_in_seconds = 60
      streams                       = ["Microsoft-InsightsMetrics"]
    }
  }
}

With this you can deploy VMInsights are part of your Terraform configuration.

Leave a Reply

Scroll to Top