Stay organized with collections
Save and categorize content based on your preferences.
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:
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.
In the Cloud Shell,
go to the directory that contains your Terraform configuration.
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.
To modify your alerting policy, make your edits and then re-apply the
Terraform configuration. For more information, see
Manage alerting policies with Terraform.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-29 UTC."],[],[],null,["# Create alerting policies with Terraform\n\nThis document describes how to use the Google Cloud Terraform Provider\nto create\nalerting policies in your Google Cloud project. The Google Cloud Terraform Provider\nprovides the following resources for alerting policies\nand notification channels:\n\n- [google_monitoring_alert_policy](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/monitoring_alert_policy)\n- [google_monitoring_notification_channel](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/monitoring_notification_channel)\n\n[Terraform](https://www.terraform.io/) is a tool for building,\nchanging, and versioning infrastructure. It uses configuration files to describe\nthe components needed to run a single application or your entire infrastructure.\nFor more information about using Terraform, see the following\ndocuments:\n\n- [Terraform on Google Cloud documentation](/docs/terraform#docs)\n- [Google Cloud Terraform Provider](https://registry.terraform.io/providers/hashicorp/google/latest/docs)\n- [Get Started - Google Cloud](https://developer.hashicorp.com/terraform/tutorials/gcp-get-started)\n\nBefore you begin\n----------------\n\n\nTo get the permissions that\nyou need to create alerting policies by using Terraform,\n\nask your administrator to grant you the\n\n\n[Monitoring Editor](/iam/docs/roles-permissions/monitoring#monitoring.editor) (`roles/monitoring.editor`)\nIAM role on your project.\n\n\nFor more information about granting roles, see [Manage access to projects, folders, and organizations](/iam/docs/granting-changing-revoking-access).\n\n\nYou might also be able to get\nthe required permissions through [custom\nroles](/iam/docs/creating-custom-roles) or other [predefined\nroles](/iam/docs/roles-overview#predefined).\n\nFor more information about Cloud Monitoring roles,\nsee [Control access with Identity and Access Management](/monitoring/access-control).\n\nCreate an alerting policy\n-------------------------\n\nTo create an alerting policy in your Google Cloud project, do the following:\n\n1. [Install and configure Terraform](/docs/terraform/install-configure-terraform).\n\n2. In the Cloud Shell,\n go to the directory that contains your Terraform configuration.\n\n3. Edit the configuration file and add your alerting policy.\n\n For example, the following configuration\n defines an alerting policy that sends a notification when the CPU utilization\n of a VM instance is greater than 50% for over one minute, with\n [repeated notifications](/monitoring/alerts/policies-in-api#send-repeated-notifications) sent every 30 minutes. \n\n resource \"google_monitoring_alert_policy\" \"alert_policy\" {\n display_name = \"CPU Utilization \u003e 50%\"\n documentation {\n content = \"The $${metric.display_name} of the $${resource.type} $${resource.label.instance_id} in $${resource.project} has exceeded 50% for over 1 minute.\"\n }\n combiner = \"OR\"\n conditions {\n display_name = \"Condition 1\"\n condition_threshold {\n comparison = \"COMPARISON_GT\"\n duration = \"60s\"\n filter = \"resource.type = \\\"gce_instance\\\" AND metric.type = \\\"compute.googleapis.com/instance/cpu/utilization\\\"\"\n threshold_value = \"50\"\n trigger {\n count = \"1\"\n }\n }\n }\n\n alert_strategy {\n notification_channel_strategy {\n renotify_interval = \"1800s\"\n notification_channel_names = [google_monitoring_notification_channel.email.name]\n }\n }\n\n notification_channels = [google_monitoring_notification_channel.email.name]\n\n user_labels = {\n severity = \"warning\"\n }\n }\n\n In the preceding sample, the `notification_channels` field defines the\n notification channel for the alerting policy. The\n `notification_channel_names` field configures that notification channel\n to send repeated notifications. Both fields reference a notification\n channel with a `display_name` of `email`, which is defined\n elsewhere in the Terraform configuration. For more information, see\n [Create and manage notification channels with Terraform](/monitoring/alerts/notification-terraform).\n\n You can use labels to associate an alerting policy\n with an [App Hub](/app-hub/docs/overview) application. For more information, see\n [How to associate an alerting policy with an App Hub application](/monitoring/alerts/policies-in-api#app-labels).\n4. In the Cloud Shell, enter `terraform apply`.\n\nTo modify your alerting policy, make your edits and then re-apply the\nTerraform configuration. For more information, see\n[Manage alerting policies with Terraform](/monitoring/alerts/manage-alerts-terraform).\n\nWhat's next\n-----------\n\n- Learn more about [Terraform](https://www.terraform.io/).\n- Try out code samples that use the Google Cloud Terraform Provider with Cloud Monitoring.\n- View the Google Cloud Terraform Provider [repository on GitHub](https://github.com/hashicorp/terraform-provider-google).\n- [File a GitHub issue](https://github.com/hashicorp/terraform-provider-google/issues) to report a bug or ask a question about Terraform."]]