Stackdriver "Alert & Notification" in GCP using Terraform

Viewed 1053

I am unable to set Alert & Notification in Stackdriver. Able to configure Notification channel and Uptime Check using Terraform. Below are the codes I am using.

resource "google_monitoring_notification_channel" "basic" {
  display_name = "Test Notification Channel"
  type         = "email"
  labels = {
    email_address = "fakeid007@gmail.com"
  }
  project = "department1"
}
resource "google_monitoring_uptime_check_config" "http" {
  display_name = "01 - Website uptime check [global]"
  timeout = "10s"
  period = "60s"
  project = "department1"

  http_check {
    path = "/"
    port = "80"
    mask_headers = null
    use_ssl = null
    validate_ssl = null
    request_method = "GET"
  }

  monitored_resource {
    type = "uptime_url"
    labels = {
      host = "35.184.98.16"
    }
  }
}

But I am not able to figure out the below part, which is possible with manual intervention.

enter image description here

In the picture you can see that the notification channel I created with terraform is visible.

Request to help me automate the Alert & Notification also.

1 Answers

I couldn't find a way of doing this through TF, gcloud or the API.

In the end I used the google_monitoring_alert_policy using a Monitoring Query Language query with the condition_monitoring_query_language block, along the lines of:

resource "google_monitoring_alert_policy" "some-uptime-monitor-policy" {
  display_name = "Some Uptime Monitor"
 
  conditions {
    display_name = "Some Site Unavailable"

    condition_monitoring_query_language {
        query = "fetch uptime_url | metric 'monitoring.googleapis.com/uptime_check/check_passed' | filter (resource.host == 'somesite.com') | group_by 1m, [value_check_passed_count_true: count_true(value.check_passed)] | every 5m | condition value_check_passed_count_true < 1 '1'"
        duration = "60s" 
        }
    }
  }

I had several monitors which is why I went the condition_monitoring_query_language way, if you have one monitor you could use condition_threshold in its place. I found I couldn't get what I wanted with condition_threshold due the multi site requirement

Related