How do I configure app insights instrumentation for app service via terraform?

Viewed 63

How do I configure App Insights instrumentation for an app service via Terraform? Is it all via app_settings, or is there a resource I am missing?

I create app insights resource:

resource "azurerm_application_insights" "app1" {
  for_each = local.all_envs
  application_type    = "web"
  location            = azurerm_resource_group.rg-webapps.location
  name                = "appi-app1-${each.value}"
  resource_group_name = azurerm_resource_group.rg-webapps.name
  retention_in_days   = 30
  sampling_percentage = 0
  workspace_id = azurerm_log_analytics_workspace.log-analytics-workspace[each.value].id
}

I tie it to my app service:

resource "azurerm_windows_web_app" "app1" {
  name                = "app1"
  location            = azurerm_resource_group.rg-webapps.location
  resource_group_name = azurerm_resource_group.rg-webapps.name
  ...

  app_settings = {
    APPLICATIONINSIGHTS_ROLE_NAME   = "role1"
    APPINSIGHTS_INSTRUMENTATIONKEY = azurerm_application_insights.app1["dev"].instrumentation_key
    APPLICATIONINSIGHTS_CONNECTION_STRING = azurerm_application_insights.app1["dev"].connection_string
  }
  ...

}

But it says application insights is not fully enabled:

enter image description here

Is instrumentation controlled by these config keys, which I have to manually set?

enter image description here

1 Answers

Tried to check with appsettings for instrumentation key and connection string in my case and it was not enabled in portal.

app_settings = {

    "APPINSIGHTS_INSTRUMENTATIONKEY"                  = azurerm_application_insights.<app>.instrumentation_key
    "APPLICATIONINSIGHTS_CONNECTION_STRING"           = azurerm_application_insights.<app>.connection_string
}

enter image description here

Also include ApplicationInsightsAgent_EXTENSION_VERSION in the app settings .

app_settings = {

    "APPINSIGHTS_INSTRUMENTATIONKEY"                  = azurerm_application_insights.<app>.instrumentation_key
    "APPLICATIONINSIGHTS_CONNECTION_STRING"           = azurerm_application_insights.<app>.connection_string
     "APPINSIGHTS_PORTALINFO"                         =         "ASP.NET"
      "APPINSIGHTS_PROFILERFEATURE_VERSION"           =        "1.0.0"
    "ApplicationInsightsAgent_EXTENSION_VERSION"      =          "~2"
}

For working properly, your app may require additional settings from below: check what works for your app.

  • "APPINSIGHTS_INSTRUMENTATIONKEY"
  • "APPINSIGHTS_PROFILERFEATURE_VERSION"
  • "APPINSIGHTS_SNAPSHOTFEATURE_VERSION"
  • "APPLICATIONINSIGHTS_CONNECTION_STRING"
  • "ApplicationInsightsAgent_EXTENSION_VERSION"
  • "DiagnosticServices_EXTENSION_VERSION"
  • "InstrumentationEngine_EXTENSION_VERSION"
  • "SnapshotDebugger_EXTENSION_VERSION"
  • "XDT_MicrosoftApplicationInsights_BaseExtensions"
  • "XDT_MicrosoftApplicationInsights_Mode"

And try to set a tag on the azurerm_application_insights as said by nancy in SO reference

resource "azurerm_application_insights" "webapp-ka-repo" {
...
  tags {
    "hidden-link:/subscriptions/<subscription id>/resourceGroups/<rg name>/providers/Microsoft.Web/sites/<site name>": "Resource"
  }
}

or

tags = {
    "hidden-link:/subscriptions/${data.azurerm_subscription.current.subscription_id}/resourceGroups/${azurerm_resource_group.example.name}/providers/Microsoft.Web/sites/<sitename>” = "Resource"
  }

and check if it is enabled.

enter image description here

Related