首页 > 解决方案 > 我们能否在 Azure 资源组模板中获取 Application Insights 的连接字符串?

问题描述

我们可以在 Azure 资源组模板中检索 Application Insights 实例的连接字符串吗?

我可以通过下面的代码检索检测密钥,但是当我尝试使用相同或 Listkey 获取 connectionString 时,它会给出错误。

"outputs": {
    "MyAppInsightsInstrumentationKey": {
        "value": "[reference(resourceId('Microsoft.Insights/components', variables('myAppInsightsInstanceName')), '2014-04-01').connectionString]",
        "type": "string"
    }
}

错误:{"error":{"code":"InvalidTemplate","message":"部署模板验证失败:'找不到模板变量'myAppInsightsInstanceName'。请参阅https://aka.ms/arm-template /#variables使用详情。'.","additionalInfo":[{"type":"TemplateViolation","info":{"lineNumber":95,"linePosition":40,"path":"properties.template .outputs.MyAppInsightsInstrumentationKey"}}]}}

标签: azureazure-application-insightsazure-resource-managerinstrumentation

解决方案


正如@ZakiMa 所评论的,您需要使用更新的 API 版本。

像这样的东西应该工作:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "variables": {
    "MyAppInsightsInstanceName": "<My App Insights Instance Name>"
  },
  "outputs": {
    "MyAppInsightsConnectionString": {
      "value": "[reference(resourceId('Microsoft.Insights/components', variables('MyAppInsightsInstanceName')), '2020-02-02').ConnectionString]",
      "type": "string"
    }
  },
  "resources": []
}

推荐阅读