7

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)

3 Answers 3

12

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:

enter image description here

In terraform:

app_settings = {
    "APPINSIGHTS_INSTRUMENTATIONKEY" = "${azurerm_application_insights.example.instrumentation_key}"
  }

The result shows the logs for the function "functioninsighttest":

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

This appears to work for .NET Framework as well, as you might expect.
Here is the link to the Terraform documentation on how to create the azurerm_application_insights resource: terraform.io/docs/providers/azurerm/r/application_insights.html
One thing to be cognizant of when using this method to add APP_SETTINGs to an App Service is that Terraform will clobber any existing APP_SETTING and replace it with whatever it's configured with. In some cases this may break your CI/CD workflow. Thus I would recommend if you want to access this value to use something like Key Vault + Key Vault References; that allows Terraform to create the individual secrets (not whole APP_SETTINGs config) and therefore not erase existing config that could break your deployment.
3

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"
  }

1 Comment

What is the 1st way? And do we need to provide so much configuration? "APPINSIGHTS_INSTRUMENTATIONKEY" , "APPLICATIONINSIGHTS_CONNECTION_STRING" these two alone will not configure the connection? @Maciek
3

Documentation:

As of terrform 3.75 (Sept.2023), the documentation mentions:

For application insight related settings, please use application_insights_connection_string and application_insights_key. Terraform will assign the value to the key APPINSIGHTS_INSTRUMENTATIONKEY and APPLICATIONINSIGHTS_CONNECTION_STRING in app setting.

Solution:

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}"
  }
}

Update:

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"
  }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.