Mengelola kebijakan pemberitahuan dengan Terraform
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Dokumen ini menjelaskan cara menggunakan
Penyedia TerraformGoogle Cloud untuk mengedit dan menghapus
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:
Fitur ini hanya didukung untuk Google Cloud project.
Untuk konfigurasi App Hub, pilih project host App Hub atau project pengelolaan folder yang mendukung aplikasi.
Sebelum memulai
Untuk mendapatkan izin yang
diperlukan untuk mengubah kebijakan pemberitahuan menggunakan Terraform,
minta administrator Anda untuk memberi Anda
peran IAM berikut di project Anda:
Untuk mengedit atau menghapus kebijakan pemberitahuan di project Google Cloud Anda, lakukan langkah-langkah berikut:
Instal dan konfigurasi Terraform
untuk project Anda. Untuk konfigurasi App Hub, pilih project host App Hub atau project pengelolaan folder yang mendukung aplikasi.
Di Cloud Shell,
buka direktori yang berisi konfigurasi Terraform Anda.
Edit file konfigurasi, temukan resource google_monitoring_alert_policy
untuk kebijakan pemberitahuan Anda, lalu ubah atau hapus
resource tersebut.
Di Cloud Shell, masukkan terraform apply.
Mengekspor konfigurasi kebijakan pemberitahuan ke konfigurasi Terraform
Anda dapat mengekspor konfigurasi kebijakan pemberitahuan dari Google Cloud
ke definisi Terraform dalam konfigurasi Terraform Anda.
Proses ini berguna jika Anda membuat kebijakan pemberitahuan di Google Cloud
setelah membuat konfigurasi Terraform.
Untuk mengekspor kebijakan pemberitahuan, lakukan langkah berikut:
Instal dan konfigurasi Terraform
untuk project Anda. Untuk konfigurasi App Hub, pilih project host App Hub atau project pengelolaan folder yang mendukung aplikasi.
Di Cloud Shell,
buka direktori yang berisi konfigurasi Terraform Anda.
Tambahkan blok import ke file dalam konfigurasi Terraform Anda.
Blok impor memiliki argumen berikut:
to: Alamat resource seperti yang ditentukan oleh
konfigurasi Terraform Anda. Kolom ini memiliki format
google_monitoring_alert_policy.RESOURCE_NAME,
dengan RESOURCE_NAME adalah nilai yang Anda pilih.
Sebaiknya pilih nama resource yang jelas mendeskripsikan
kebijakan pemberitahuan Anda.
id: Nama kebijakan pemberitahuan. Kolom ini memiliki format
project/PROJECT_ID/alertPolicies/ALERT_POLICY_ID.
Misalnya, Anda ingin mengekspor kebijakan pemberitahuan yang memantau penggunaan CPU, jadi Anda menetapkan RESOURCE_NAME ke cpu_usage_threshold. Kemudian, Anda menetapkan id sebagai nama kebijakan pemberitahuan:
import {
to = google_monitoring_alert_policy.cpu_usage_threshold
id = "projects/my-alerting-project/alertPolicies/7160801095019277297"
}
Di Cloud Shell, jalankan perintah berikut:
terraform plan -generate-config-out=generated.tf
Perintah sebelumnya membuat file bernama generated.tf, yang memungkinkan Anda meninjau definisi kebijakan pemberitahuan sebelum menambahkannya ke konfigurasi Terraform. Anda dapat mengedit
definisi, memindahkannya ke file lain, atau
membiarkannya tidak berubah.
Di Cloud Shell, masukkan terraform apply untuk mengimpor definisi kebijakan pemberitahuan ke konfigurasi Terraform Anda.
Untuk mengetahui informasi selengkapnya tentang cara membuat definisi untuk resource yang bukan bagian dari konfigurasi Terraform Anda, lihat Membuat konfigurasi.
[[["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,["# 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."]]