Roger Chen
Roger Chen

Reputation: 303

How to set resource health alert condition with terraform

I'm trying to use terraform to create resource health alert, it's pretty simple very terraform

resource "azurerm_monitor_activity_log_alert" "resourcehealth" {
  name                = "${var.client_initial}-MCS Optimise Resource Health"
  description         = "${var.client_initial}-MCS Optimise Resource Health Alerts"
  resource_group_name = var.resource_group_name
  scopes              = [var.scopes]
  criteria {
    category = "ResourceHealth"
  }

  action {
    action_group_id = var.action_group_id
  }
  tags = var.tags
}

However, i found it's lack of ability to further set granual alert condition, like we only want alerted on when current resource status is degraded or unavailable, and reson type is platform initiated. Terraform seems to be giving all to all the conditions.

Upvotes: 3

Views: 3114

Answers (1)

Zaduvalo
Zaduvalo

Reputation: 39

Single resource scope

resource "azurerm_monitor_activity_log_alert" "resourcehealth" {
  name                = "${var.client_initial}-MCS Optimise Resource Health"
  description         = "${var.client_initial}-MCS Optimise Resource Health Alerts"
  resource_group_name = var.resource_group_name
  scopes              = [var.scope]
  criteria {
    resource_id    = var.scope
    operation_name = "Microsoft.Resourcehealth/healthevent/Activated/action"
    category       = "ResourceHealth"
  }

  action {
    action_group_id = var.action_group_id
  }
  tags = var.tags
}

For multiple resources use for_each

resource "azurerm_monitor_activity_log_alert" "resourcehealth" {
  for_each = var.scopes

  name                = "${var.client_initial}-MCS Optimise Resource Health"
  description         = "${var.client_initial}-MCS Optimise Resource Health Alerts"
  resource_group_name = var.resource_group_name
  scopes              = [each.key]
  criteria {
    resource_id    = each.key
    operation_name = "Microsoft.Resourcehealth/healthevent/Activated/action"
    category       = "ResourceHealth"
  }

  action {
    action_group_id = var.action_group_id
  }
  tags = var.tags
}

Upvotes: 1

Related Questions