I have problem with data rule. I create terraform code that create Log Analytics workspace and data rule and diag settings and all is fine all create butt when i want to create transformation it did not see existing data rule. Someone can help me or maybe now how to fix? I use azurerm version = “~>3.103.1”
This is my code
`
resource "azurerm_monitor_diagnostic_setting" "backenddiagnosticsettings" {
name = "${var.resource_group_prefix}-${var.application_environment_name}-be${var.instance_name}-logs"
target_resource_id = azurerm_linux_web_app.backend.id[enter image description here]
log_analytics_workspace_id = azurerm_log_analytics_workspace.logworkspace.id # data.azurerm_log_analytics_workspace.logworkspace.id
enabled_log {
category = "AppServiceConsoleLogs"
retention_policy {
enabled = true
}
}
metric {
category = "AllMetrics"
retention_policy {
enabled = true
}
}
}
data "azurerm_monitor_data_collection_rule" "example" {
name = "example-rule"
resource_group_name = azurerm_resource_group.example.name
}
output "rule_id" {
value = data.azurerm_monitor_data_collection_rule.example.id
}
resource "azurerm_log_analytics_workspace" "logworkspace" {
name = "${var.resource_group_prefix}-${var.application_environment_name}${var.instance_name}-logs"
location = var.location
resource_group_name = data.azurerm_resource_group.apprg.name
sku = "PerGB2018"
retention_in_days = 30
data_collection_rule_id = ["${azurerm_monitor_data_collection_rule.backendlogs-dcr.id}"]
}
resource "azurerm_monitor_data_collection_rule" "backendlogs-dcr" {
name = "${var.resource_group_prefix}-${var.application_environment_name}-backend-dcr"
resource_group_name = data.azurerm_resource_group.apprg.name
location = var.location
destinations {
log_analytics {
name = "${azurerm_log_analytics_workspace.logworkspace.name}"
workspace_resource_id = azurerm_log_analytics_workspace.logworkspace.id
}
}
data_flow {
streams = ["Microsoft-Table-AppServiceConsoleLogs"]
#destinations = ["example-destination-log"]
destinations = ["${azurerm_log_analytics_workspace.logworkspace.name}"]
output_stream = "Microsoft-AppServiceConsoleLogs"
transform_kql = "sourcen| extend Context = parse_json(ResultDescription)n| extend Thread_CF = tostring(Context.thread)n| extend Level_CF = tostring(Context.level)n| extend Logger_CF = tostring(Context.logger)n| extend Message_CF = tostring(Context.message)n| project-away Context,ResultDescription"
#"sourcen| extend LL_CF = substring(ResultDescription, 12, 34)"
}
}
enter image description here
To help with this error
Nazar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
It seems like you’re having trouble getting your Terraform code to recognize an existing data rule when creating a Log Analytics workspace. Here are some tips to make it more understandable:
-
Check Data Rule Existence: Make sure the data rule you’re referring to (
azurerm_monitor_data_collection_rule.backendlogs-dcr
) exists before you create the Log Analytics workspace. If it’s created later, Terraform might miss it, so you might need to runterraform apply
again. -
Using Interpolation: Instead of directly mentioning the data rule ID in the Log Analytics workspace setup, try using interpolation with the data rule resource directly. For instance:
data_collection_rule_id = [azurerm_monitor_data_collection_rule.backendlogs-dcr.id]
-
Verify Data Rule Name and ID: Double-check that the data rule you’re referring to has the correct name (
backendlogs-dcr
). If the name doesn’t match, Terraform won’t find it. -
Managing Dependencies: Ensure you’ve properly managed dependencies between resources. If the Log Analytics workspace depends on the data rule, explicitly specify the dependency like this:
depends_on = [azurerm_monitor_data_collection_rule.backendlogs-dcr]
By following these steps, you should be able to ensure that your Log Analytics workspace recognizes the existing data rule and uses it for transformations. If you’re still having trouble, consult the Terraform documentation or Azure provider documentation for more guidance.
Piyush Ranjan Satapathy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.