# siem-ingest / azure / t1 "Standard" # # The Activity Log, chosen resources' logs, and optionally Entra ID sign-in # and audit logs, streamed to one Event Hub for the SIEM, optionally to a # Sentinel workspace as well, with alerts when events are refused for lack of # capacity or stop arriving. locals { tags = merge({ owner = var.owner environment = var.environment data_classification = "confidential" managed_by = "terraform" }, var.extra_tags) law = var.log_analytics_workspace_id != "" ? var.log_analytics_workspace_id : null } data "azurerm_subscription" "current" {} resource "random_string" "suffix" { length = 6 special = false upper = false } resource "azurerm_resource_group" "siem" { name = "rg-${var.name}" location = var.location tags = local.tags } resource "azurerm_eventhub_namespace" "siem" { name = "${var.name}-${random_string.suffix.result}" location = azurerm_resource_group.siem.location resource_group_name = azurerm_resource_group.siem.name sku = "Standard" capacity = 1 auto_inflate_enabled = true maximum_throughput_units = var.max_throughput_units minimum_tls_version = "1.2" tags = local.tags } resource "azurerm_eventhub" "security" { name = "security-logs" namespace_id = azurerm_eventhub_namespace.siem.id partition_count = 8 message_retention = 7 } resource "azurerm_eventhub_consumer_group" "siem" { name = "siem" namespace_name = azurerm_eventhub_namespace.siem.name eventhub_name = azurerm_eventhub.security.name resource_group_name = azurerm_resource_group.siem.name } resource "azurerm_eventhub_namespace_authorization_rule" "diagnostics" { name = "azure-monitor" namespace_name = azurerm_eventhub_namespace.siem.name resource_group_name = azurerm_resource_group.siem.name listen = true send = true manage = true } resource "azurerm_eventhub_authorization_rule" "siem" { name = "siem-reader" namespace_name = azurerm_eventhub_namespace.siem.name eventhub_name = azurerm_eventhub.security.name resource_group_name = azurerm_resource_group.siem.name listen = true send = false manage = false } # --------------------------------------------------------------------------- # Sources # --------------------------------------------------------------------------- resource "azurerm_monitor_diagnostic_setting" "activity_log" { name = "to-siem" target_resource_id = data.azurerm_subscription.current.id eventhub_name = azurerm_eventhub.security.name eventhub_authorization_rule_id = azurerm_eventhub_namespace_authorization_rule.diagnostics.id log_analytics_workspace_id = local.law dynamic "enabled_log" { for_each = ["Administrative", "Security", "Policy", "Alert", "ServiceHealth", "ResourceHealth"] content { category = enabled_log.value } } } resource "azurerm_monitor_diagnostic_setting" "resources" { for_each = var.resource_ids name = "to-siem" target_resource_id = each.value eventhub_name = azurerm_eventhub.security.name eventhub_authorization_rule_id = azurerm_eventhub_namespace_authorization_rule.diagnostics.id log_analytics_workspace_id = local.law enabled_log { category_group = "allLogs" } } resource "azurerm_monitor_aad_diagnostic_setting" "entra" { count = var.include_entra_logs ? 1 : 0 name = "to-siem" eventhub_name = azurerm_eventhub.security.name eventhub_authorization_rule_id = azurerm_eventhub_namespace_authorization_rule.diagnostics.id log_analytics_workspace_id = local.law dynamic "enabled_log" { for_each = ["SignInLogs", "NonInteractiveUserSignInLogs", "ServicePrincipalSignInLogs", "AuditLogs", "RiskyUsers", "UserRiskEvents"] content { category = enabled_log.value } } } # --------------------------------------------------------------------------- # Alerts: refused for lack of capacity, and silence # --------------------------------------------------------------------------- resource "azurerm_monitor_action_group" "siem" { name = "${var.name}-alerts" resource_group_name = azurerm_resource_group.siem.name short_name = "siemingest" tags = local.tags dynamic "email_receiver" { for_each = toset(var.alert_emails) content { name = email_receiver.value email_address = email_receiver.value } } } resource "azurerm_monitor_metric_alert" "throttled" { name = "${var.name}-throttled" resource_group_name = azurerm_resource_group.siem.name scopes = [azurerm_eventhub_namespace.siem.id] description = "The namespace refused events for lack of capacity. Raise max_throughput_units." frequency = "PT5M" window_size = "PT15M" tags = local.tags criteria { metric_namespace = "Microsoft.EventHub/namespaces" metric_name = "ThrottledRequests" aggregation = "Total" operator = "GreaterThan" threshold = 0 } action { action_group_id = azurerm_monitor_action_group.siem.id } } # A subscription in use rarely goes an hour without an Activity Log event. An # hour with nothing usually means a diagnostic setting was removed or broke. resource "azurerm_monitor_metric_alert" "silent" { name = "${var.name}-silent" resource_group_name = azurerm_resource_group.siem.name scopes = [azurerm_eventhub_namespace.siem.id] description = "No events arrived for an hour. Check the diagnostic settings still point here." frequency = "PT15M" window_size = "PT1H" tags = local.tags criteria { metric_namespace = "Microsoft.EventHub/namespaces" metric_name = "IncomingMessages" aggregation = "Total" operator = "LessThanOrEqual" threshold = 0 } action { action_group_id = azurerm_monitor_action_group.siem.id } }