Create alerting policies with Terraform

This document describes how to use the Google Cloud Terraform Provider to create alerting policies in your Google Cloud project. The Google Cloud Terraform Provider provides the following resources for alerting policies and notification channels:

Terraform is a tool for building, changing, and versioning infrastructure. It uses configuration files to describe the components needed to run a single application or your entire infrastructure. For more information about using Terraform, see the following documents:

Before you begin

To get the permissions that you need to create alerting policies by using Terraform, ask your administrator to grant you the Monitoring Editor (roles/monitoring.editor) IAM role on your project. For more information about granting roles, see Manage access to projects, folders, and organizations.

You might also be able to get the required permissions through custom roles or other predefined roles.

For more information about Cloud Monitoring roles, see Control access with Identity and Access Management.

Create an alerting policy

To create an alerting policy in your Google Cloud project, do the following:

  1. Ensure that Terraform is installed in the Cloud Shell.

  2. In the Cloud Shell, go to the directory that contains your Terraform configuration.

  3. Edit the configuration file and add your alerting policy.

    For example, the following configuration defines an alerting policy that sends a notification when the CPU utilization of a VM instance is greater than 50% for over one minute, with repeated notifications sent every 30 minutes.

    resource "google_monitoring_alert_policy" "alert_policy" {
      display_name = "CPU Utilization > 50%"
      documentation {
        content = "The $${metric.display_name} of the $${resource.type} $${resource.label.instance_id} in $${resource.project} has exceeded 50% for over 1 minute."
      }
      combiner     = "OR"
      conditions {
        display_name = "Condition 1"
        condition_threshold {
            comparison = "COMPARISON_GT"
            duration = "60s"
            filter = "resource.type = \"gce_instance\" AND metric.type = \"compute.googleapis.com/instance/cpu/utilization\""
            threshold_value = "0.5"
            trigger {
              count = "1"
            }
        }
      }
    
      alert_strategy {
        notification_channel_strategy {
            renotify_interval = "1800s"
            notification_channel_names = [google_monitoring_notification_channel.email.name]
        }
      }
    
      notification_channels = [google_monitoring_notification_channel.email.name]
    
      user_labels = {
        severity = "warning"
      }
    }
    

    In the preceding sample, the notification_channels field defines the notification channel for the alerting policy. The notification_channel_names field configures that notification channel to send repeated notifications. Both fields reference a notification channel with a display_name of email, which is defined elsewhere in the Terraform configuration. For more information, see Create and manage notification channels with Terraform.

  4. In the Cloud Shell, enter terraform apply.

To modify your alerting policy, make your edits and then re-apply the Terraform configuration. For more information, see Manage alerting policies with Terraform.

What's next

  • Learn more about Terraform.
  • Try out code samples that use the Google Cloud Terraform Provider with Cloud Monitoring.
  • View the Google Cloud Terraform Provider repository on GitHub.
  • File a GitHub issue to report a bug or ask a question about Terraform.