[[["易于理解","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,["# Manage alerting policies with Terraform\n\nThis document describes how to use the\nGoogle Cloud Terraform Provider to edit and delete\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\nThis feature is supported only for Google Cloud projects.\nFor [App Hub](/app-hub/docs/overview)\nconfigurations, select the App Hub host project or\nthe app-enabled folder's management project.\n\nBefore you begin\n----------------\n\n\nTo get the permissions that\nyou need to modify alerting policies by using Terraform,\n\nask your administrator to grant you the\nfollowing IAM roles on your project:\n\n- [Monitoring Editor](/iam/docs/roles-permissions/monitoring#monitoring.editor) (`roles/monitoring.editor`)\n- Create and use log-based alerting policies: [Logs Configuration Writer](/iam/docs/roles-permissions/logging#logging.configWriter) (`roles/logging.configWriter`)\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| **Note:** The Monitoring Editor role also lets you perform activities like creating dashboards and viewing incidents. If you only need to configure alerting policies with Terraform, then you can use the Monitoring AlertPolicy Editor (`roles/monitoring.alertPolicyEditor`) role instead.\n\nFor more information about Cloud Monitoring roles,\nsee [Control access with Identity and Access Management](/monitoring/access-control).\n\nEdit or delete alerting policies\n--------------------------------\n\nTo edit or delete an alerting policy in your Google Cloud project, do the following:\n\n1. [Install and configure Terraform](/docs/terraform/install-configure-terraform)\n for you project. For [App Hub](/app-hub/docs/overview)\n configurations, select the App Hub host project or\n the app-enabled folder's management project.\n\n2. In the Cloud Shell,\n go to the directory that contains your Terraform configuration.\n\n3. Edit the configuration file, locate the `google_monitoring_alert_policy`\n resource for your alerting policy, and then either modify or delete\n that resource.\n\n4. In the Cloud Shell, enter `terraform apply`.\n\nExport an alerting policy configuration to a Terraform configuration\n--------------------------------------------------------------------\n\nYou can export an alerting policy configuration from Google Cloud\nto a Terraform definition in your Terraform configuration.\nThis process is helpful if you created an alerting policy in Google Cloud\nafter you created your Terraform configuration.\n\nTo export your alerting policy, do the following:\n\n1. [Install and configure Terraform](/docs/terraform/install-configure-terraform)\n for you project. For [App Hub](/app-hub/docs/overview)\n configurations, select the App Hub host project or\n the app-enabled folder's management project.\n\n2. In the Cloud Shell,\n go to the directory that contains your Terraform configuration.\n\n3. Add an [`import`](https://developer.hashicorp.com/terraform/language/import) block to a file in your\n Terraform configuration.\n The import block has the following arguments:\n\n - `to`: The resource address as specified by your\n Terraform configuration. This field has the format of\n `google_monitoring_alert_policy.`\u003cvar translate=\"no\"\u003eRESOURCE_NAME\u003c/var\u003e,\n where \u003cvar translate=\"no\"\u003eRESOURCE_NAME\u003c/var\u003e is a value you choose.\n We recommend choosing a resource name that clearly describes\n your alerting policy.\n\n - `id`: The name of the alerting policy. This field has the format of\n `project/`\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e`/alertPolicies/`\u003cvar translate=\"no\"\u003eALERT_POLICY_ID\u003c/var\u003e.\n\n For example, you want to export an alerting policy that monitors CPU usage,\n so you set \u003cvar translate=\"no\"\u003eRESOURCE_NAME\u003c/var\u003e to\n `cpu_usage_threshold`. You then set `id` as the alerting policy name: \n\n ```\n import {\n to = google_monitoring_alert_policy.cpu_usage_threshold\n id = \"projects/my-alerting-project/alertPolicies/7160801095019277297\"\n }\n ```\n4. In the Cloud Shell, run the following command:\n\n ```\n terraform plan -generate-config-out=generated.tf\n ```\n\n The previous command creates a file named `generated.tf`, which lets you\n review your alerting policy definition\n before you add it to your Terraform configuration. You can edit\n the definition, move it to a different file, or\n leave it unchanged.\n5. In the Cloud Shell, enter `terraform apply` to import your\n alerting policy definition to your Terraform configuration.\n\nFor more information about generating definitions for resources that aren't\npart of your Terraform configuration, see\n[Generating configuration](https://developer.hashicorp.com/terraform/language/import/generating-configuration).\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."]]