Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Dokumen ini menjelaskan cara menggunakan Google Cloud Penyedia Terraform
untuk membuat
kebijakan pemberitahuan di project Google Cloud Anda. Penyedia Terraform menyediakan resource berikut untuk kebijakan pemberitahuan dan saluran notifikasi: Google Cloud
Terraform adalah alat untuk membangun, mengubah, dan membuat versi infrastruktur. Terraform menggunakan file konfigurasi untuk menjelaskan
komponen yang diperlukan untuk menjalankan satu aplikasi atau seluruh infrastruktur Anda.
Untuk mengetahui informasi selengkapnya tentang cara menggunakan Terraform, lihat dokumen berikut:
Untuk mendapatkan izin yang
diperlukan untuk membuat kebijakan pemberitahuan menggunakan Terraform,
minta administrator untuk memberi Anda
peran IAM Monitoring Editor (roles/monitoring.editor)
di project Anda.
Untuk mengetahui informasi selengkapnya tentang cara memberikan peran, lihat Mengelola akses ke project, folder, dan organisasi.
Anda mungkin juga bisa mendapatkan
izin yang diperlukan melalui peran
khusus atau peran
bawaan lainnya.
Di Cloud Shell,
buka direktori yang berisi konfigurasi Terraform Anda.
Edit file konfigurasi dan tambahkan kebijakan pemberitahuan Anda.
Misalnya, konfigurasi berikut
menentukan kebijakan pemberitahuan yang mengirimkan notifikasi saat pemakaian CPU
instance VM lebih dari 50% selama lebih dari satu menit, dengan
notifikasi berulang yang dikirim setiap 30 menit.
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"}}
Dalam contoh sebelumnya, kolom notification_channels menentukan
saluran notifikasi untuk kebijakan pemberitahuan. Kolom
notification_channel_names mengonfigurasi saluran notifikasi tersebut
untuk mengirim notifikasi berulang. Kedua kolom mereferensikan saluran
notifikasi dengan display_nameemail, yang ditentukan
di tempat lain dalam konfigurasi Terraform. Untuk mengetahui informasi selengkapnya, lihat Membuat dan mengelola saluran notifikasi dengan Terraform.
Untuk mengubah kebijakan pemberitahuan, lakukan pengeditan, lalu terapkan kembali
konfigurasi Terraform. Untuk mengetahui informasi selengkapnya, lihat
Mengelola kebijakan pemberitahuan dengan Terraform.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 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."]]