如何使用 ARM 模板创建 Application Insights API 注释键

How to create Application Insights API Key for annotations using ARM template

我正在尝试从 ARM 模板创建 Application Insights API 密钥。我需要 API 密钥用于在资源部署期间创建的写入注释,以便在从 Azure Devops 部署应用程序期间发布注释有效。

我试图找到有关如何使其正常工作的信息,但我只能找到有关如何使用 PowerShell 或 Azure REST API 创建密钥的示例。

我需要开始工作的是使用 ARM 模板创建 API 键。 我已经用 json 进行了多次尝试,但都没有成功;

 {
  "name": "[variables('applicationInsightsName')]",
  "type": "Microsoft.Insights/components",
  "location": "[resourceGroup().location]",
  "apiVersion": "2014-04-01",
  "tags": {
    "displayName": "[concat('Component ', variables('applicationInsightsName'))]"
  },
  "properties": {
    "applicationId": "[variables('applicationInsightsName')]"
  },
  "resources": [
    {
      "name": "action",
      "type": "apikeys",
      "location": "[resourceGroup().location]",
      "apiVersion": "2015-05-01",
      "properties": {
        "name": "Azure Devops Release Annotations",
        "linkedWriteProperties": [
          "[concat(resourceId('Microsoft.Insights/components', variables('applicationName')), '/annotations')]"
        ],
        "linkedReadProperties": []
      },
      "dependsOn": [
        "[resourceId('Microsoft.Insights/components', variables('applicationInsightsName'))]"
      ]
    }
  ]
}

目前为止我找到的最好的信息是 this,但帮助不大。

是否可以使用 ARM 模板创建 API 密钥?

不,这是不可能的,ARM 模板仅模拟 PUT 请求,而 Microsoft.Insights/Components/ApiKeys/ActionPOST 请求。

我建议一个简单的方法。您可以使用参考 InstrumentationKey 的输出。然后您可以在任何地方使用它:另一个资源、链接模板或使用 Azure DevOps 的后续步骤的输出。

{
    "$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "name": {
            "type": "string",
            "defaultValue": "[concat('Whosebug-', uniqueString(resourceGroup().name))]"
        },
        "type": {
            "type": "string",
            "defaultValue": "web"
        },
        "requestSource": {
            "type": "string",
            "defaultValue": "IbizaAIExtension"
        }
    },
    "resources": [
        {
            "name": "[parameters('name')]",
            "type": "microsoft.insights/components",
            "location": "[resourceGroup().location]",
            "apiVersion": "2014-08-01",
            "properties": {
                "ApplicationId": "[parameters('name')]",
                "Application_Type": "[parameters('type')]",
                "Flow_Type": "Redfield",
                "Request_Source": "[parameters('requestSource')]"
            }
        }
    ],
    "outputs":{
        "APPINSIGHTS_INSTRUMENTATIONKEY":{
            "type": "string",
            "value": "[reference(resourceId('Microsoft.Insights/components', parameters('name')), '2014-08-01').InstrumentationKey]"
        }
    }
}

我用过很多次