Using New Relic Alerts child module, how to optionally use in that module a new child module for Teams/Slack alert channel?

Hello,

I basically have a service using a child New Relic alerting module, say located in the modules/terraform-nr-alert folder, which has it’s say 2 alert conditions (newrelic_nrql_alert_conditions resources) and two respective associated polices (newrelic_alert_policy resources). Within the each of the two newrelic_alert_policys, they are already tied to a required newrelic_alert_channel.email_channel.id in the channel_ids property of each of the two newrelic_alert_policys.

I want to now create a potentially optional teams notification child module - on the same level as the modules/terraform-nr-alert module of course - calling it say modules/terraform-nr-teams-notifications, and using a webhook URL for MSFT teams inside a newrelic_alert_channel resource. Optional as in, make a reference in the modules/terraform-nr-alert in each of the two newrelic_alert_policy resources in their channel_ids property, to that teams webhook notification channel.

So say in the modules/terraform-nr-alert folder I have one of the already existing alert policies below,

resource "newrelic_alert_policy" "policy__app" {
  name                = "Web App"
  incident_preference = "PER_CONDITION_AND_TARGET"
  channel_ids         = [ newrelic_alert_channel.email_channel.id ]
}

Could I make an optional reference in channel_ids property above to the modules/terraform-nr-teams-notifications newrelic_alert_channel resource containing the teams webhook (let’s call it say "teams_channel")?

I know I could make it non-optional and put it in like so, if I called the modules/terraform-nr-teams-notifications module say "nr_teams_notifications", and called the newrelic_alert_channel in that say "teams_channel",

resource "newrelic_alert_policy" "policy__app" {
  name                = "Web App"
  incident_preference = "PER_CONDITION_AND_TARGET"
  channel_ids         = [ newrelic_alert_channel.email_channel.id,
         module.nr_teams_notifications.newrelic_alert_channel.teams_channel.id ]
}

I think.

But how to make that module.nr_teams_notifications.newrelic_alert_channel.teams_channel.id above optional in case someone does not want to use my modules/terraform-nr-teams-notifications child module, but only the modules/terraform-nr-alert with its already required email channel alone?

Perhaps I could also not make a separate teams module and still just use the existing modules/terraform-nr-alert, and make the root/parent module pass in the webhook url in a variable that is made optional/conditional somehow, and based on whether that is provided or not the modules/terraform-nr-alert can create the alert channel for the msft teams webhook.

Seems like I might be able to do exactly right above depending on whether a variable for the webhook url is passed in a variable to the modules/terraform-nr-alert module, by doing something similar to the below:

I guess the question is, if the above is the way to go, how do I make that inclusion in the resource "newrelic_alert_policy" "policy__app" { .. } above optional in its channel_ids property shown above? Could I just perhaps do something like an expression by tacking on || null like in the below(again, just still using this inside the existing modules/terraform-nr-alert module) ?

resource "newrelic_alert_policy" "policy__app" {
  name                = "Web App"
  incident_preference = "PER_CONDITION_AND_TARGET"
  channel_ids         = [ newrelic_alert_channel.email_channel.id,
         newrelic_alert_channel.teams_channel.id  || null ]
}

Or perhaps, better yet, just define the variables with a default of "" - or would I do null?:

variable "teams_channel" {
  type = string
  default = ""
}

and keep the above attribute assignment without the || null - if that even worked - and this instead

resource "newrelic_alert_policy" "policy__app" {
  name                = "Web App"
  incident_preference = "PER_CONDITION_AND_TARGET"
  channel_ids         = [ newrelic_alert_channel.email_channel.id,
         newrelic_alert_channel.teams_channel.id  ]
}

Hi @aaa,

I’m afraid I must admit that I got a bit lost in the background information because there’s lots of moving parts here and I’m not familiar enough with New Relic to follow how all of these parts work together.

However, reading the last parts of your question I think you are asking about how you could make channel_ids have one additional element if the module caller set a particular optional variable.

If so, I think there are a couple different ways to get that done but the simplest one that comes to my mind would be the following:

variable "teams_channel_id" {
  type    = string
  default = null
}

resource "newrelic_alert_policy" "policy__app" {
  name                = "Web App"
  incident_preference = "PER_CONDITION_AND_TARGET"

  channel_ids = concat(
    [newrelic_alert_channel.email_channel.id],
    var.teams_channel_id[*],
  )
}

This concat call is concatenating together the explicitly-defined list containing the “email channel” ID and a dynamic list constructed from your variable using the [*] splat operator. Using [*] with a non-list value coerces that value to be either a single-element list or an empty list depending on whether the value is null, and so all together this means that you’ll either concatenate a single-element list containing the “teams channel id” or concatenate an empty list, which will therefore not affect the result at all.

1 Like

Appreciate it @apparentlymart ! Yes, a lot of moving parts here and I have to keep track myself, but this appears to be a way to go.