Google CloudのCloud Monitoringにおいてslack通知する仕組みをterraformで構築したい。 Email通知の例は公式のガイドに記載があるので、これを参考にslack通知を実現したい。
Cloud MonitoringとSlackの連携
Cloud MonitoringとSlack連携の方法も公式ドキュメントに記載がある。 これに従い、SlackにGoogle Cloud Monitoring アプリを導入して利用する。SlackのAdmin権限が必要。
Slack連携をTerraformで管理したい
手動でセットアップする場合は上記手順で問題ないが、問題はterraformでセットアップする場合が困難。 自作のSlack AppであればOAuth Tokenを取得して sensitive_labels.auth_token に指定すればよさそうだが、3rd Party Appの場合はOAuth Tokenを取得できない。 このため、terraformで slack連携のnotificationを作成することができない。
代替策として、手動でslack連携を作成してimportする方法がある。
importするときは通知のURLを参照することが可能(操作ボタンのリンクアイコン、projects/[PROJECT_ID]/notificationChannels/[CHANNEL_ID] 形式)なので、これをリソースIDとしてimportすればよい。

$ cat main.tf provider "google" { region = "asia-northeast1" } resource "google_monitoring_alert_policy" "alert_policy" { display_name = "My Alert Policy" combiner = "OR" conditions { display_name = "test condition" condition_threshold { filter = "metric.type=\"compute.googleapis.com/instance/disk/write_bytes_count\" AND resource.type=\"gce_instance\"" duration = "60s" comparison = "COMPARISON_GT" aggregations { alignment_period = "60s" per_series_aligner = "ALIGN_RATE" } } } notification_channels = [google_monitoring_notification_channel.slack.name] user_labels = { foo = "bar" } } resource "google_monitoring_notification_channel" "slack" { display_name = "Sample Notification" type = "slack" labels = { "channel_name" = "#notifications" "team" = "my-slack-workspace-name" } lifecycle { ignore_changes = [labels["auth_token"]] } } import { to = google_monitoring_notification_channel.slack id = "projects/my-project-name/notificationChannels/4507022200563725809" } $ terraform plan google_monitoring_notification_channel.slack: Preparing import... [id=projects/my-project-name/notificationChannels/4507022200563725809] google_monitoring_notification_channel.slack: Refreshing state... [id=projects/my-project-name/notificationChannels/4507022200563725809] google_monitoring_alert_policy.alert_policy: Refreshing state... [id=projects/my-project-name/alertPolicies/11273926698407592966] Terraform will perform the following actions: # google_monitoring_notification_channel.slack will be imported resource "google_monitoring_notification_channel" "slack" { description = null display_name = "Sample Notification" enabled = true force_delete = false id = "projects/my-project-name/notificationChannels/4507022200563725809" labels = { "auth_token" = null "channel_name" = "#notifications" "team" = "my-slack-workspace-name" } name = "projects/my-project-name/notificationChannels/4507022200563725809" project = "my-project-name" type = "slack" user_labels = {} verification_status = null } Plan: 1 to import, 0 to add, 0 to change, 0 to destroy.
こうすればリソースのインポートと管理自体は可能
notificationを変更できない
一度インポートしたら、あとは通知先チャンネルの変更などをterraformを通して実行できるかというと、実はこれができない。 Google Cloud のAPIではauth_tokenを未指定だとAPI validationエラーになるのでterraformのapplyが実行できない。 また、適当な文字列をauth_tokenに指定すると今度はslack通知が動作しない。 このため、上記リソースをインポートしても基本的に利用できない。
github上でもissueとして報告されている。 provider-googleの不具合というよりGoogle Cloud APIの不具合?
対策
自作のSlack Appを利用する。 この場合はBot User OAuth tokenが取得でき、これをauth_tokenに指定すればよさそう。
または、更新を諦めて手動で構築したnotificationリソースをdataリソースで参照だけ行う、または文字列としてgoogle_monitoring_alert_policyに渡してやる。 こうすればslack通知の設定の変更はterraformで制御できないが、変更頻度がより多いであろうアラートポリシーの制御は可能となる。