使用Terraform启用活动日志诊断设置



当前存在一个模块,用于为链接到此处的Azure资源创建日志诊断设置。使用该门户,我能够为活动日志生成日志诊断设置,正如这里提到的那样。我试图启用活动日志诊断设置并将日志发送到存储帐户,但只遇到了此模块。

但是,似乎不可能使用此模块将活动日志发送到日志分析工作区。它也不支持门户中提到的日志类别(即AdministrativeSecurityServiceHealth等(,只提供ActionDeleteWrite。这让我相信,它们并非用于相同的目的。第一个模块需要target_resource_id,由于订阅级别中存在"活动日志",因此不存在此类id。

因此,是否可以使用第一个提到的模块或完全不同的模块来启用诊断设置?将感谢您对此事的任何帮助

您可以通过在azurerm_monitor_diagnostic_setting资源中将订阅id指定为target_resource_id来配置此功能。

示例:

resource "azurerm_monitor_diagnostic_setting" "example" {
name                           = "example"
target_resource_id             = "/subscriptions/85306735-db49-41be-b899-b0fc48095b01"
eventhub_name = azurerm_eventhub.diagnostics.name
eventhub_authorization_rule_id = azurerm_eventhub_namespace_authorization_rule.diagnostics.id
log {
category = "Administrative"
retention_policy {
enabled = false
}
}

您应该使用属性"log_analytics_workspace_id";

resource "azurerm_monitor_diagnostic_setting" "example" {
name                           = "example"
target_resource_id             = "/subscriptions/xxxx"
log_analytics_workspace_id     = azurerm_log_analytics_workspace.this.id
log_analytics_destination_type = "Dedicated" # or null see [documentation][1]
log {
category = "Administrative"
retention_policy {
enabled = false
}
}

我知道这是一个老问题,但我找到的最好方法是首先查询所有类别,然后启用所有类别。

// enable all diagnostic settings for a resource
data "azurerm_monitor_diagnostic_categories" "logs" {
resource_id = var.target_resource_id
}
resource "azurerm_monitor_diagnostic_setting" "monitor-diagnostic-settings" {
name                       = "diag-setting"
target_resource_id         = var.target_resource_id
storage_account_id         = var.storage_account_id
log_analytics_workspace_id = var.log_analytics_workspace_id
dynamic "log" {
iterator = entry
for_each = data.azurerm_monitor_diagnostic_categories.logs.log_category_types
content {
category = entry.value
enabled  = true
}
}
dynamic "metric" {
iterator = entry
for_each = data.azurerm_monitor_diagnostic_categories.logs.metrics
content {
category = entry.value
enabled  = true
}
}
}

最新更新