Search code examples
azureterraformazure-functionsazure-application-insightsterraform-provider-azure

Terraform deployment for 'Workspace based Application Insight' on Azure


I have been trying to figure out a way to prepare a terraform template for my app service/az function where I can connect it to application Insight while creating them through Terraform. Well the it worked, BUT the application Insight shows

Migrate this resource to Workspace-based Application Insights to gain support for all of the capabilities of Log Analytics, including Customer-Managed Keys and Commitment Tiers. Click here to learn more and migrate in a few clicks.

How do I acheive it from terraform? As from the documentation page of terraform there is no mention of such setup. Appreciate you help on this. Here is the terraform code for az-function

    resource "azurerm_linux_function_app" "t_funcapp" {
  name                = "t-function-app"
  location            = local.resource_location
  resource_group_name = local.resource_group_name
  service_plan_id     = azurerm_service_plan.t_app_service_plan.id

  storage_account_name       = azurerm_storage_account.t_funcstorage.name
  storage_account_access_key = azurerm_storage_account.t_funcstorage.primary_access_key

  site_config {
    application_stack {
      java_version = "11"
    }
    remote_debugging_enabled = false
    ftps_state = "AllAllowed"
  }
  app_settings = {
    APPINSIGHTS_INSTRUMENTATIONKEY = "${azurerm_application_insights.t_appinsights.instrumentation_key}"
  }
  depends_on = [
    azurerm_resource_group.t_rg,
    azurerm_service_plan.t_app_service_plan,
    azurerm_storage_account.t_funcstorage,
    azurerm_application_insights.t_appinsights
  ]
}

Here is the terraform code for app insight

resource "azurerm_application_insights" "t_appinsights" {
  name                = "t-appinsights"
  location            = local.resource_location
  resource_group_name = local.resource_group_name
  application_type    = "web"
  depends_on = [
    azurerm_log_analytics_workspace.t_workspace
  ]
}

output "instrumentation_key" {
  value = azurerm_application_insights.t_appinsights.instrumentation_key
}

output "app_id" {
  value = azurerm_application_insights.t_appinsights.app_id
}

Solution

  • You must create a Log Analytics Workspace and add it to your Application Insights.

    For example

    resource "azurerm_log_analytics_workspace" "example" {
      name                = "workspace-test"
      location            = local.resource_location
      resource_group_name = local.resource_group_name
      sku                 = "PerGB2018"
      retention_in_days   = 30
    }
    
    resource "azurerm_application_insights" "t_appinsights" {
      name                = "t-appinsights"
      location            = local.resource_location
      resource_group_name = local.resource_group_name
      workspace_id        = azurerm_log_analytics_workspace.example.id
      application_type    = "web"
    }
    
    output "instrumentation_key" {
      value = azurerm_application_insights.t_appinsights.instrumentation_key
    }
    
    output "app_id" {
      value = azurerm_application_insights.t_appinsights.app_id
    }
    

    Hope this helps!