Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to associate an Azure app service with an application insights resource (new or existing) using terraform?

I looked at the documentation of both azurerm_app_service and azurerm_application_insights and I just do not see a way to tie them.

Yet on the App Service page in the portal there is a link to Application Insights, currently grayed out: enter image description here

So, how do I enable it with terraform?

like image 491
mark Avatar asked Feb 11 '20 18:02

mark


2 Answers

You need numerous app settings to get this to work properly as intended. The ones I had to add to get it all working were:

  • "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"
like image 120
JapethMarvel Avatar answered Sep 20 '22 01:09

JapethMarvel


It seems that enabling application insights using Terraform is not working yet currently. There is a Feature Request: Attach azurerm_application_insights to a azurerm_app_service in Github.

It might be possible to set a tag on the azurerm_application_insights resource,

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

Usually, if you need to enable application insights component in your app service, you need to add APPINSIGHTS_* environment variables to the app_settings of your web app.

For example,

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

See argument reference even it's about Azure function.

enter image description here ref: https://www.olivercoding.com/2018-06-24-terraform/

https://github.com/terraform-providers/terraform-provider-azurerm/issues/2457

like image 38
Nancy Xiong Avatar answered Sep 19 '22 01:09

Nancy Xiong