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:
Is instrumentation controlled by these config keys, which I have to manually set?
2 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
}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/ name>": "Resource" }
}or
tags = { "hidden-link:/subscriptions/${data.azurerm_subscription.current.subscription_id}/resourceGroups/${azurerm_resource_group.example.name}/providers/ = "Resource" }and check if it is enabled.
1I just had this same problem myself, a linux app plan with two apps
- linux_web_app
- linux_function_app
the function_app was really straight forward, it could be assosiated and enabled via the site_config block of the function app resource
site_config { always_on = false application_insights_connection_string = azurerm_application_insights.<your_app_insights_resource_name>.connection_string application_insights_key = azurerm_application_insights.<your_app_insights_resource_name>.instrumentation_key }However the web_app was a bit trickier: configured through the app_settings argument, started with the same two
app_settings = { APPINSIGHTS_INSTRUMENTATIONKEY = "${azurerm_application_insights.<your_app_insights_resource_name>.instrumentation_key}" APPLICATIONINSIGHTS_CONNECTION_STRING = "${azurerm_application_insights.<your_app_insights_resource_name>.connection_string}"
}this seemed to link or associate the web_app with app_insights, but frustratingly, NOT enable it!
after spending way to long trying other suggestions found on the internet, and not getting anywher, i asked our friendly neighbourhood AI if it had any suggestions, and it did...
adding the following config to the app_settings
APPINSIGHTS_PROFILERFEATURE_VERSION = "1.0.0"
APPINSIGHTS_SNAPSHOTFEATURE_VERSION = "1.0.0"
ApplicationInsightsAgent_EXTENSION_VERSION = "~2"
DiagnosticServices_EXTENSION_VERSION = "~3"
InstrumentationEngine_EXTENSION_VERSION = "~2"
SnapshotDebugger_EXTENSION_VERSION = "1.0.15"
XDT_MicrosoftApplicationInsights_BaseExtensions = "disabled"
XDT_MicrosoftApplicationInsights_Mode = "recommended"seemed to do the trick in my case, hope this helps 😉