1
votes

I'm creating alerts (azurerm_monitor_scheduled_query_rules_alert) in Azure using Terraform. You can include a list of action groups (i.e. the groups that you send the alerts to).

Within the TFVars file I will pass in a variable value of a list of the names of the action groups. However, the alert module needs the ID's of the resources, not the names. So I have a data source that would get the info of an action group. The Alert resource can then refer to the data source to acquire the azure resource id.

This works fine if I have just one action group, but the size of the list with action group names can vary. I'm trying to figure out how I can convert all action group names to id for ingestion by the resource.

resource "azurerm_monitor_scheduled_query_rules_alert" "tfTestAlertExample" {
for_each = {for alert in var.scheduled_query_alerts : alert.name => alert}

  name                = each.value["name"]
  location            = data.azurerm_resource_group.resource_group.location
  resource_group_name = data.azurerm_resource_group.resource_group.name

  action {

    # --This part here. How do I get make this dynamic?--
    action_group           = [
      data.azurerm_monitor_action_group.action_group.id
    ] 

    email_subject          = each.value["email_subject"]
    custom_webhook_payload = "{}"
  }

  data_source_id = ................ etc

So in the above example, there will only be one action{} block, but the Action_group list within that needs to be dynamic, with ID's retrieved from a data source. Or maybe there's another way of doing this that I've not considered.

Any help would be greatly appreciated.

1
Do you mean something like this: action_group = data.azurerm_monitor_action_group.action_group.*.id or action_group = data.azurerm_monitor_action_group.action_group[*].id? - Marcin
That'd be good but the problem is the conversion of name to id. Not sure if I can have a data source resource that is dynamic. - ScubaManDan
So you want to filter your data source based on group names? - Marcin
i.e. it might be that I have 3 action groups, or maybe 2. How do I retrieve the IDs for each of these from a dynamic list - ScubaManDan
You can have data source dynamic. Just use for_each and then fetch IDs in the resource. - rkm

1 Answers

1
votes

If you just want to convert the list of action group names to its Ids, you can do it like this:

# decalre the variables
variable "action_group_names" {
  default = ["nancyAG1","nancyAG2"]
}

# restrive the Id of action group
data "azurerm_monitor_action_group" "example" {
  count = length(var.action_group_names)
  resource_group_name = "existingRG"
  name                = element(var.action_group_names,count.index)
}

# output the result to the terminal
output "groups_id" {
  value = data.azurerm_monitor_action_group.example[*].id
  
}

Then pass Ids to the resource like this:

resource "azurerm_monitor_scheduled_query_rules_alert" "example" {
  name                = format("%s-queryrule", var.prefix)
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

  action {
    action_group           = data.azurerm_monitor_action_group.example[*].id
    email_subject          = "Email Header"
    custom_webhook_payload = "{}"
  }

Check the action_group Ids.

enter image description here