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
- Enable the Service Health API for the project that you want to create alerts for.
- Have a notification channel defined in your Terraform configuration. For
more information, see the documentation for the
google_monitoring_notification_channelresource.
Example configuration
The following example Terraform configuration enables Personalized Service Health and alerts for all new incidents and all updates.
provider
"google"
{
projec
t
=
" PROJECT_ID
"
}
resource
"google_project_service"
"project"
{
service
=
"servicehealth.googleapis.com"
}
resource
"google_monitoring_alert_policy"
"alert_policy_all"
{
display_
na
me
=
"All incidents, all updates"
combi
ner
=
"OR"
e
na
bled
=
"true"
co
n
di
t
io
ns
{
display_
na
me
=
"test condition"
co
n
di
t
io
n
_ma
t
ched_log
{
f
il
ter
=
"resource.type = \"servicehealth.googleapis.com/Event\" AND jsonPayload.category = \"INCIDENT\" AND jsonPayload.@type = \"type.googleapis.com/google.cloud.servicehealth.logging.v1.EventLog\""
label_ex
tra
c
t
ors
=
{
s
tate
=
"EXTRACT(jsonPayload.state)"
descrip
t
io
n
=
"EXTRACT(jsonPayload.description)"
impac
te
dProduc
ts
=
"EXTRACT(jsonPayload.impactedProducts)"
s
tart
Time
=
"EXTRACT(jsonPayload.startTime)"
t
i
tle
=
"EXTRACT(jsonPayload.title)"
impac
te
dLoca
t
io
ns
=
"EXTRACT(jsonPayload.impactedLocations)"
}
}
}
docume
ntat
io
n
{
co
ntent
=
"### $${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_
t
ype
=
"text/markdown"
}
aler
t
_s
trate
gy
{
n
o
t
i
f
ica
t
io
n
_ra
te
_limi
t
{
period
=
"300s"
}
}
n
o
t
i
f
ica
t
io
n
_cha
nnels
=
[
"projects/ PROJECT_ID
/notificationChannels/ NOTIFICATION_CHANNEL
"
]
user_labels
=
{
#
Add
a
n
y
ex
tra
labels
t
ha
t
migh
t
be
help
ful
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 updatesoption 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:
-
Documentation for the
google_monitoring_alert_policyresource. -
Example alert conditions that you can add to the
filterfield of your Terraform configuration and define when the alert triggers.

