Adding multiple destinations to new relic workflows using terraform

slack channel ids

variable "channel_ids" {
   type    = set(string)
   default = ["XXXXXXXXXX","YYYYYYYYY"]
}

creating notification channels using slack channel ids

resource "newrelic_notification_channel" "notification_channel" {
  for_each       = var.channel_ids
  name           = "test" # will modify if required
  type           = "SLACK" # will parameterize this
  destination_id = "aaaaaaaaa-bbbbb-cccc-ddddd-eeeeeeeeee" 
  product        = "IINT"
    property {
      key   = "channelId"
      value = each.value
  }
}

Now I want to create something like below (two destinations)

resource "newrelic_workflow" "newrelic_workflow" {
  name = "my-workflow"
  muting_rules_handling = "NOTIFY_ALL_ISSUES"

  issues_filter {
    name = "Filter-name"
    type = "FILTER"

    predicate {
      attribute = "accumulations.policyName"
      operator = "EXACTLY_MATCHES"
      values = [ "policy_name" ]
    }
  }
    destination {
      channel_id = newrelic_notification_channel.notification_channel.id
    }
    
    destination {
      channel_id = newrelic_notification_channel.notification_channel.id
    }
}

I tried using for_each and for loop but no luck. Any idea on how to get my desired output?

Is it possible to loop through and create multiple destinations within the same resource, like attaching multiple destination to a single workflow?

Hi @dodugu,

It looks like your goal is to have one destination block for each instance of newrelic_notification_channel.notification_channel, with each one referring to the id of its corresponding object.

If so, I think the following would achieve that:

resource "newrelic_workflow" "newrelic_workflow" {
  name = "my-workflow"
  muting_rules_handling = "NOTIFY_ALL_ISSUES"

  issues_filter {
    name = "Filter-name"
    type = "FILTER"

    predicate {
      attribute = "accumulations.policyName"
      operator = "EXACTLY_MATCHES"
      values = [ "policy_name" ]
    }
  }

  dynamic "destination" {
    for_each = newrelic_notification_channel.notification_channel
    content {
      channel_id = destination.value.id
    }
  }
}

The dynamic "destination" block above is an example of a dynamic block, which produces a dynamic number of destination blocks based on the number of elements of newrelic_notification_channel.notification_channel.

Hi @apparentlymart,

This is exactly what I am looking for and worked great. I’m very new to terraform and didn’t know about dynamic block.

Thank you very much! Appreciate it!