Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
105 views
in Technique[技术] by (71.8m points)

Integrate an already created Azure Application Insight with an API App in App Service using Azure Resource Manager

I am creating an app insight in a different resource group using a Terraform Script. I want to associate the previously created App Insight in when i create a web app or an API app. I am passing the Intrumentation key of the previuosly created app insight in the web app however they are not getting linked. When i try to create the app insight from within the same ARM template where the Web App is created it works fine.

Sample ARM that works fine:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "webSiteName": {
            "type": "string",
            "minLength": 1,
            "defaultValue": "TestWebsiteWithAppInsight",
            "metadata": {
                "description": "Describes the name of the new website"
            }
        },
        "appInsightsName": {
            "type": "string",
            "minLength": 1,
            "defaultValue": "TestAppInsight",
            "metadata": {
                "description": "Describes the name of the Application Insights resource"
            }
        },
        "hostingPlanName": {
            "type": "string",
            "minLength": 1,
            "defaultValue": "sudipta-ase-plan1"
        },
        "skuName": {
            "type": "string",
            "defaultValue": "F1",
            "allowedValues": [
                "F1",
                "D1",
                "B1",
                "B2",
                "B3",
                "S1",
                "S2",
                "S3",
                "P1",
                "P2",
                "P3",
                "P4"
            ],
            "metadata": {
                "description": "Describes plan's pricing tier and capacity. Check details at https://azure.microsoft.com/en-us/pricing/details/app-service/"
            }
        },
        "skuCapacity": {
            "type": "int",
            "defaultValue": 4,
            "minValue": 1,
            "metadata": {
                "description": "Describes plan's instance count"
            }
        }
    },
    "variables": {
    },
    "resources": [
        {
            "apiVersion": "2015-08-01",
            "name": "[parameters('hostingPlanName')]",
            "type": "Microsoft.Web/serverfarms",
            "location": "[resourceGroup().location]",
            "tags": {
                "displayName": "HostingPlan"
            },
            "sku": {
                "name": "[parameters('skuName')]",
                "capacity": "[parameters('skuCapacity')]"
            },
            "properties": {
                "name": "[parameters('hostingPlanName')]"
            }
        },
        {
            "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'))]"
                    ],
                    "properties": {
                        "APPINSIGHTS_INSTRUMENTATIONKEY": "[reference(concat('microsoft.insights/components/', parameters('appInsightsName'))).InstrumentationKey]"
                    }
                },
                {
                    "apiVersion": "2015-08-01",
                    "name": "Microsoft.ApplicationInsights.AzureWebSites",
                    "type": "siteextensions",
                    "dependsOn": [
                        "[resourceId('Microsoft.Web/Sites', parameters('webSiteName'))]"
                    ],
                    "properties": {
                    }
                }
            ]
        },
        {
            "apiVersion": "2014-04-01",
            "name": "[parameters('appInsightsName')]",
            "type": "Microsoft.Insights/components",
            "location": "East US",
            "tags": {
                "[concat('hidden-link:', resourceGroup().id, '/providers/Microsoft.Web/sites/', parameters('webSiteName'))]": "Resource",
                "displayName": "AppInsightsComponent"
            },
            "properties": {
                "applicationId": "[parameters('appInsightsName')]"
            }
        }
    ]
}

Sample ARM that does not work:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "webSiteName": {
            "type": "string",
            "minLength": 1,
            "defaultValue": "TestWebsiteWithAppInsight",
            "metadata": {
                "description": "Describes the name of the new website"
            }
        },
        "appInsightsName": {
            "type": "string",
            "minLength": 1,
            "defaultValue": "test123",
            "metadata": {
                "description": "Describes the name of the Application Insights resource"
            }
        },
        "hostingPlanName": {
            "type": "string",
            "minLength": 1,
            "defaultValue": "sudipta-ase-plan1"
        },
        "skuName": {
            "type": "string",
            "defaultValue": "F1",
            "allowedValues": [
                "F1",
                "D1",
                "B1",
                "B2",
                "B3",
                "S1",
                "S2",
                "S3",
                "P1",
                "P2",
                "P3",
                "P4"
            ],
            "metadata": {
                "description": "Describes plan's pricing tier and capacity. Check details at https://azure.microsoft.com/en-us/pricing/details/app-service/"
            }
        },
        "skuCapacity": {
            "type": "int",
            "defaultValue": 4,
            "minValue": 1,
            "metadata": {
                "description": "Describes plan's instance count"
            }
        }
    },
    "variables": {
    },
    "resources": [
        {
            "apiVersion": "2015-08-01",
            "name": "[parameters('hostingPlanName')]",
            "type": "Microsoft.Web/serverfarms",
            "location": "[resourceGroup().location]",
            "tags": {
                "displayName": "HostingPlan"
            },
            "sku": {
                "name": "[parameters('skuName')]",
                "capacity": "[parameters('skuCapacity')]"
            },
            "properties": {
                "name": "[parameters('hostingPlanName')]"
            }
        },
        {
            "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'))]"
            ],
            "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'))]"
                    ],
                    "properties": {
                        "APPINSIGHTS_INSTRUMENTATIONKEY": "['99f330eb-ae79-4070-b8b6-e50d01f6e391123134434sdfdadaa']"
                    }
                },
                {
                    "apiVersion": "2015-08-01",
                    "name": "Microsoft.ApplicationInsights.AzureWebSites",
                    "type": "siteextensions",
                    "dependsOn": [
                        "[resourceId('Microsoft.Web/Sites', parameters('webSiteName'))]"
                    ],
                    "properties": {
                    }
                }
            ]
        }
    ]
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Please change the code from "APPINSIGHTS_INSTRUMENTATIONKEY": "['99f330eb-ae79-4070-b8b6-e50d01f6e391123134434sdfdadaa']" to "APPINSIGHTS_INSTRUMENTATIONKEY": "99f330eb-ae79-4070-b8b6-e50d01f6e391123134434sdfdadaa".

For string value should be no brackets: [ and ]. Brackets [ and ] are used for ARM template functions.

You add functions in your templates by enclosing them within brackets: [ and ], respectively.

Update:

I am passing the Intrumentation key of the previuosly created app insight in the web app however they are not getting linked.

Based on my test, if want to link to Azure webapp, you also need to add the following code. It also works with an existing appInsightsName.

 {
            "apiVersion": "2014-04-01",
            "name": "[parameters('appInsightsName')]",
            "type": "Microsoft.Insights/components",
            "location": "East US",
            "tags": {
                "[concat('hidden-link:', resourceGroup().id, '/providers/Microsoft.Web/sites/', parameters('webSiteName'))]": "Resource",
                "displayName": "AppInsightsComponent"
            },
            "properties": {
                "applicationId": "[parameters('appInsightsName')]"
            }
        }

ARM template I used

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "serverFarmName": {
      "type": "string",
      "defaultValue": "tomfreePlan"
    },
    "serverFarmResourceGroup": {
      "type": "string",
      "defaultValue": "CXP-TomSun"
    }},
  "variables": {
    "webSiteName": "[concat('TomARMTest-1', uniqueString(resourceGroup().id))]",
    "existingApplicationInsight": "tomarmtestdemo"
  },
  "resources": [
    {
      "name": "[variables('webSiteName')]",
      "type": "Microsoft.Web/sites",
      "location": "southcentralus",
      "apiVersion": "2015-08-01",
      "dependsOn": [
      ],
      "tags": {
        "[concat('hidden-related:', resourceId(parameters('serverFarmResourceGroup'), 'Microsoft.Web/serverFarms', parameters('serverFarmName')))]": "Resource",
        "displayName": "TomARMTest"
      },
      "properties": {
        "name": "[variables('webSiteName')]",
        "serverFarmId": "[resourceId(parameters('serverFarmResourceGroup'), 'Microsoft.Web/serverFarms', parameters('serverFarmName'))]"
      },
      "resources": [
        {
          "name": "appsettings",
          "type": "config",
          "apiVersion": "2015-08-01",
          "dependsOn": [
            "[resourceId('Microsoft.Web/sites', variables('webSiteName'))]",
            "[resourceId('microsoft.insights/components/', variables('existingApplicationInsight'))]"
          ],
          "tags": {
            "displayName": "tomtest"
          },
          "properties": {
            "APPINSIGHTS_INSTRUMENTATIONKEY": "xxxxxxxxxxx"

          }
        },
        {
          "apiVersion": "2015-08-01",
          "name": "Microsoft.ApplicationInsights.AzureWebSites",
          "type": "siteextensions",
          "dependsOn": [
            "[resourceId('Microsoft.Web/Sites', variables('webSiteName'))]"
          ],
          "properties": {
          }
        }
      ]

    },
    {
      "apiVersion": "2014-04-01",
      "name": "[variables('existingApplicationInsight')]",
      "type": "Microsoft.Insights/components",
      "location": "East US",
      "tags": {
        "[concat('hidden-link:', resourceGroup().id, '/providers/Microsoft.Web/sites/', variables('webSiteName'))]": "Resource",
        "displayName": "AppInsightsComponent"
      },
      "properties": {
        "applicationId": "[variables('existingApplicationInsight')]"
      }
    }
  ],
  "outputs": {}
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...