Benachrichtigungsrichtlinien mit Terraform verwalten
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
In diesem Dokument wird beschrieben, wie Sie mit demGoogle Cloud Terraform-Provider Google Cloud Benachrichtigungsrichtlinien in Ihrem Projekt bearbeiten und löschen. Der Google Cloud Terraform-Provider bietet die folgenden Ressourcen für Benachrichtigungsrichtlinien und Benachrichtigungskanäle:
Terraform ist ein Tool zum Erstellen, Ändern und zur Versionsverwaltung von Infrastruktur. Terraform verwendet Konfigurationsdateien, um die Komponenten zu beschreiben, die zum Ausführen einer einzelnen Anwendung oder Ihrer gesamten Infrastruktur erforderlich sind.
Weitere Informationen zur Verwendung von Terraform finden Sie in den folgenden Dokumenten:
Diese Funktion wird nur für Google Cloud -Projekte unterstützt.
Wählen Sie für App Hub-Konfigurationen das App Hub-Hostprojekt oder das Verwaltungsprojekt des für Apps aktivierten Ordners aus.
Hinweise
Bitten Sie Ihren Administrator, Ihnen die folgenden IAM-Rollen für Ihr Projekt zuzuweisen, um die Berechtigungen zu erhalten, die Sie zum Ändern von Benachrichtigungsrichtlinien mit Terraform benötigen:
Wechseln Sie in Cloud Shell zu dem Verzeichnis, das Ihre Terraform-Konfiguration enthält.
Bearbeiten Sie die Konfigurationsdatei, suchen Sie die google_monitoring_alert_policy-Ressource für Ihre Benachrichtigungsrichtlinie und ändern oder löschen Sie diese Ressource.
Geben Sie in Cloud Shell terraform apply ein.
Konfiguration einer Benachrichtigungsrichtlinie in eine Terraform-Konfiguration exportieren
Sie können eine Konfiguration für Benachrichtigungsrichtlinien aus Google Cloudin eine Terraform-Definition in Ihrer Terraform-Konfiguration exportieren.
Dieser Vorgang ist hilfreich, wenn Sie eine Benachrichtigungsrichtlinie in Google Clouderstellt haben, nachdem Sie Ihre Terraform-Konfiguration erstellt haben.
So exportieren Sie Ihre Benachrichtigungsrichtlinie:
Wechseln Sie in Cloud Shell zu dem Verzeichnis, das Ihre Terraform-Konfiguration enthält.
Fügen Sie einer Datei in Ihrer Terraform-Konfiguration einen import-Block hinzu.
Der Importblock hat die folgenden Argumente:
to: Die Ressourcenadresse, wie in Ihrer Terraform-Konfiguration angegeben. Dieses Feld hat das Format google_monitoring_alert_policy.RESOURCE_NAME, wobei RESOURCE_NAME ein von Ihnen ausgewählter Wert ist.
Wir empfehlen, einen Ressourcennamen zu wählen, der Ihre Benachrichtigungsrichtlinie eindeutig beschreibt.
id: Der Name der Benachrichtigungsrichtlinie. Dieses Feld hat das Format project/PROJECT_ID/alertPolicies/ALERT_POLICY_ID.
Sie möchten beispielsweise eine Benachrichtigungsrichtlinie exportieren, die die CPU-Auslastung überwacht. Dazu legen Sie RESOURCE_NAME auf cpu_usage_threshold fest. Legen Sie dann id als Namen der Benachrichtigungsrichtlinie fest:
import {
to = google_monitoring_alert_policy.cpu_usage_threshold
id = "projects/my-alerting-project/alertPolicies/7160801095019277297"
}
Führen Sie in Cloud Shell den folgenden Befehl aus:
terraform plan -generate-config-out=generated.tf
Mit dem vorherigen Befehl wird eine Datei namens generated.tf erstellt, in der Sie die Definition der Benachrichtigungsrichtlinie überprüfen können, bevor Sie sie Ihrer Terraform-Konfiguration hinzufügen. Sie können die Definition bearbeiten, in eine andere Datei verschieben oder unverändert lassen.
Geben Sie in Cloud Shell terraform apply ein, um die Definition Ihrer Benachrichtigungsrichtlinie in Ihre Terraform-Konfiguration zu importieren.
Weitere Informationen zum Generieren von Definitionen für Ressourcen, die nicht Teil Ihrer Terraform-Konfiguration sind, finden Sie unter Konfiguration generieren.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-08-28 (UTC)."],[],[],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."]]