azureazure-functionsterraformazure-application-insights

Configure Application Insights for Azure Function App (Terraform)


using below snippet I can create application insights and function_app but I am not sure how to configure function-app to use the created application insights.

module "resource_group" {
  source      = "git::ssh://address"
  environment = "${var.environment}"
  application = "${var.application}"
  app_version = "${var.app_version}"
}

module "function_app" {
  source                            = "git::ssh://address"
  environment                       = "${var.environment}"
  application                       = "${var.application}"
  resource_group_name               = "${module.resource_group.name}"
  app_version                       = "${var.app_version}"
  app_service_plan_id               = "${module.app_service_plan.id}"
  storage_primary_connection_string = "${module.storage_account.primary_connection_string}"
}

resource "azurerm_application_insights" "application_insights" {
  name                = "${var.environment}-${var.application}-appInsights"
  location            = "${module.resource_group.location}"
  resource_group_name = "${module.resource_group.name}"
  application_type    = "Web"
}

but When I visit azure > function_app > Monitor, it still shows to configure application_insights.

configure application insights

any suggestion?


Solution

  • See sample here: https://www.olivercoding.com/2018-06-24-terraform/. You need to add the APPINSIGHTS_INSTRUMENTATIONKEY to the app_settings of your function app.

    Update - 10/09/2023

    Adding application_insights_connection_string and application_insights_key inside the site_config block is the new way. Terraform automatically sets up the app_settings map.

    Credit: Rajesh Swarnkar