0

I am working on to create the alerts in azure with Terraform scripts. I am trying to create different metric type alerts for different resources. (For example: functionapp01, functionapp02, logicapp01 and logicapp02 etc.)

This is the script:

terraform {
  required_version = ">=0.12"
}

resource "azurerm_monitor_metric_alert" "metric_alert" {
  name                = var.metric_alert_name
  resource_group_name = var.rg_name
  scopes              = [var.resource_id_01,var.resource_id_02]
  description         = var.metric_alert_description
  tags                = var.tags
  frequency           = var.frequency
  severity            = var.severity
  window_size         = var.window_size
  enabled             = var.is_enabled
  
  criteria {
    metric_namespace = var.metric_namespace
    metric_name      = var.metric_name
    aggregation      = var.aggregation
    operator         = var.operator
    threshold        = var.threshold
  }

  action {
    action_group_id = var.action_group_id
  }

}

Whenever I run the above script, then I am getting the below error:

Service returned an error. Status=400 Code="BadRequest" Message="Alerts are currently not supported with multi resource level for microsoft.web/sites

Reference Links:

azurerm_monitor_metric_alert

Metrics and Dimensions Supported

So, can anyone suggest me on this issue?

Pradeep
  • 5,101
  • 14
  • 68
  • 140

1 Answers1

2

As the error show, the microsoft.web/sites resource type does not support Multi-resource alerts, refer to https://learn.microsoft.com/en-us/azure/azure-monitor/platform/alerts-metric-near-real-time#metrics-and-dimensions-supported enter image description here enter image description here

In this case, you have to create each monitor_metric_alert on each resource level.

For example, if you have created two functions, functionapp01, functionapp02. You can do it like this.

variable "function_apps" {
  default = ["functionapp01","functionapp02"]
}


data "azurerm_function_app" "example" {
  for_each = toset(var.function_apps)
  name                = each.value
  resource_group_name = "funtions_rg"
}


resource "azurerm_monitor_metric_alert" "metric_alert" {
  for_each = toset(var.function_apps)
  name                = "${each.value}-example-metricalert"
  resource_group_name = var.rg_name
  scopes              = [data.azurerm_function_app.example[each.value].id]
  description         = var.metric_alert_description
  tags                = var.tags
  frequency           = var.frequency
  severity            = var.severity
  window_size         = var.window_size
  enabled             = var.is_enabled
  
  criteria {
    metric_namespace = var.metric_namespace
    metric_name      = var.metric_name
    aggregation      = var.aggregation
    operator         = var.operator
    threshold        = var.threshold
  }

  action {
    action_group_id = var.action_group_id
  }

}

Update

If you have function app resources is being created with Terraform, you can use them like this:

            variable "function_apps" {
              default = ["functionapp01","functionapp02"]
            }
            
         resource "azurerm_function_app" "example" {
            for_each = toset(var.function_apps)
            name                      = "${each.value}-example-funapp"
            location                  = azurerm_resource_group.example.location
            resource_group_name       = azurerm_resource_group.example.name
            app_service_plan_id       = azurerm_app_service_plan.example.id
          
        
            storage_account_name       = azurerm_storage_account.example.name
            storage_account_access_key = azurerm_storage_account.example.primary_access_key
          
            app_settings = { }
          
            version = "~3"
           
          }
        
        
        
        resource "azurerm_monitor_metric_alert" "test" {
          for_each = toset(var.function_apps)
          name                = "${each.value}-example-metricalert"
          resource_group_name = azurerm_resource_group.example.name
          scopes              = [azurerm_function_app.example[each.value].id]
          description         = var.metric_alert_description
          severity            = var.severity
          window_size         = var.window_size
          enabled             = var.is_enabled
      
            criteria {
        metric_namespace = var.metric_namespace
        metric_name      = var.metric_name
        aggregation      = var.aggregation
        operator         = var.operator
        threshold        = var.threshold
      }
    
        
  action {
    action_group_id = var.action_group_id
  }
}
Nancy
  • 26,865
  • 3
  • 18
  • 34
  • Thanks @Nancy, The above answer contains the code snippet for importing the functions and iterating the item in `azurerm_monitor_metric_alert`. But in my case I have created the function apps, app service plans and storage resources etc.. using TF scripts. So, can you please update your answer according to it. – Pradeep Dec 28 '20 at 04:54
  • is there any way to provide different resource ids to the `scopes` property? because I have created separate module for `azurerm_monitor_metric_alert`. I want to pass different resource ids, metric names and alert names etc.. – Pradeep Dec 28 '20 at 05:43
  • The above code create two metric alerts for two function app with dynamic resources Id. `scopes = [azurerm_function_app.example[each.value].id]`. Don't it answer" How to create same alert for multiple resources of same resource type"? And the criteria is an example. Now I change it like your code. – Nancy Dec 28 '20 at 06:02
  • The above answer contains the code snippet for creating one alert of each function app. But I want to create multiple alerts by passing different metrics. If you have any reference documents pls share links, then I will look into it. – Pradeep Dec 28 '20 at 06:13
  • [Here](http://5.9.10.113/65291589/change-count-to-for-each-terraform) is an example of using `dynamic_criteria`. It looks like what you are looking for. – Nancy Dec 28 '20 at 06:23
  • No, I'm not looking for `dynamic_criteria`. – Pradeep Dec 28 '20 at 06:29
  • 1
    "create multiple alerts by passing different metrics" Does this mean? Could you give it an example. Or you can accept this and post it in another thread. – Nancy Dec 28 '20 at 06:30
  • Yes, I'm looking that one. do you have any sample code snippet? – Pradeep Dec 28 '20 at 06:36
  • In fact, you also could refer to my current code, just add a `variable "logic_apps"` then create each logic app and each metric alerts for each logic app with `scopes = [azurerm_logic_app_workflow.example[each.value].id]` and the respect criteria in another metric_alert. – Nancy Dec 28 '20 at 07:29