I'm deploying a Key Vault in Azure via an ARM template and the "azurerm_template_deployment" resource, but I need to enable diagnostic settings and stream diagnostic data to my existing log analytics workspace.
The aim is to deploy the key vault itself with the diagnostic settings in the same "terraform apply"
When running terraform apply, the key vault itself gets deployed, but the diagnostic settings do not get enabled, and fail via the following message:
[error]Error: Error creating Monitor Diagnostics Setting "kv-diagnostics" for Resource "/subscriptions/----/resourceGroups/rg-test-001/providers/Microsoft.Resources/deployments/kv-diagnostics": insights.DiagnosticSettingsClient#CreateOrUpdate: Failure responding to request: StatusCode=404 -- Original Error: autorest/azure: Service returned an error. Status=404 Code="" Message="No HTTP resource was found that matches the request URI 'h_ttps://management.azure.com/subscriptions/---/resourceGroups/rg-test-001/providers/Microsoft.Resources/deployments/kv_test/providers/microsoft.insights/diagnosticSettings/kv-diagnostics?api-version=2017-05-01-preview'."
The following is my code for the diagnostic settings resource in Terraform:
resource "azurerm_monitor_diagnostic_setting" "kv-diag" {
count = length(var.kv_name)
name = "kv-diagnostics"
target_resource_id = azurerm_template_deployment.kv[count.index].id
log_analytics_workspace_id = azurerm_log_analytics_workspace.log.id
log {
...
}
}
metric {
...
}
}
}
Where azurerm_log_analytics_workspace.logs.id
already exists and the variable var.kv_name is a list of names (so far there is only 1 name in the list). The output of "terraform plan" shows the target_resource_id = (known after apply), which makes sense as in the ideal situation the key vault hasn't been deployed yet, so it doesn't have an id.
Why is Azure throwing this error? Where am I going wrong?