Configure alerts in Terraform

If you're using Terraform, you can set up alerts for service health events that appear in Personalized Service Health. This document explains how.

Before you begin

You must have a notification channel defined in your Terraform configuration. For more information, see the documentation for the google_monitoring_notification_channel resource.

Example configuration

The following example Terraform configuration enables Personalized Service Health and alerts for all new incidents and all updates.

provider "google" {
  project = "PROJECT_ID"
}

resource "google_project_service" "project" {
  service = "servicehealth.googleapis.com"
}

resource "google_monitoring_alert_policy" "alert_policy_all" {
  display_name = "All incidents, all updates"
  combiner     = "OR"
  enabled      = "true"
  conditions {
    display_name = "test condition"
    condition_matched_log {
      filter     = "resource.type = \"servicehealth.googleapis.com/Event\" AND jsonPayload.category = \"INCIDENT\" AND jsonPayload.@type = \"type.googleapis.com/google.cloud.servicehealth.logging.v1.EventLog\""
      label_extractors = {
          state = "EXTRACT(jsonPayload.state)"
          description = "EXTRACT(jsonPayload.description)"
          impactedProducts = "EXTRACT(jsonPayload.impactedProducts)"
          startTime = "EXTRACT(jsonPayload.startTime)"
          title = "EXTRACT(jsonPayload.title)"
          impactedLocations = "EXTRACT(jsonPayload.impactedLocations)"
      }
    }
  }

  documentation {
    content = "### $${log.extracted_label.title}\nCheck out [Personalized Service Health dashboard](https://console.cloud.google.com/servicehealth/incidentDetails/projects%2F${resource.labels.resource_container}%2Flocations%2F${resource.labels.location}%2Fevents%2F${resource.labels.event_id}?project=${resource.labels.resource_container}) for more details.<br><br>  Description: $${log.extracted_label.description}<br><br>  Impacted products: $${log.extracted_label.impactedProducts}<br><br> Impacted locations: $${log.extracted_label.impactedLocations}<br><br>  Incident start time: $${log.extracted_label.startTime}<br><br>  State: $${log.extracted_label.state}"
    mime_type = "text/markdown"
  }
  alert_strategy {
    notification_rate_limit {
      period = "300s"
    }
  }

  notification_channels =  ["projects/PROJECT_ID/notificationChannels/NOTIFICATION_CHANNEL"]

  user_labels = {  # Add any extra labels that might be helpful
    scope = "all"
  }
}

The service servicehealth.googleapis.com refers to Personalized Service Health, and the filter pulls all events. See the Service Health API reference for the fields you can set.

The previous configuration is equivalent to choosing the All incidents, all updates option in setting up an alert within the Service Health dashboard.

See the following pages for more information on the fields you can set in the alert policy: