How to set the Severity as Error in GCP alert page using Terraform GCP

146 Views Asked by At

I am setting up for IaC in Terraform for Google cloud function when error occurs.

This is my code in Terraform:

resource "google_monitoring_alert_policy" "my_cf_alert_policy" {
    display_name = "${var.ENVIRONMENT}_my_cf_error_alert"
    notification_channels = [
        google_monitoring_notification_channel.email_channel.name
        ]
    combiner     = "OR"
    conditions {
        display_name = "send_alert_when_severity=ERROR"
        condition_matched_log {
            filter = "resource.type=\"cloud_function\" resource.labels.function_name=\"${google_cloudfunctions_function.my_cf.name}\" severity=\"ERROR\""
        }
    }

    alert_strategy {
        auto_close = "604800s"
        notification_rate_limit {
            period = "300s"
        }
    }
}

but when I see the same alert policy in GCP console, I could see the Severity is No severity as attached.

enter image description here

I need to have Severity set to Error.

1

There are 1 best solutions below

4
On

You need to add the severity field in the google_monitoring_alert_policy block. This code should work:

resource "google_monitoring_alert_policy" "alert_policy" {
  display_name = "Error alert"
  severity = "ERROR"
  combiner     = "OR"
  conditions {
    display_name = "Condition 1"
    condition_matched_log {
            filter = "resource.type=\"cloud_function\" resource.labels.function_name=\"demo-cf\" severity=\"ERROR\""
    }
  }

  alert_strategy {
    notification_rate_limit {
        period = "300s"
    }
  }

  notification_channels = [data.google_monitoring_notification_channel.email.name]

}

For log-based metrics, you need to specify the notification_rate_limit field.

FYI. notificationChannelStrategy is not allowed for log-based alerts