【问题标题】:How to create alerts for Azure resources using Terraform如何使用 Terraform 为 Azure 资源创建警报
【发布时间】:2021-03-31 21:14:32
【问题描述】:

我已经为应用服务、AppService 计划、存储帐户和逻辑应用等 Azure 资源准备了 Terraform 脚本......

我已经成功部署了上述 Terraform 脚本。但我想使用 Terraform 为上述资源配置警报。

有没有什么方法可以在没有 ARM 模板部署的情况下使用 Terraform 创建警报?

【问题讨论】:

    标签: azure terraform azure-alerts


    【解决方案1】:

    当然有。这是来自 Application Insights 的自定义日志搜索示例。但是您可以轻松地为 Azure Monitor 等其他来源修改它

    resource "azurerm_application_insights" "example" {
      name                = "${var.prefix}-appinsights"
      location            = azurerm_resource_group.example.location
      resource_group_name = azurerm_resource_group.example.name
      application_type    = "web"
      retention_in_days   = 30
    }
    
    resource "azurerm_monitor_action_group" "example" {
      name                = "CriticalAlertsAction"
      resource_group_name = azurerm_resource_group.example.name
      short_name          = "p0action"
    
      email_receiver {
        name                    = "sendtoadmin"
        email_address           = "admin@example.com"
        use_common_alert_schema = true
      }
    }
    
    resource "azurerm_monitor_scheduled_query_rules_alert" "example-alert1" {
      name                = "${var.prefix}-alertrule1"
      location            = azurerm_resource_group.example.location
      resource_group_name = azurerm_resource_group.example.name
    
      action {
        action_group = [
          azurerm_monitor_action_group.example.id
        ]
      }
      data_source_id = azurerm_application_insights.example.id
      description    = "Exception threshold reached"
      enabled        = true
      # Count all requests with server error result code grouped into 5-minute bins
      query       = <<-QUERY
      requests
        | where cloud_RoleName == "frontend" and name !contains "Health" and resultCode startswith "5" 
      QUERY
      severity    = 1
      frequency   = 5
      time_window = 5
      trigger {
        operator  = "GreaterThan"
        threshold = 10
      }
    }
    

    【讨论】:

    • 我没有这样的例子。因此,我们不能为您完成所有工作;)这个示例应该足以让您自己解决其余的问题。也许首先通过门户手动创建此类警报,然后将所需的项目(例如从那里的查询)提取到 Terraform
    猜你喜欢
    • 2020-11-18
    • 2020-11-20
    • 2020-04-22
    • 2022-07-24
    • 2018-11-24
    • 2021-03-02
    • 2021-01-21
    • 2022-01-03
    • 2019-05-19
    相关资源
    最近更新 更多