Terraform setup to collect container logs in analytics workspace

Erik Heeren 60 Reputation points
2025-11-27T14:26:43.04+00:00

Hello,

I've got a small PoC setup involving:

  • An AKS cluster
  • A Log Analytics workspace
  • A data collection rule linking the AKS cluster to the log analytics workspace

When I deploy a demo namespace (the contoso pet shop) the application works, but none of my container logs show up in the analytics workspace. Once I set up logging through the portal logs start showing up. I'v made sure to verify that my terraform code matches the resources that get generated when I do it the clickops way.

What does the portal do in the background that my terraform code should be reproducing?

Here's the relevant parts of the terraform code:

resource "azurerm_resource_group" "poc_rg" {
  name     = var.resource_group_name
  location = var.aks_cluster_location
}

resource "azurerm_kubernetes_cluster" "poc_aks" {
  name                = var.aks_cluster_name
  location            = azurerm_resource_group.poc_rg.location
  resource_group_name = azurerm_resource_group.poc_rg.name
  dns_prefix          = var.aks_cluster_dnsprefix
  default_node_pool {
    name       = "default"
    node_count = var.aks_cluster_node_pool_count
    vm_size    = var.aks_cluster_node_pool_vmsize
  }
  identity {
    type = "SystemAssigned"
  }
  azure_active_directory_role_based_access_control {
    admin_group_object_ids = var.aks_cluster_admin_groups
  }
}

resource "azurerm_log_analytics_workspace" "aks_logs" {
  name                = "aks-logs"
  location            = var.location
  resource_group_name = azurerm_resource_group.poc_rg.name
  sku                 = "PerGB2018"
  retention_in_days   = 30
}

resource "azurerm_monitor_data_collection_rule" "aks_logs" {
  depends_on          = [azurerm_kubernetes_cluster.poc_aks]
  kind                = "Linux"
  location            = var.location
  name                = "aks-logs-collection-rule"
  resource_group_name = azurerm_resource_group.poc_rg.name
  data_flow {
    destinations = ["ciworkspace"]
    streams      = ["Microsoft-ContainerLog", "Microsoft-ContainerLogV2", "Microsoft-KubeEvents", "Microsoft-KubePodInventory"]
  }
  data_sources {
    extension {
      extension_json = jsonencode({
        dataCollectionSettings = {
          enableContainerLogV2   = true
          interval               = "1m"
          namespaceFilteringMode = "Off"
        }
      })
      extension_name = "ContainerInsights"
      name           = "ContainerInsightsExtension"
      streams        = ["Microsoft-ContainerLog", "Microsoft-ContainerLogV2", "Microsoft-KubeEvents", "Microsoft-KubePodInventory"]
    }
  }
  destinations {
    log_analytics {
      name                  = "ciworkspace"
      workspace_resource_id = azurerm_log_analytics_workspace.aks_logs.id
    }
  }
}

resource "azurerm_monitor_data_collection_rule_association" "aks_logs" {
  name                    = "collect-kubernetes-logs"
  target_resource_id      = azurerm_kubernetes_cluster.poc_aks.id
  data_collection_rule_id = azurerm_monitor_data_collection_rule.aks_logs.id
}

One extra annoying detail: if I delete all resources in my subscription and then redeploy everything, I still get to see logs from the previous deployment. Does "delete" mean something else on azure than it does in the rest of the world?

Azure Synapse Analytics
Azure Synapse Analytics
An Azure analytics service that brings together data integration, enterprise data warehousing, and big data analytics. Previously known as Azure SQL Data Warehouse.
{count} votes

1 answer

Sort by: Most helpful
  1. Bharath Y P 2,485 Reputation points Microsoft External Staff Moderator
    2025-11-27T17:14:09.2933333+00:00

    Hello Erik Heeren,

    We understand that you configured an Azure AKS Cluster, Log Analytics Workspace (LAW), and Data Collection Rule (DCR) using Terraform. The application logs were not appearing in the LAW until the user manually clicked the "enable monitoring" option in the Azure portal.

    The primary reason for the failure was the lack of the Data Collection Rule Association (DCRA) and the missing Azure Monitor Agent (AMA) extension deployment.

    In the Azure portal's "enable monitoring" button performs several implicit configuration steps that your Terraform code must define explicitly. While you've likely created the DCR and the Log Analytics Workspace (LAW), the vital steps to enable data flow are usually the Agent Deployment and the DCR Association.

    • Logs won't flow until the Azure Monitor Agent (AMA) is running on your cluster nodes. The portal's "enable monitoring" feature installs the Container Insights extension (Microsoft.AzureMonitor.Containers), which deploys the AMA as a DaemonSet to collect and send logs/metrics.
    • The DCR defines what data to collect and where to send it. However, the cluster needs an explicit instruction telling its onboarded AMA agent which DCR it should follow. This crucial link is the Data Collection Rule Association (DCRA).
    • The AMA agent uses a Managed Identity to authenticate and send data. This identity requires permission to write to the Log Analytics workspace. While the oms_agent configuration often handles this implicitly, an explicit grant ensures no permission failures.

    Could you please help us with the below details:

    • Have you verified that the managed identity for your AKS has been granted necessary permissions (like Log Analytics Contributor) to the Log Analytics workspace?
    • Can you confirm whether the diagnostic settings in your Terraform match the settings that get created when you configure them via the portal?

    When you delete AKS, DCRs, and even the cluster, the workspace (or its tables) remains unless you explicitly delete it. Container logs (ContainerLogV2) are stored in the workspace, not in the cluster. By default, Log Analytics keeps data for 30 days (or longer if you configured retention). So even if the cluster is gone, the logs remain until retention expires or you purge them. Removing associations stops new ingestion, but historical data stays in the workspace. Certain Azure resources (e.g., storage accounts, workspaces) have a “soft delete” or delayed purge for recovery purposes. Deleting the resource doesn’t instantly erase its data.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.