连接 Azure 日志工作区与数据收集规则 (Terraform)

huangapple go评论58阅读模式
英文:

Connect Azure Log Workspace with Data Collection Rule (Terrraform)

问题

我正在尝试通过Terraform为表"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"并单击"创建转换"时,我甚至无法选择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能够自动连接到Log Analytics,或者有一个额外的资源用于连接,但我找不到类似的内容。

英文:

I am trying to create a Data Collection Rule for the table "Perf" by terrafrom, but I have trouble get my "azurerm_log_analytics_workspace" connected to the "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]
  }
}

Both resources are in the same resource group and same region.

I tried to create a DCR through portal and compare the templates with the one created by terraform and they are the same. The only difference is in the Log Analytics workspace where I find "defaultDataCollectionRuleResourceId" with the ID of the the created DCR. Then I tried to add my terraform DCR manually to the Log Analytics, but when selecting the table "Perf" and clicking on "Create Transformation" I can't even select the DCR. I also tried to create it via "azapi_resource", but had the same Issue.

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
    ]
  }
}

I would have expected the DCR connect automatically to Log Analytics, or that there is an additional resource for connection, but I could not find anything like this.

答案1

得分: 1

我通过直接从CLI使用日志分析命令找到了解决此问题的方法:

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中拥有所有内容,这只是一个备注。

英文:

I found a workaround for this Issue by using log analytics command from CLI directly:

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
  ]
}

I found this in the log analytics cli documentation. When you execute the terraform, DCR is connected correctly, but you will not be able to create transformations in the azure portal, which is fine for us, as we have everything inside terraform, but just as remark.

huangapple
  • 本文由 发表于 2023年2月23日 21:22:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545431.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定