使用数据收集规则连接Azure日志工作区(Terrraform)

lymnna71  于 2023-02-25  发布在  其他
关注(0)|答案(1)|浏览(121)

我正尝试按terrafrom为表“Perf”创建数据收集规则,但无法将“azurerm_log_analytics_workspace”连接到“azurerm_monitor_data_collection_rule"。

resource "azurerm_resource_group" "this" {
  location = var.environment_config.location_name
  name     = local.naming.rg_name
}

resource "azurerm_log_analytics_workspace" "this" {
  name                = local.naming.log_name
  location            = azurerm_resource_group.this.location
  resource_group_name = azurerm_resource_group.this.name
}

resource "azurerm_monitor_data_collection_rule" "this" {
  name                = local.naming.dcr_name
  resource_group_name = azurerm_resource_group.this.name
  location            = azurerm_resource_group.this.location

  destinations {
    log_analytics {
      workspace_resource_id = azurerm_log_analytics_workspace.this.id
      name                  = local.naming.log_name
    }
  }

  data_flow {
    streams      = ["Microsoft-Table-Perf"]
    destinations = [local.naming.log_name]
  }
}

两个资源都在同一资源组和同一区域中。
我尝试通过门户创建DCR,并将模板与terraform创建的模板进行比较,发现它们是相同的。唯一的区别是在Log Analytics工作区中,我发现“defaultDataCollectionRuleResourceId”具有创建的DCR的ID。然后,我尝试将terraform DCR手动添加到Log Analytics。但当选择表“Perf”并单击“Create Transformation”时,我甚至无法选择DCR。我还尝试通过“azapi_resource"创建它,但遇到了相同的问题。

resource "azapi_resource" "azurerm_monitor_data_collection_rule" {
  type      = "Microsoft.Insights/dataCollectionRules@2021-09-01-preview"
  name      = local.naming.dcr_name
  location  = azurerm_resource_group.this.location
  parent_id = azurerm_resource_group.this.id

  body = jsonencode({
    properties = {
      dataFlows = [
        {
          destinations = [local.naming.log_name]
          streams      = ["Microsoft-Table-Perf"]
        }
      ]

      destinations = {
        logAnalytics = [
          {
            name                = local.naming.log_name
            workspaceResourceId = azurerm_log_analytics_workspace.this.id
          }
        ]
      }
    }
    kind = "WorkspaceTransforms"
  })
  lifecycle {
    ignore_changes = [
      tags
    ]
  }
}

我本来期望DCR自动连接到日志分析,或者有一个额外的连接资源,但我找不到这样的东西。

bkkx9g8r

bkkx9g8r1#

我找到了解决此问题的方法,即直接从CLI使用log analytics命令:

resource "null_resource" "connect_dcr_to_log_analytics" {

  provisioner "local-exec" {
    command = "az monitor log-analytics workspace update --resource-group ${local.naming.rg_name} --workspace-name ${local.naming.log_name} --data-collection-rule \"${azapi_resource.azurerm_monitor_data_collection_rule.id}\""
  }

  depends_on = [
    azapi_resource.azurerm_monitor_data_collection_rule,
    azurerm_log_analytics_workspace.this
  ]
}

我在日志分析cli文档中发现了这个。当你执行terraform时,DCR连接正确,但是你不能在azure门户中创建转换,这对我们来说很好,因为我们在terraform中有所有的东西,但是正如备注。

相关问题