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

4

11

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?

Acetabulum answered 11/2, 2020 at 18:52 Comment(0)
R
6

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

Rubino answered 12/2, 2020 at 0:23 Comment(1)
We use Asp.Net Core and according to learn.microsoft.com/en-us/azure/azure-monitor/app/asp-net-core the key should be ApplicationInsights__InstrumentationKey.Acetabulum
H
10

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"
Hitoshi answered 8/5, 2020 at 18:10 Comment(2)
This worked for App Service (not just function). Thanks!Jaquesdalcroze
This worked for me in App Service as well. Look up each individual setting to see what the possible values are. An alternative is to first configure Application Insights manually in the portal -> View your Configuration tab to see all the app settings which Azure automatically set -> copy these into terraform. When I did this, there were some additional settings I included.Divalent
R
6

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

Rubino answered 12/2, 2020 at 0:23 Comment(1)
We use Asp.Net Core and according to learn.microsoft.com/en-us/azure/azure-monitor/app/asp-net-core the key should be ApplicationInsights__InstrumentationKey.Acetabulum
S
2

There is one key variable allow for particular language and OS version:

  {
"name": "ApplicationInsightsAgent_EXTENSION_VERSION",
"value": "~3",
"slotSetting": false  }
  1. Value=~2 is for windows APP
  2. Value=~3 is for Linux APP

Reference Link: https://learn.microsoft.com/en-us/azure/azure-monitor/app/azure-web-apps-nodejs?tabs=windows

Shrieve answered 3/8, 2023 at 12:8 Comment(0)
S
0
resource "azurerm_service_plan" "asp" {
  name                = provide_name
  resource_group_name = azurerm_resource_group.rg.name
  location            = azurerm_resource_group.rg.location
  os_type             = "Windows"
  sku_name            = "B1"
}

resource "azurerm_windows_web_app" "app" {
  name                = provideName
  resource_group_name = azurerm_resource_group.rg.name
  location            = azurerm_resource_group.rg.location
  service_plan_id     = azurerm_service_plan.asp.id

  site_config {
    application_stack {
      current_stack  = "dotnet"
      dotnet_version = "v6.0"
    }
    # ip_restriction {
    #   action     = "Deny"
    #   ip_address = "0.0.0.0/0"
    #   name       = "Deny_AllTraffic"
    #   priority   = 200
    # }
  }

  app_settings = {
    "APPINSIGHTS_INSTRUMENTATIONKEY"        = azurerm_application_insights.appi.instrumentation_key
    "APPLICATIONINSIGHTS_CONNECTION_STRING" = azurerm_application_insights.appi.connection_string
  }

  logs {
    detailed_error_messages = true
    http_logs {
      azure_blob_storage {
        retention_in_days = 7
        sas_url           = "https://${azurerm_storage_account.st.name}.blob.core.windows.net/${azurerm_storage_container.st_con.name}${data.azurerm_storage_account_blob_container_sas.stg_sas.sas}"
      }
    }
  }
}
Sinew answered 10/7 at 16:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.