如何在 ARM 模板中检索 Application Insight(位于另一个资源组中)的检测密钥?

How can i retrieve instrumentation key for an Application Insight (which resides in another resource group) in ARM template?

是否有任何方法可以在 ARM 模板中检索 Application Insights(位于另一个资源组中)的检测密钥?

我已经使用以下代码使用 ARM 模板创建了一个 appInsights,

{
  "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "AppInsightsName": { "type": "string" },
    "Location": { "type": "string", "defaultValue": "westeurope" }
  },
  "variables": {
    //"apiVersion": "2018-02-01-preview",
    "apiVersion": "2016-08-01",
    "location": "[parameters('Location')]",
    "ApplicationInsightsName": "[parameters('AppInsightsName')]"
  },
  "resources": [
    {
      "apiVersion": "2014-04-01",
      "type": "Microsoft.Insights/components",
      "name": "[variables('ApplicationInsightsName')]",
      "location": "[variables('location')]",
      "kind": "other",
      "properties": {
        "applicationId": "[variables('ApplicationInsightsName')]"
      }
    }
  ]
}

现在我正在尝试 link azure 函数应用程序,它使用此 appInsights 在另一个资源组中运行。

下面是我试过的代码,

{
  "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
  "value": "[reference(resourceId(variables('AppInsightsResourceGroup'),'Microsoft.Insights/components', variables('ApplicationInsightsName'))).InstrumentationKey]"
}

但我收到以下错误,

有人可以提供一些破解方法吗?

可以对已从另一个模板部署的资源使用引用功能。您只需要按照 https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/template-functions-resource#reference 文档中的说明传入 apiVersion 参数。请注意,您还需要将引用的 属性 从“.InstrumentationKey”更改为“.properties.InstrumentationKey”。

"value": "[reference(resourceId(variables('AppInsightsResourceGroup'),'Microsoft.Insights/components', variables('ApplicationInsightsName')), '2015-05-01', 'Full').properties.InstrumentationKey]"

您可以部署以下模板进行验证(只需将两个变量替换为您的值):

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
  },
  "variables": {
      "AppInsightsResourceGroup": "myAIRG",
      "ApplicationInsightsName": "myAI"
  },
  "resources": [
  ],
  "outputs": {
      "appInsightsKey": {
          "type": "string",
          "value": "[reference(resourceId(variables('AppInsightsResourceGroup'),'Microsoft.Insights/components', variables('ApplicationInsightsName')), '2015-05-01', 'Full').properties.InstrumentationKey]"
      }
  }
}