首页 > 解决方案 > 如何将 Azure Eventhub 主连接密钥和辅助连接密钥作为 ARM 模板输出返回?

问题描述

我已经准备了一个用于部署 Azure Eventhub 实例的 ARM 模板,并且想知道如何访问这两个连接密钥以将它们作为输出返回?

门户截图

我想以以下形式返回一个字符串:

端点=sb://my-eventhub.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=ojZMQcJD7uYifxJyGeXG6tNDdZyaC1/h5tmX6ODVfmY=

这是我当前的模板:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "clusterName": {
            "type": "string",
            "defaultValue": "eventhub",
            "metadata": {
                "description": "Name for the Event Hub cluster."
            }
        },
        "namespaceName": {
            "type": "string",
            "defaultValue": "namespace",
            "metadata": {
                "description": "Name for the Namespace to be created in cluster."
            }
        }
    },
    "variables": {
        "clusterName": "[concat(resourceGroup().name, '-', parameters('clusterName'))]",
        "namespaceName": "[concat(resourceGroup().name, '-', parameters('namespaceName'))]"
    },
    "outputs": {
        "MyClusterName": {
            "type": "string",
            "value": "[variables('clusterName')]"
        },
        "PrimaryConnectionString": {
            "type": "string",
            "value": "WHAT TO USE HERE PLEASE?"
        },
        "SecondaryConnectionString": {
            "type": "string",
            "value": "WHAT TO USE HERE PLEASE?"
        }
    },
    "resources": [
        {
            "type": "Microsoft.EventHub/clusters",
            "apiVersion": "2018-01-01-preview",
            "name": "[variables('clusterName')]",
            "location": "[resourceGroup().location]",
            "sku": {
                "name": "Dedicated",
                "capacity": 1
            }
        },
        {
            "type": "Microsoft.EventHub/namespaces",
            "apiVersion": "2018-01-01-preview",
            "name": "[variables('namespaceName')]",
            "location": "[resourceGroup().location]",
            "dependsOn": [
                "[resourceId('Microsoft.EventHub/clusters', variables('clusterName'))]"
            ],
            "sku": {
                "name": "Standard",
                "tier": "Standard",
                "capacity": 1
            },
            "properties": {
                "isAutoInflateEnabled": false,
                "maximumThroughputUnits": 0,
                "clusterArmId": "[resourceId('Microsoft.EventHub/clusters', variables('clusterName'))]"
            }
        }
    ]
}

我尝试了以下方法:

"value": "[listKeys(resourceId(concat('Microsoft.ServiceBus/namespaces/AuthorizationRules'), variables('namespaceName'), 'RootManageSharedAccessKey'),'2018-01-01-preview').primaryConnectionString]"

但得到部署错误:

[错误]ParentResourceNotFound:无法对嵌套资源执行请求的操作。未找到父资源“my-rg-namespace”。

更新:

按照 Jesse 的建议,以下内容对我有用(谢谢!):

"variables": {
    "clusterName": "[concat(resourceGroup().name, '-', parameters('clusterName'))]",
    "namespaceName": "[concat(resourceGroup().name, '-', parameters('namespaceName'))]",
    "defaultSASKeyName": "RootManageSharedAccessKey",
    "authRuleResourceId": "[resourceId('Microsoft.EventHub/namespaces/authorizationRules', variables('namespaceName'), variables('defaultSASKeyName'))]"
},
"outputs": {
    "MyClusterName": {
        "type": "string",
        "value": "[variables('clusterName')]"
    },
    "PrimaryConnectionString": {
        "type": "string",
        "value": "[listkeys(variables('authRuleResourceId'), '2015-08-01').primaryConnectionString]"
    },
    "SecondaryConnectionString": {
        "type": "string",
        "value": "[listkeys(variables('authRuleResourceId'), '2015-08-01').secondaryConnectionString]"
    }
},

更新 2:

此外,Jesse 注意到我的 ARM 模板在两个方面存在错误,因为它没有创建事件中心,而是创建了一个集群,并且它在我的命名空间之外,并提供了以下有价值的评论:

事件中心集群基本上是一种预留专用计算的方式。这不是大多数情况下需要的东西,而且……不便宜。想一想 Xbox Live 的规模,您每秒可以看到近 500 万个事件,并且具有更高的性能需求。如果您没有考虑这种规模或时间敏感性,您可能需要重新考虑对专用集群的需求。

通常,您只需配置一个事件中心命名空间,该命名空间将使用共享基础架构,并确保将嘈杂的邻居和类似情况降至最低。这对于大多数场景来说已经足够了,即使是那些需要高吞吐量的场景。如果您不确定,这可能是您想要开始的地方,然后如果您的需要证明成本合理,则升级到专用集群。

事件中心命名空间是一组事件中心实例的容器,这些实例由唯一的终结点组合在一起。每个事件中心都由一组分区组成。当您发布或使用事件时,事件中心的分区是实际数据所在的位置。当您使用其中一个 SDK 时,首先要告诉它您的命名空间的端点和您感兴趣的事件中心。您需要对分区有一个普遍的认识,但大多数“入门”场景会为您处理这些细节,现实世界中的相当一部分也是如此......但是,这个概念是一个重要的概念。

标签: azureazure-eventhubarm-template

解决方案


看起来您可能使用了不正确的资源 ID,因为没有具有正确名称的服务总线命名空间,所以从失败的地方Microsoft.ServiceBus而不是从失败的地方拉取。Microsoft.EventHub

您可能想尝试使用类似于以下的表单来识别您的资源:

"variables": {                
    "location": "[resourceGroup().location]",
    "apiVersion": "2015-08-01",
    "defaultSASKeyName": "RootManageSharedAccessKey",
    "authRuleResourceId": "[resourceId('Microsoft.EventHub/namespaces/authorizationRules', parameters('namespaceName'), variables('defaultSASKeyName'))]"
},

这应该允许使用listkeys您在上面详述的方式返回它:

"outputs": {
    "NamespaceConnectionString": {
        "type": "string",
        "value": "[listkeys(variables('authRuleResourceId'), variables('apiVersion')).primaryConnectionString]"
    }
}

可以在事件中心示例模板中找到简单部署的完整示例。


推荐阅读