Benachrichtigungsrichtlinien mit Terraform erstellen
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
In diesem Dokument wird beschrieben, wie Sie mit dem Google Cloud Terraform-Provider Google Cloud Benachrichtigungsrichtlinien in Ihrem Projekt erstellen. 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:
Bitten Sie Ihren Administrator, Ihnen die IAM-Rolle Monitoring Editor (roles/monitoring.editor) für Ihr Projekt zuzuweisen, damit Sie die Berechtigungen erhalten, die Sie zum Erstellen von Benachrichtigungsrichtlinien mit Terraform benötigen.
Weitere Informationen zum Zuweisen von Rollen finden Sie unter Zugriff auf Projekte, Ordner und Organisationen verwalten.
Wechseln Sie in Cloud Shell zu dem Verzeichnis, das Ihre Terraform-Konfiguration enthält.
Bearbeiten Sie die Konfigurationsdatei und fügen Sie Ihre Benachrichtigungsrichtlinie hinzu.
Die folgende Konfiguration definiert beispielsweise eine Benachrichtigungsrichtlinie, die eine Benachrichtigung sendet, wenn die CPU-Auslastung einer VM-Instanz länger als eine Minute über 50% liegt. Wiederholte Benachrichtigungen werden alle 30 Minuten gesendet.
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"}}
Im vorherigen Beispiel wird im Feld notification_channels der Benachrichtigungskanal für die Benachrichtigungsrichtlinie definiert. Mit dem Feld notification_channel_names wird konfiguriert, dass über diesen Benachrichtigungskanal wiederholte Benachrichtigungen gesendet werden. Beide Felder verweisen auf einen Benachrichtigungs-Channel mit einem display_name von email, der an anderer Stelle in der Terraform-Konfiguration definiert ist. Weitere Informationen finden Sie unter Benachrichtigungskanäle mit Terraform erstellen und verwalten.
Wenn Sie Ihre Benachrichtigungsrichtlinie ändern möchten, nehmen Sie die gewünschten Änderungen vor und wenden Sie die Terraform-Konfiguration noch einmal an. Weitere Informationen finden Sie unter Benachrichtigungsrichtlinien mit Terraform verwalten.
[[["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,["# 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."]]