Well, I’ve had better days, I’m tied and disappointed. I’ve been trying to create a diagnostic setting and it hasn’t been successful. I have create 1 variable for all the setting and have defaults values defined as well. My problem is the each.value.. for the resource. I’m failing rather miserably.
variables.tf
variable "activity_log" {
description = "Azure monitor activity log alert rules for postgres server"
type = any
default = {
server_stop = {
name = "psqlsrv_stopped"
description = "Alert rule if this server is stopped"
criteria = {
category = "Administrative"
operation_name = "Microsoft.DBforPostgreSQL/flexibleServers/stop/action"
statuses = ["Succeeded", "Failed"]
}
}
server_restart = {
name = "psqlsrv_restarted"
description = "Alert rule if this server is restarted"
criteria = {
category = "Administrative"
operation_name = "Microsoft.DBforPostgreSQL/flexibleServers/restart/action"
statuses = ["Succeeded", "Failed"]
}
}
server_delete = {
name = "psqlsrv_deleted"
description = "Alert rule if this server is restarted"
criteria = {
category = "Administrative"
operation_name = "Microsoft.DBforPostgreSQL/flexibleServers/delete"
statuses = ["Succeeded", "Failed"]
}
}
servicehealth = {
name = "psqlsrv_servicehealth"
description = "Alert rule if this server has a service health alert"
criteria = {
category = "ServiceHealth"
events = ["Incident", "ActionRequired", "Security"]
}
}
resourcehealth = {
name = "psqlsrv_servicehealth"
description = "Alert rule if this server has a resource health alerts"
criteria = {
category = "ResourceHealth"
resource_health = {
current = ["Degraded", "Unavailable", "Unknown"]
previous = ["Available"]
reason = ["PlatformInitiated", "UserInitiated", "Unknown"]
}
}
}
}
}
main.tf
resource "azurerm_monitor_activity_log_alert" "alert_rule" {
for_each = var.activity_log
name = each.value["name"]
description = each.value["description"]
scopes = [azurerm_postgresql_flexible_server.postgres_server.id]
resource_group_name = element(var.rgname, 1)
criteria {
category = each.value.criteria.category
operation_name = each.value.criteria.operation_name
statuses = each.value.criteria.statuses
service_health {
events = each.value.service_health.events
}
resource_health {
current = each.value.resource_health.current
previous = each.value.resource_health.previous
reason = each.value.resource_health.reason
}
}
action {
action_group_id = data.azurerm_monitor_action_group.action_group.id
}
tags = local.project_tags
}
Create azure monitor diagnostic setting