Unable to Deploy Guest Metrics using Terraform in Azure

Swapnil Panchal 21 Reputation points
2020-10-03T04:54:24.487+00:00

I am trying to deploy below guest metric alert for all VMs under my subscription using terraform,But its throwing below error.

Error: Error creating or updating metric alert "Memory Usage Alert" (resource group "MyTemp"): insights.MetricAlertsClient#CreateOrUpdate: Failure responding to request: StatusCode=400 -- Original Error: autorest/azure: Service returned an error. Status=400 Code="BadRequest" Message="Multi-resource alert rules are not supported for metrics in the Azure.VM.Windows.GuestMetrics namespace.

Looking at above error when I dig more in it , for guest metric multi resources are not supported.

I want to know is there any way we can pass mutliple Virtual machines and then run in for loop or something like that so that it will deploy the alert one by one on all VMs mentioned in list.?

Below is the Terraform template which is working absolutely fine for Platform Metrics and not for Guest Metrics.

data "azurerm_subscription" "current" {
  subscription_id  = var.subscription_id //it will get the current subscription id
}

resource "azurerm_monitor_metric_alert" "myalert" {
  name                      = "Memory Usage Alert"
  resource_group_name       = "MyTemp" //resource name to which you want to deploy this alert
  scopes                    = [data.azurerm_subscription.current.id]
  description               = "Action will be triggered when Memory Usage is greater than 85."
  target_resource_type      = "Microsoft.Compute/virtualMachines"
  target_resource_location  = "centralindia"
  frequency                 = "PT30M"
  window_size               = "P1D"
  severity                  = "2"
  enabled                   = "true"

  criteria {
    metric_namespace = "Azure.VM.Windows.GuestMetrics"
    metric_name      = "Memory\\% Committed bytes in use"
    aggregation      = "Average"
    operator         = "GreaterThanOrEqual"
    threshold        = 85
  }

  action {
    action_group_id = "/subscriptions/xxxxxxxx/resourceGroups/xxxxx/providers/Microsoft.Insights/actionGroups/xxxxxxx"
  }
}
Azure Monitor
Azure Monitor
An Azure service that is used to collect, analyze, and act on telemetry data from Azure and on-premises environments.
2,804 questions
Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,146 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. CyrAz 5,181 Reputation points
    2020-10-03T09:45:24.583+00:00

    I'm currently working on a similar problem (deploying standardized alerts for every resources of a same type), and so far the solution we've come up with for metrics that were not supported in multi-resources mode is including the alerts resources in the resource's template itself.
    So the alerts are created for each deployed resource at the same time as the resource itself.
    That indeed creates a lot of alert rules, but we didn't come up with a better idea so far...

    If anyone has a better idea, I'll be glad to hear it :)

    1 person found this answer helpful.

  2. Weily 86 Reputation points
    2020-10-03T07:47:03.267+00:00