terraformterraform-provider-gcp

Create GCP alerting policy for uptime check using terraform


Is there any way to create GCP alerting policy for uptime check using terraform and filter value of metric.label.check_id of already deployed resource?

Provided examples in the terraform docs show only alerting policy for metrics not for uptime check for already deployed resource so I’m not sure if that is even possible with the terraform.


Solution

  • I have figure out a solution which works in my case.

    I have create uptime check and uptime check alert by two separate terraform modules.

    Terrraform uptime check module looks like:

    
    resource "google_monitoring_uptime_check_config" "uptime-check" {
      project = var.project_id
      display_name = var.display_name
      timeout = "10s"
      period = "60s"
    
      http_check {
        path = var.path
        port = var.port
        use_ssl = true
        validate_ssl = true
      }
    
      monitored_resource {
        type = "uptime_url"
        labels = {
          host = var.hostname,
          project_id = var.project_id
        }
      }
    
      content_matchers {
        content = "\"status\":\"UP\""
      }
    }
    

    Then for the outputs.tf for that module I have:

    output "uptime_check_id" {
      value = google_monitoring_uptime_check_config.uptime-check.uptime_check_id
    }
    
    

    Then in the alerts module I have follow terraform docs but modified them to code which looks like:

    module "medallies-common-alerts" {
      source                           = "./modules/alerts"
      project_id                       = var.project_id
      uptime_check_depends_on          = [module.uptime-check]
      check_id                         = module.uptime-check.uptime_check_id
    }
    
    ...
    
    resource "google_monitoring_alert_policy" "alert_policy_uptime_check" {
      project = var.project_id
      enabled = true
      depends_on = [var.uptime_check_depends_on]
    
       ....
    
    
       condition_threshold {
          filter     = format("metric.type=\"monitoring.googleapis.com/uptime_check/check_passed\" AND metric.label.\"check_id\"=\"%s\" AND resource.type=\"uptime_url\"",var.check_id)
          duration   = "300s"
          comparison = "COMPARISON_GT"
          threshold_value = "1"
    
          trigger {
              count = 1
          }
    
    ...
    
    }
    

    Hope it will help someone too.