Girolamo
Girolamo

Reputation: 311

Add environment based Multiple Notification Channel to GCP Alert Policy with Terraform Lookup

I'm trying to add multiple notification channels to a GCP Alert policy with terraform. My issue is that I need to add different notification channels based on the production environment where they are deployed. As long as I keep the notification channel unique, I can easily deploy in the following way.

Here is my variables.tf file:

locals {
  notification_channel = {
    DEV = "projects/[PROJECT_ID]/notificationChannels/[CHANNEL_ID]"
    PRD = "projects/[PROJECT_ID]/notificationChannels/[CHANNEL_ID]"
  }
}

Here is my main.tf file:

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"
      }
    }
  }
    
   user_labels = {
     foo = "bar"
   }

   notification_channels = [lookup(local.notification_channel, terraform.workspace)]
}

My issue here happens when I try to map multiple notification channels instead of one per environment. Something like:

locals {
  notification_channel = {
    DEV = ["projects/[PROJECT_ID]/notificationChannels/[CHANNEL_ID]", "projects/[PROJECT_ID]/notificationChannels/[CHANNEL_ID]" ]...
  }
}

However, if I try this way, system tells me that Inappropriate value for attribute "notification_channels": element 0: string.

Here's documentation of:

Could you help?

Upvotes: 0

Views: 657

Answers (1)

Marko E
Marko E

Reputation: 18138

If I understood your question, you actually need only to remove the square brackets:

notification_channels = lookup(local.notification_channel, terraform.workspace)

Since the local variable notification_channel is already a list, you only need to use lookup to fetch the value based on the workspace you are currently in.

Upvotes: 1

Related Questions