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

PP0*_*006 8 azure azure-resource-manager azure-application-insights azure-rm-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')]"
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

现在我尝试将在另一个资源组中运行的 azure 函数应用程序与此 appInsights 链接。

下面是我尝试过的代码,

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

但我收到以下错误,

在此输入图像描述

有人可以提供一些如何破解这个的想法吗?

kwi*_*ill 9

可以对已从其他模板部署的资源使用参考功能。您只需传入https://learn.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]"
Run Code Online (Sandbox Code Playgroud)

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

{
  "$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]"
      }
  }
}
Run Code Online (Sandbox Code Playgroud)