Add a notification policy

A notification policy controls how alerts are routed to contact points. It matches on alert labels to decide which contact point receives each notification. The policy tree starts with a default route, and you can add nested policies for more specific routing.

To add a notification policy, complete the following steps:

  1. Add the following to your alerts.tf file:

    hcl
    resource "grafana_notification_policy" "main" {
      contact_point   = grafana_contact_point.email_alerts.name
      group_by        = ["alertname"]
      group_wait      = "30s"
      group_interval  = "5m"
      repeat_interval = "4h"
    }

    This configuration:

    • Sets the default contact point to your email contact point
    • Groups alerts by their name so related alerts are batched together
    • Waits 30 seconds before sending the first notification (to batch related alerts)
    • Sends grouped notifications every 5 minutes while the alert is active
    • Repeats notifications every 4 hours

Note: The grafana_notification_policy resource manages the entire notification policy tree for your Grafana instance. If you already have a custom policy tree configured, this resource will replace it. Use disable_provenance = true if you want to allow UI edits alongside Terraform management.

Adding label-based routing

To route specific alerts to different contact points, add nested policies with matchers:

hcl
resource "grafana_notification_policy" "main" {
  contact_point = grafana_contact_point.email_alerts.name
  group_by      = ["alertname"]

  policy {
    matcher {
      label = "severity"
      match = "="
      value = "critical"
    }
    contact_point = grafana_contact_point.pagerduty.name
  }
}

This routes alerts with severity = "critical" to PagerDuty, while all other alerts go to the default email contact point.

In the next milestone, you preview your deployment with terraform plan.


page 7 of 10