Search code examples
azureterraformazure-application-insightsazure-web-app-service

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?


Solution

  • 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