Using Terraform, how do you enable Application Insights agent-based monitoring for
Azure App Service (.NET Core 2.x) Azure Function App (.NET Core 2.x)
To enable the Application Insights agent-based monitoring for Azure App Service (.NET Core 2.x) Azure Function App (.NET Core 2.x), you just need to add the environment variable for application insight in the app setting like below:
In Azure portal:
In terraform:
app_settings = {
"APPINSIGHTS_INSTRUMENTATIONKEY" = "${azurerm_application_insights.example.instrumentation_key}"
}
The result shows the logs for the function "functioninsighttest":
Apparently now in 2021 there are two ways to this.
The second one is:
app_settings = {
"MSDEPLOY_RENAME_LOCKED_FILES" = "1"
"WEBSITE_HEALTHCHECK_MAXPINGFAILURES" = "10"
"ASPNETCORE_ENVIRONMENT" = "Development"
"APPINSIGHTS_INSTRUMENTATIONKEY" = azurerm_application_insights.appi1.instrumentation_key
"APPLICATIONINSIGHTS_CONNECTION_STRING" = azurerm_application_insights.appi1.connection_string
"APPINSIGHTS_PROFILERFEATURE_VERSION" = "1.0.0"
"APPINSIGHTS_SNAPSHOTFEATURE_VERSION" = "1.0.0"
"ApplicationInsightsAgent_EXTENSION_VERSION" = "~2"
"DiagnosticServices_EXTENSION_VERSION" = "~3"
"InstrumentationEngine_EXTENSION_VERSION" = "disabled"
"SnapshotDebugger_EXTENSION_VERSION" = "disabled"
"XDT_MicrosoftApplicationInsights_BaseExtensions" = "disabled"
"XDT_MicrosoftApplicationInsights_Java" = "1"
"XDT_MicrosoftApplicationInsights_Mode" = "recommended"
"XDT_MicrosoftApplicationInsights_NodeJS" = "1"
"XDT_MicrosoftApplicationInsights_PreemptSdk" = "disabled"
}
As of terrform 3.75 (Sept.2023), the documentation mentions:
For application insight related settings, please use
application_insights_connection_stringandapplication_insights_key. Terraform will assign the value to the keyAPPINSIGHTS_INSTRUMENTATIONKEYandAPPLICATIONINSIGHTS_CONNECTION_STRINGin app setting.
Hence adding, application_insights_connection_string and application_insights_key inside the site_config block worked for me.
# App Insight is created with Workspace Mode
resource "azurerm_log_analytics_workspace" "law_fxapp" {
name = "appinsight-law"
resource_group_name = ".."
location = ".."
sku = "PerGB2018"
retention_in_days = 365
}
resource "azurerm_application_insights" "app_insight_fxapp" {
name = "appinsightfxapp"
resource_group_name = ".."
location = ".."
workspace_id = azurerm_log_analytics_workspace.law_fxapp.id
application_type = "web"
}
resource "azurerm_linux_function_app" "linux_function_app" {
name = "linux-fx-app"
resource_group_name = ".."
location = ".."
storage_account_name = ".."
storage_account_access_key = ".."
service_plan_id = ".."
site_config {
..
application_stack {..}
application_insights_connection_string = "${azurerm_application_insights.app_insight_fxapp.connection_string}"
application_insights_key = "${azurerm_application_insights.app_insight_fxapp.instrumentation_key}"
}
}
I found providing the values to the keys in app_settings works as well:
resource "azurerm_linux_function_app" "linux_function_app" {
name = "linux-fx-app"
// other properties ...
# Application Insights settings using app_settings block
app_settings = {
"APPLICATIONINSIGHTS_CONNECTION_STRING" = azurerm_application_insights.app_insight_fxapp.connection_string
"APPINSIGHTS_INSTRUMENTATIONKEY" = azurerm_application_insights.app_insight_fxapp.instrumentation_key
"ApplicationInsightsAgent_EXTENSION_VERSION" = "~3"
}
}