You can try this sample at https://diverse.services/building-azure-monitor-for-application-monitoring-with-terraform/
Unable to Deploy Guest Metrics using Terraform in Azure
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"
}
}
2 answers
Sort by: Oldest
-
-
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 :)