使用Azure Resource Manager将已创建的Azure Application Insight与App Service中的API App集成

时间:2017-12-29 08:06:45

标签: azure azure-web-sites azure-application-insights

我正在使用Terraform脚本在不同的资源组中创建应用洞察。 我想在创建Web应用程序或API应用程序时关联先前创建的App Insight。 我在Web应用程序中传递了先前创建的app洞察的Intrumentation键,但是它们没有被链接。 当我尝试在创建Web应用程序的同一个ARM模板中创建应用程序洞察时,它可以正常工作。

示例效果良好的ARM示例:

{
    "$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')]"
            }
        }
    ]
}

不起作用的示例ARM:

{
    "$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": {
                    }
                }
            ]
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

请将代码从"APPINSIGHTS_INSTRUMENTATIONKEY": "['99f330eb-ae79-4070-b8b6-e50d01f6e391123134434sdfdadaa']"更改为"APPINSIGHTS_INSTRUMENTATIONKEY": "99f330eb-ae79-4070-b8b6-e50d01f6e391123134434sdfdadaa"

对于字符串值,不应该括号:[和] 。括号[和]用于ARM template functions

  

您可以在模板中添加函数,方法是将它们括在括号中:[和]。

<强>更新

  

我正在传递网络应用中先前创建的应用洞察的Intrumentation键,但是它们没有被链接。

根据我的测试,如果要链接到Azure webapp,还需要添加以下代码。 它也适用于现有的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模板

{
  "$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": {}
}