如何return Azure Eventhub primary and secondary connection keys as ARM template output?

How to return Azure Eventhub primary and secondary connection keys as ARM template output?

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

我想要 return 形式的字符串:

Endpoint=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]"

但出现部署错误:

[error]ParentResourceNotFound: Can not perform requested operation on nested resource. Parent resource 'my-rg-namespace' not found.

更新:

按照 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 模板在两个方面是错误的,因为它没有创建事件中心,而是创建了一个集群,并且它在我的命名空间之外,并提供了这个有价值的评论:

The Event Hubs cluster is basically a way of reserving dedicated compute. It's not something that most scenarios need and it is... not cheap. Think of something on the scale of Xbox Live where you're seeing nearly 5 millions of events per second and which have higher performance needs. If you're not looking at that kind of scale or that sensitivity around timing, you probably want to rethink the need for a dedicated cluster.

Normally, you'd just provision an Event Hubs namespace which will use shared infrastructure with certain guarantees to minimize noisy neighbors and similar. This is adequate for the majority of scenarios, even those with high throughput needs. If you're not sure, this is probably the place that you want to start and then upgrade to a dedicated cluster if your needs justify the cost.

An Event Hubs namespace is the container for a set of Event Hub instances grouped together by a unique endpoint. Each Event Hub is made of a set of partitions. When you're publishing or consuming events, the partitions of an Event Hub are where the actual data is. When you're working with one of the SDKs, you'll start by telling it about the endpoint of your namespace and the Event Hub that you're interested in. You'll need a general awareness of partitions, but most of the "Getting Started" scenarios handle that detail for you, as do a fair portion of the real-world ones.... but, the concept is an important one.

您似乎使用了不正确的资源 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]"
    }
}

可以在 Event Hubs sample template.

中找到简单部署的完整示例