如何使用 Terraform 创建 Azure 警报

ity*_*970 4 monitoring azure azure-monitoring terraform

我需要使用 terraform 为多个虚拟机创建警报。有人有一个我可以使用的简单例子,例如:

我想监视名为 Rg1 的资源组中名为 vm01、vm01、vm03 的 Azure 虚拟机。例如,我想监控 CPU 使用率和内存。有人可以帮助我提供一个简单的示例,然后我可以在此基础上进行构建吗?

Tho*_*ZRH 10

你可以试试这个:

resource "azurerm_resource_group" "rg" {
  name     = "example-rg"
  location = "northeurope"
}

resource "azurerm_monitor_action_group" "ag" {
  name                = "myactiongroup"
  resource_group_name = azurerm_resource_group.rg.name
  short_name          = "exampleactiongroup"

}

resource "azurerm_monitor_metric_alert" "alert" {
  name                = "example-metricalert"
  resource_group_name = azurerm_resource_group.rg.name
  scopes              = ["/subscriptions/1234xxx"]
  description         = "description"
  target_resource_type = "Microsoft.Compute/virtualMachines"
  
  criteria { 
    metric_namespace = "Microsoft.Compute/virtualMachines"
    metric_name      = "Percentage CPU"
    aggregation      = "Total"
    operator         = "GreaterThan"
    threshold        = 80
  }

  action {
    action_group_id = azurerm_monitor_action_group.ag.id
  }
}
Run Code Online (Sandbox Code Playgroud)

Azure 警报的 Terraform 提供程序文档位于此处