例如,以下配置定义了一项提醒政策,该政策会在虚拟机实例的 CPU 利用率超过 50% 且持续 1 分钟以上时发送通知,并且每 30 分钟发送一次重复通知。
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"}}
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-28。"],[],[],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."]]