0

I'm using the pagerduty_ruleset_rule resource in combination with count to create multiple PagerDuty rules. Each rule is devised from a count.index of the services list of maps.


Variable definition - services list of maps (passed in as a var.services):

variable "services" {
  type        = list(object({ id = string, service_tags = list(string) }))
  description = "List of service IDs and tags to route events to"
}

Resource definition - pagerduty_ruleset_rule:

resource "pagerduty_ruleset_rule" "rule" {
  count = length(var.services)

  ruleset  = pagerduty_ruleset.custom_ruleset.id
  position = count.index
  disabled = false
  conditions {
    operator = "or"
    subconditions {
      operator = "contains"
      parameter {
        path  = "payload.group"
        value = "service:${var.services[count.index].service_tags[0]}"
      }
    }
    subconditions {
      operator = "contains"
      parameter {
        path  = "payload.group"
        value = "service:${var.services[count.index].service_tags[1]}"
      }
    }
  }
  actions {
    route {
      value = var.services[count.index].id
    }
  }
}

Notice how I have separate subconditions maps for each element in service_tags.

Is there a way to dynamically create subconditions maps based on the size of the service_tags list?

In the above code snippet, I assume that the size of the list is 2 (indexes [0] & [1]), but I'd like this to be dynamic - having the number of subconditions be equal to the size of the service_tags list.

bwalsh434
  • 17
  • 3
  • 3
    Woudn't [dynamic blocks](https://www.terraform.io/docs/configuration/expressions.html#dynamic-blocks) be what you are looking for? – Marcin Aug 21 '20 at 00:10
  • @Marcin Good point. I started playing around with this but considering each `service` has it's own set of `service_tags`, I would need to convert the `count` to `for_each` then create a nested `for_each` inside of that for the `service_tags`. Is the nested for_each doable? I can't seem to get it working. – bwalsh434 Aug 21 '20 at 19:54

0 Answers0