8
votes

We have an Azure DevOps release pipeline, which sets up all of our Azure resources in a location. I can create everything successfully with ARM templates, but I'm struggling to link the App Service with the App Insights resource.

If I were doing it manually, I'd click a "Turn on site extension" button in the AppInsights blade of the App Service (under the heading "Enable Application Insights through site extension without redeploying your code").

I've tried adding an "Azure App Service Manage" step to my release pipeline, set to install the "Application Insights extension for Azure App Service" extension:

Screenshot of Release Pipeline Step for Installing AppInsights

In addition, I've added an "Azure App Service Manage" step to my release pipeline, set to "Enable Continuous Monitoring":

Screenshot of Release Pipeline Step for Enabling Continuous Monitoring

But the result is still that AppInsights is connected, but the extension is not installed:

Screenshot of Azure Portal showing extension is not turned on

Is there any way I can do this automatically? Either via an ARM template, a PowerShell script, or something else?

Edit: In the "Extensions" blade, I can see "Application Insights extension for Azure App Service" (v2.6.5) and "ASP.NET Core Logging Extensions" (v2.2.0), but I'm still asked to "Turn on site extension" in the "Aplication Insights" blade.

4

4 Answers

11
votes

In an ARM-template you can do:

{
  "type": "Microsoft.Web/sites",
  "apiVersion": "2018-02-01",
  "name": "[variables('web_app_service_name')]",
  "location": "[resourceGroup().location]",
  "dependsOn": [
    "[resourceId('Microsoft.Web/serverfarms', variables('plan_name'))]",
    "[resourceId('Microsoft.Insights/components', variables('app_insights_name'))]"
  ],
  "kind": "app",
  "properties": {
    "siteConfig": {
      "appSettings": [
        {
          "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
          "value": "[reference(variables('app_insights_name'), '2015-05-01').InstrumentationKey]"
        },
        {
          "name": "ApplicationInsightsAgent_EXTENSION_VERSION",
          "value": "~2"
        }
      ]
    }
  }
}

Refer to documentation at https://docs.microsoft.com/en-us/azure/azure-monitor/app/azure-web-apps#automate-monitoring

2
votes

i think you would need to do something like that:

    {
        "apiVersion": "2015-08-01",
        "name": "[parameters('webSiteName')]",
        "type": "Microsoft.Web/sites",
        "location": "[resourceGroup().location]",
        "tags": {
            "[concat('hidden-related:', resourceGroup().id, '/providers/Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]": "Resource",
            "displayName": "Website"
        },
        "dependsOn": [
            "[resourceId('Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]",
            "[resourceId('microsoft.insights/components/', parameters('appInsightsName'))]"
        ],
        "properties": {
            "name": "[parameters('webSiteName')]",
            "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('hostingPlanName'))]"
        },
        "resources": [
            {
                "apiVersion": "2015-08-01",
                "name": "appsettings",
                "type": "config",
                "dependsOn": [
                    "[resourceId('Microsoft.Web/Sites', parameters('webSiteName'))]",
                    "Microsoft.ApplicationInsights.AzureWebSites"
                ],
                "properties": {
                    "APPINSIGHTS_INSTRUMENTATIONKEY": "[reference(concat('microsoft.insights/components/', parameters('appInsightsName'))).InstrumentationKey]"
                }
            },
            {
                // this bit installs application insights extension
                "apiVersion": "2015-08-01",
                "name": "Microsoft.ApplicationInsights.AzureWebSites",
                "type": "siteextensions",
                "dependsOn": [
                    "[resourceId('Microsoft.Web/Sites', parameters('webSiteName'))]"
                ],
                "properties": {
                }
            }
        ]
    }

I've never actually tried this, but looks correct, link to the example I've found: https://github.com/tomasr/webapp-appinsights/blob/master/WebSite.json

2
votes

Make sure that your app settings key is APPINSIGHTS_INSTRUMENTATIONKEY and not ApplicationInsights:InstrumentationKey. Somewhere in the MS docs it gives the impression that you can use either. In fact that's not the case, in Azure you need to use the former otherwise Application Insights won't be enabled for server side insights.

1
votes

In order for the Azure Portal to show an active integration with Application Insights, you need to set three app settings.

https://docs.microsoft.com/en-us/azure/azure-monitor/app/azure-web-apps?tabs=net#automate-the-creation-of-an-application-insights-resource-and-link-to-your-newly-created-app-service

{
    "resources": [
        {
            "name": "[parameters('name')]",
            "type": "Microsoft.Web/sites",
            "properties": {
                "siteConfig": {
                    "appSettings": [
                        {
                            "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
                            "value": "[reference('microsoft.insights/components/AppMonitoredSite', '2015-05-01').InstrumentationKey]"
                        },
                        {
                            "name": "APPLICATIONINSIGHTS_CONNECTION_STRING",
                            "value": "[reference('microsoft.insights/components/AppMonitoredSite', '2015-05-01').ConnectionString]"
                        },
                        {
                            "name": "ApplicationInsightsAgent_EXTENSION_VERSION",
                            "value": "~2"
                        }
                    ]
                },
                "name": "[parameters('name')]",
                "serverFarmId": "[concat('/subscriptions/', parameters('subscriptionId'),'/resourcegroups/', parameters('serverFarmResourceGroup'), '/providers/Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]",
                "hostingEnvironment": "[parameters('hostingEnvironment')]"
            },
            "dependsOn": [
                "[concat('Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]",
                "microsoft.insights/components/AppMonitoredSite"
            ],
            "apiVersion": "2016-03-01",
            "location": "[parameters('location')]"
        },
        {
            "apiVersion": "2016-09-01",
            "name": "[parameters('hostingPlanName')]",
            "type": "Microsoft.Web/serverfarms",
            "location": "[parameters('location')]",
            "properties": {
                "name": "[parameters('hostingPlanName')]",
                "workerSizeId": "[parameters('workerSize')]",
                "numberOfWorkers": "1",
                "hostingEnvironment": "[parameters('hostingEnvironment')]"
            },
            "sku": {
                "Tier": "[parameters('sku')]",
                "Name": "[parameters('skuCode')]"
            }
        },
        {
            "apiVersion": "2015-05-01",
            "name": "AppMonitoredSite",
            "type": "microsoft.insights/components",
            "location": "West US 2",
            "properties": {
                "ApplicationId": "[parameters('name')]",
                "Request_Source": "IbizaWebAppExtensionCreate"
            }
        }
    ],
    "parameters": {
        "name": {
            "type": "string"
        },
        "hostingPlanName": {
            "type": "string"
        },
        "hostingEnvironment": {
            "type": "string"
        },
        "location": {
            "type": "string"
        },
        "sku": {
            "type": "string"
        },
        "skuCode": {
            "type": "string"
        },
        "workerSize": {
            "type": "string"
        },
        "serverFarmResourceGroup": {
            "type": "string"
        },
        "subscriptionId": {
            "type": "string"
        }
    },
    "$schema": "https://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0"
}

See also my other answer on this: Azure Cli How to enable Application Insights for webapp