Azure monitor pagerduty terraform

From UVOO Tech Wiki
Revision as of 17:20, 11 March 2025 by Busk (talk | contribs) (Created page with "# Simple example of terraform for azure monitor & PD ``` locals { PAGERDUTY_INTEGRATION_KEY = "" ag_name = "" resource_group_name = "" sql_data...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Simple example of terraform for azure monitor & PD

locals {
  PAGERDUTY_INTEGRATION_KEY = ""
  ag_name                   = ""
  resource_group_name       = ""
  sql_database_name         = ""
  sql_server_name           = ""
  admin_email               = ""
  sev4_alert_name           = "myapp-sql-cpu-gt-80-alert"
  sev2_alert_name           = "myapp-sql-cpu-eq-100-alert"
}

provider "azurerm" {
  features {}
}

data "azurerm_resource_group" "existing_rg" {
  name = local.resource_group_name
}

data "azurerm_mssql_server" "existing_sql_server" {
  name                = local.sql_server_name
  resource_group_name = data.azurerm_resource_group.existing_rg.name
}

data "azurerm_mssql_database" "existing_sql_db" {
  name = local.sql_database_name
  server_id = data.azurerm_mssql_server.existing_sql_server.id
}

resource "azurerm_monitor_action_group" "sql_alert_action_group" {
  name                = local.ag_name
  resource_group_name = data.azurerm_resource_group.existing_rg.name
  short_name          = "pd-alerts"


  webhook_receiver {
    name                    = "pagerduty-webhook"
    service_uri             = "https://events.pagerduty.com/integration/${local.PAGERDUTY_INTEGRATION_KEY}/enqueue"
    use_common_alert_schema = true
  }

  email_receiver {
    name          = "AdminEmail"
    email_address = local.admin_email
  }
}

resource "azurerm_monitor_metric_alert" "sql_cpu_alert_sev4" {
  name                = local.sev4_alert_name
  resource_group_name = data.azurerm_resource_group.existing_rg.name
  scopes              = [data.azurerm_mssql_database.existing_sql_db.id]
  description         = "Triggers when avg CPU consumption exceeds 80% over 15 minutes."
  severity            = 4
  frequency           = "PT5M"
  window_size         = "PT15M"

  criteria {
    metric_namespace = "Microsoft.Sql/servers/databases"
    metric_name      = "cpu_percent"
    aggregation      = "Average"
    operator         = "GreaterThan"
    threshold        = 80
  }

  action {
    action_group_id = azurerm_monitor_action_group.sql_alert_action_group.id
  }
}

resource "azurerm_monitor_metric_alert" "sql_cpu_alert_sev2" {
  name                = local.sev2_alert_name
  resource_group_name = data.azurerm_resource_group.existing_rg.name
  scopes              = [data.azurerm_mssql_database.existing_sql_db.id]
  description         = "Triggers when avg CPU consumption is 100%."
  severity            = 2
  frequency           = "PT1M"
  window_size         = "PT5M"

  criteria {
    metric_namespace = "Microsoft.Sql/servers/databases"
    metric_name      = "cpu_percent"
    aggregation      = "Average"
    operator         = "GreaterThanOrEqual"
    threshold        = 100
  }

  action {
    action_group_id = azurerm_monitor_action_group.sql_alert_action_group.id
  }
}