Create alerting policies with Terraform

The Google Cloud Terraform Provider lets you create alerting policies in your Google Cloud project to monitor your services and resources. You configure these policies and their notification channels by using the google_monitoring_alert_policy and google_monitoring_notification_channel resources.

Terraform uses configuration files to build, change, and version the components needed to run a single application or your entire infrastructure. For more information about using Terraform, see the Terraform on Google Cloud documentation, the Google Cloud Terraform Provider reference, and Get Started - Google Cloud.

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. Install and configure Terraform.

  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 = "50"
            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.

    You can use labels to associate an alerting policy with an App Hub application. For more information, see How to associate an alerting policy with an App Hub application.

  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.