Platform · Enterprise

Alert Routing That Fails Safe: PagerDuty, Alertmanager and Grafana with Terraform

Page only on alerts labelled both pagerduty and critical, send the rest to Slack, create per-team PagerDuty services in Terraform and park new services on a placeholder escalation policy so nothing is silently dropped.

  • PagerDuty
  • Terraform
  • Alertmanager
  • Grafana
  • Vault
  • External Secrets
  • Prometheus
  • Slack

// Why this matters

Good alertmanager pagerduty routing is mostly about what you refuse to page for. Teams that page on every warning learn to ignore pages, and the first real incident arrives into an inbox that has been trained to be skimmed. The rule we use is blunt on purpose: a page needs an explicit opt-in label and critical severity; everything else is a chat message.

The second half of the problem is the opposite failure: alerts that fire and go nowhere. Platforms that onboard teams automatically will, sooner or later, create a service before anyone has decided who should be on call. If the default for an unconfigured service is "nothing happens", you have an outage in your alerting that you will discover during an incident.

Failing safe means picking the default that is annoying rather than invisible. A parked incident on a placeholder policy is annoying in the right way: it shows up in the incident list, it can be searched for, and the runbook says exactly how to fix it.

// The problem

In a multi-tenant platform, alert rules are written by many teams with different ideas of severity. Prometheus evaluates them, Alertmanager groups and routes them, and Grafana has its own alerting engine with contact points. Each needs a destination and a credential, and each is a place where "I thought that went to PagerDuty" can be false.

Credentials are the other pain. PagerDuty Events API integration keys are secrets. If a team has to paste one into a values file, it ends up in Git, in a ticket, or in a screenshot. If a platform engineer has to create each service by clicking, services are inconsistent and some are missing.

We need routing that is explicit and testable, services that exist because teams exist, and a delivery path for keys that never touches a human.

// The solution

The Terraform side is deliberately small. For each team in the platform's team list it creates a pagerduty_service and an events API v2 pagerduty_service_integration, with the escalation policy set to a shared placeholder. The integration key is written to the secret store at a per-team path as part of the same apply. This is the same "team list drives everything" idea as From Form to Running Cluster: Self-Service Team Onboarding, and it keeps services and namespaces from drifting.

The placeholder policy is created once and contains a single rule that notifies a small platform-owned schedule. Its only job is to guarantee that a page is never swallowed. Once the owning team provides real on-call details, an operator attaches the real policy in the PagerDuty UI or via a second Terraform variable; that step is documented because we decided not to automate a decision that needs a human.

On the receiving side, Alertmanager routes by matchers. The top-level route has a child that matches pagerduty="true" and severity="critical" with continue: false, sending to the PagerDuty receiver; a second child sends everything for the team to Slack. Each team gets its own receiver pair, selected by a team label that rules must carry. Grafana-managed alerts use contact points and notification policies with the same two-label logic, so behaviour is identical regardless of which engine fired the alert.

Keys reach both systems through External Secrets: the secret store path becomes a Kubernetes Secret, which the Alertmanager config references through routing_key_file, and which Grafana provisioning reads as an environment variable. Compare the config model in Vault as the Control Plane: A Five-Tier Key Model with Locking. Alternatives we rejected were routing purely on severity (too noisy, no opt-in), one global PagerDuty service (no ownership, no escalation per team) and storing keys in Helm values (leaks into Git).

How it flows flow1. Team list → 2. Terraform → 3. Placeholder policy → 4. Secret store → 5. Delivery → 6. Gate: route match → 7. Page → 8. Chat → 9. Manual step01Team list02Terraform03Placeholder policy04Secret store05Delivery06 · GATEGate: route match07Page08Chat09Manual step
How it flows. End-to-end flow. Dashed pink nodes are gates: points where a human or a check must pass before the next stage runs.
  1. Team listthe same source that creates namespaces
  2. Terraformone PagerDuty service plus integration per team
  3. Placeholder policynew services land on a parking escalation policy
  4. Secret storeintegration keys written to a per-team path
  5. DeliveryExternal Secrets mounts keys for Alertmanager and Grafana
  6. Gate: route match gatelabels pagerduty=true AND severity=critical
  7. Pagematched alerts go to the team service
  8. Chateverything else goes to the team Slack channel
  9. Manual stepattach the real escalation policy per runbook
Routing decision flow1. Alert fires → 2. Has team label? → 3. Gate: pagerduty=true and severity=critical → 4. Page → 5. Otherwise → 6. Escalation policy01Alert fires02Has team label?03 · GATEGate: pagerduty=trueand severity=critical04Page05Otherwise06Escalation policy
Routing decision. The same two-label rule is applied by Alertmanager and by Grafana.
  1. Alert firesPrometheus rule or Grafana rule
  2. Has team label?no, goes to a platform catch-all channel
  3. Gate: pagerduty=true and severity=critical gateboth required to page
  4. Pageteam PagerDuty service via events API
  5. Otherwiseteam Slack channel
  6. Escalation policyplaceholder until attached to the real one
Source of truthTeam listLabel contractProvisioningTerraformPagerDuty providerVault writeDeliveryExternal SecretsK8s SecretConsumersAlertmanagerGrafanaSlack
Where things live. Terraform owns the service; the secret store owns the key.

// Before you start

  • A PagerDuty account with API access and permission to create services and integrations.
  • Terraform 1.5+ and the PagerDuty/pagerduty provider.
  • Prometheus with Alertmanager (or kube-prometheus-stack) and a Grafana instance with unified alerting.
  • A secret store (Vault shown) and External Secrets Operator in the cluster.
  • A Slack incoming webhook or app token stored as a secret, per team or shared.
  • An agreed label contract for alerts: team, severity, pagerduty.

// Step-by-step implementation

  1. 01 Define the label contract

    Decide the three labels before writing any config, and write them down where rule authors will see them. team selects the destination, severity expresses urgency and pagerduty is the explicit opt-in. The reason for the opt-in is human: critical does not mean "wake someone" to everyone.

    Enforce the contract with a CI check on rule files so a rule without team fails the build instead of landing in the catch-all.

  2. 02 Create the placeholder policy and per-team services in Terraform

    The placeholder escalation policy is created once and referenced by every new service. Give it a real, monitored target, such as a platform on-call schedule, so a parked incident does page someone eventually; the point is that it is never empty.

    Services take their names from the team list. Use for_each on a set so adding a team is additive and removing one is a visible plan.

  3. 03 Store each integration key in the secret store

    Write the key to a per-team path in the same apply. That guarantees a service never exists without its key being available, and removes any human from the transfer. Use the provider's write-only or ephemeral support if your version has it; otherwise accept that the key is in state and protect state accordingly.

    Keep the path convention predictable so the delivery layer can template it from the team name.

  4. 04 Deliver keys with External Secrets

    An ExternalSecret per team pulls the key into a Kubernetes Secret in the monitoring namespace. Alertmanager can read it from a mounted file, which avoids putting it in the rendered config.

    Generate these resources from the same team list so there is no per-team hand-editing.

  5. 05 Configure Alertmanager routes and receivers

    The route tree has the PagerDuty child first with both matchers, then a Slack child per team. Because the PagerDuty child does not set continue, a paged alert is not also sent to chat unless you add a second route; many teams prefer both, in which case set continue: true on the page route.

    Mount each team Secret and reference its file with routing_key_file, which Alertmanager supports for PagerDuty receivers.

  6. 06 Mirror the logic in Grafana alerting

    Grafana-managed rules do not pass through Alertmanager unless you configure it so, which means they need their own contact points and policy. Provision them as files so the behaviour is reviewed like code.

    Use the same label matchers so an alert behaves identically whichever engine evaluated it. The integration key comes from an environment variable populated from the same Secret.

  7. 07 Write the runbook for the manual step and test the path

    Document the single manual action: when a team supplies on-call details, attach the real escalation policy to its service. Include how to find parked incidents (filter on the placeholder policy) so a missed step is discoverable.

    Then test with a synthetic alert for each outcome: page, chat, and parked. amtool can show which route an alert will take without firing it.

// How to verify it works

  • amtool config routes test returns the receiver named in each comment above for the three label sets.
  • The synthetic alert creates an incident on the team service and it shows the placeholder escalation policy until the real one is attached.
  • Removing the pagerduty label from the same alert sends a Slack message only; no incident is created.
  • kubectl -n monitoring get externalsecret shows SecretSynced for every team.
  • Searching Git history for the integration key prefix returns nothing.

// Design decisions and trade-offs

  • A page requires two labels, pagerduty and critical; anything else goes to chat. Paging is opt-in twice.
  • PagerDuty services are created from the same team list that creates namespaces, so a team cannot exist without a service.
  • New services attach to a placeholder escalation policy that parks incidents instead of dropping them.
  • Integration keys travel through the secret store into Alertmanager and Grafana; they never appear in Git or Terraform state outputs.
  • The one manual step (attaching a real escalation policy) is a runbook item, not a hidden assumption.

// Pitfalls and lessons learned

  • Without ignore_changes on the escalation policy, the next apply silently reverts a real policy back to the placeholder.
  • Alertmanager reads routing_key_file at config load; rotating the Secret requires a reload (or use a config reloader sidecar) before the new key is used.
  • Grafana and Prometheus alerts can fire for the same condition; decide which engine owns each alert or you will page twice.
  • Label values are strings: pagerduty: true without quotes in rule YAML is a boolean and will not match "true".
  • An alert missing the team label falls through to the catch-all. Lint rules in CI, and alert on alerts reaching the catch-all.
  • PagerDuty events API v2 routing keys are per integration; copying a key between services makes incidents appear in the wrong place.
  • If the placeholder schedule has no one on it, the "safe" default silently drops pages again. Monitor that it is staffed.

// Frequently asked questions

How do I route only critical alerts to PagerDuty in Alertmanager?

Add a child route with matchers for severity="critical" and an explicit opt-in label, pointing at a pagerduty_configs receiver; put it before the catch-all route.

Can Terraform manage PagerDuty services and escalation policies?

Yes, the PagerDuty provider has resources for services, integrations, schedules and escalation policies; use ignore_changes where humans own the value.

How do I keep PagerDuty keys out of Git?

Write them from Terraform to a secret store and deliver with External Secrets, referencing them by file path in Alertmanager.

Does Grafana alerting use the same routing as Alertmanager?

It has its own notification policies and contact points, so replicate the same matcher logic in provisioning.

What should happen to alerts from a new, unconfigured team?

They should land on a staffed placeholder policy so an incident exists and is visible, rather than being dropped.

// The outcome

Every team gets a paging path from day one, noisy alerts stop waking people, and a forgotten escalation policy produces a parked incident someone can find instead of a silent gap. The design trades one deliberate manual step for never losing a real page.

// Where to take it next

// Keep reading