Terraform で Slack 通知を設定
Terraform で Slack 通知を設定
Terraform で google_monitoring_notification_channel のリソースを作成するときにエラーが発生したため、その対処方法をまとめました。
発生したエラー
google_cloud_monitoring.tf に google_monitoring_notification_channel.project_name_noti_slack を追加。
# 略
resource "google_monitoring_notification_channel" "project_name_noti_slack" {
display_name = "project_name_noti"
type = "slack"
labels = {
auth_token : ""
channel_name : "#project_name_noti"
team : "PROJECT_NAME"
}
}
# 略
$ terraform apply を実行すると、以下のエラーが発生。
$ terraform apply
略
╷
│ Error: Error creating NotificationChannel: googleapi: Error 400: Field notification_channel.labels[auth_token] had an invalid value: Value must NOT be empty if provided.
│
│ with google_monitoring_notification_channel.project_name_noti_slack,
│ on google_cloud_monitoring.tf line 999, in resource "google_monitoring_notification_channel" "project_name_noti_slack":
│ 999: resource "google_monitoring_notification_channel" "project_name_noti_slack" {
│
╵
対応方法
以下の手順でエラーを解消しました。
- Google Cloud Console でリソースを一旦作成
- Terraform で、1 で作成したリソースを import
Google Cloud Console でリソースを一旦作成
https://console.cloud.google.com/monitoring/alerting/notifications?referrer=search&project={{project_name}}
を開いて、リソースを作成する。
オブザーバビリティ -> アラート -> 通知チャンネル

Slack -> Add New

Allow

Add Slack Channel で、チャンネル名 と 表示用の名前 を入力し、Save

Terraform で、1 で作成したリソースを import
google_monitoring_notification_channel | Resources | hashicorp/google | Terraform | Terraform Registry
に記載の方法で、リソースを import できます。
Import
NotificationChannel can be imported using any of these accepted formats:
{{name}}In Terraform v1.5.0 and later, use an
importblock to import NotificationChannel using one of the formats above. For example:import { id = "{{name}}" to = google_monitoring_notification_channel.default }When using the
terraform importcommand, NotificationChannel can be imported using one of the formats above. For example:$ terraform import google_monitoring_notification_channel.default {{name}}
今回は、import block を利用しました。
import.tf を作成
https://console.cloud.google.com/monitoring/alerting/notifications?referrer=search&project={{project_name}}
を開いて、一番左のアイコンをクリックするとリソースの id をコピーできます。

import.tf を作成。
import {
id = "projects/{{project_name}}/notificationChannels/0000000000000000000"
to = google_monitoring_notification_channel.project_name_noti_slack
}
$ terraform plan でインポートされるリソースを確認
$ terraform plan
略
# google_monitoring_notification_channel.project_name_noti_slack will be imported
resource "google_monitoring_notification_channel" "project_name_noti_slack" {
description = null
display_name = "project_name_noti"
enabled = true
force_delete = false
id = "projects/{{project_name}}/notificationChannels/0000000000000000000"
labels = {
"auth_token" = null
"channel_name" = "#project_name_noti"
"team" = "PROJECT_NAME"
}
name = "projects/{{project_name}}/notificationChannels/0000000000000000000"
project = "{{project_name}}"
type = "slack"
user_labels = {}
verification_status = null
timeouts {}
}
Plan: 1 to import, 99 to add, 0 to change, 0 to destroy.
$ terraform apply で反映
$ terraform apply
略
import.tf を削除
$ terraform apply で import が完了すると import.tf は必要なくなるため削除。
