我编写了一个ARM模板来编写ServiceBus的部署和配置脚本。该脚本的目标之一是使管理主题和订阅变得容易。为了实现这一点,脚本使用数组变量。
这一切都很好,但每当我尝试为两个不同的主题使用相同的订阅名称时,我就会看到一个问题。现在,我了解订阅只能映射到一个主题。该脚本尝试通过将订阅名称加入主题来解释该问题。
我还应该注意,Azure UI允许您在两个主题下使用相同的订阅名称。此脚本源自通过azure控制台设置此方案,然后导出ARM。
我已经在这个脚本上打了几十次,但我没有看到原因。希望新的眼睛会有所帮助。
这是脚本:
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"envType": {
"type": "string",
"allowedValues": [ "dev", "prod" ],
"defaultValue": "dev",
"metadata": { "description": "The environment type being created" }
},
"sbSku": {
"type": "string",
"allowedValues": [ "Standard", "Premium" ],
"defaultValue": "Standard",
"metadata": { "description": "The messaging tier for service Bus namespace" }
}
},
"variables": {
"defaultSASKeyName": "RootManageSharedAccessKey",
"authRuleResourceId": "[resourceId('Microsoft.ServiceBus/namespaces/authorizationRules', variables('sbNamespaceName'), variables('defaultSASKeyName'))]",
"sbNamespaceName": "[concat(parameters('envType'), 'eventbus')]",
"sbVersion": "2017-04-01",
"sbTopics": [
"mytopic1",
"mytopic2",
"mytopic3",
"mytopic4"
],
"sbSubscriptions": [
{ "Name": "mysubA", "Topic": "mytopic1" },
{ "Name": "mysubB", "Topic": "mytopic2" },
{ "Name": "mysubB", "Topic": "mytopic3" },
{ "Name": "mysubC", "Topic": "mytopic4" }
]
},
"resources": [
{
"apiVersion": "[variables('sbVersion')]",
"location": "[resourceGroup().location]",
"name": "[variables('sbNamespaceName')]",
"properties": {},
"sku": {
"name": "[parameters('sbSku')]"
},
"tags": {},
"type": "Microsoft.ServiceBus/Namespaces"
},
{
"type": "Microsoft.ServiceBus/namespaces/topics",
"name": "[concat(variables('sbNamespaceName'), '/', variables('sbTopics')[copyIndex()])]",
"apiVersion": "[variables('sbVersion')]",
"location": "[resourceGroup().location]",
"dependsOn": [
"[concat('Microsoft.ServiceBus/namespaces/', variables('sbNamespaceName'))]"
],
"properties": {
"defaultMessageTimeToLive": "P14D",
"maxSizeInMegabytes": 1024,
"requiresDuplicateDetection": false,
"enablePartitioning": true
},
"copy": {
"name": "topiccopy",
"count": "[length(variables('sbTopics'))]",
"mode": "Serial",
"batchSize": 3
}
},
{
"type": "Microsoft.ServiceBus/namespaces/topics/subscriptions",
"name": "[concat(variables('sbNamespaceName'), '/', variables('sbSubscriptions')[copyIndex()].Topic, '/', variables('sbSubscriptions')[copyIndex()].Name)]",
"apiVersion": "2017-04-01",
"location": "East US",
"dependsOn": [
"[resourceId('Microsoft.ServiceBus/namespaces', variables('sbNamespaceName'))]",
"[resourceId('Microsoft.ServiceBus/namespaces/topics', variables('sbNamespaceName'), variables('sbSubscriptions')[copyIndex()].Topic)]"
],
"properties": {
"maxDeliveryCount": 10
},
"copy": {
"name": "subscriptioncopy",
"count": "[length(variables('sbSubscriptions'))]",
"mode": "Serial",
"batchSize": 1
}
}
],
"outputs": {
"NamespaceConnectionString": {
"type": "string",
"value": "[listkeys(variables('authRuleResourceId'), variables('sbVersion')).primaryConnectionString]"
},
"SharedAccessPolicyPrimaryKey": {
"type": "string",
"value": "[listkeys(variables('authRuleResourceId'), variables('sbVersion')).primaryKey]"
},
"Topics": {
"type": "array",
"value": "[concat(variables('sbTopics'))]"
},
"Subscriptionss": {
"type": "array",
"value": "[concat(variables('sbSubscriptions'))]"
}
}
}
执行时:
New-AzureRmResourceGroupDeployment -ResourceGroupName {xxx} -TemplateFile arm.servicebus.example.json
它返回:
New-AzureRmResourceGroupDeployment : 2:58:05 PM - Error: Code=InvalidTemplate; Message=Deployment template validation
failed: 'The template resource 'Microsoft.ServiceBus/namespaces/deveventbus/topics/mytopic3/subscriptions/mysubB'
cannot reference itself. Please see https://aka.ms/arm-template-expressions/#reference for usage details.'.
At line:1 char:1
+ New-AzureRmResourceGroupDeployment -ResourceGroupName Wiretappers_Ste ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-AzureRmResourceGroupDeployment], Exception
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureResourceGroupDep
loymentCmdlet
New-AzureRmResourceGroupDeployment : The deployment validation failed
At line:1 char:1
+ New-AzureRmResourceGroupDeployment -ResourceGroupName Wiretappers_Ste ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [New-AzureRmResourceGroupDeployment], InvalidOperationException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureResourceGroupDep
loymentCmdlet
该问题是由'sbSubscriptions'数组(mysubB / mytopic3)中的第三个条目引起的。这是在'资源'下面的第三个对象中处理的。
如果有人能看到我的疏忽,我们将不胜感激。
PS。如果有人知道如何让Azure工具在扩展“复制”节点和功能(resourceId,concat)之后输出模板json,那么这也是有用的。
更新:2018-03-01这是一个工作模板供将来参考。有关详细信息,请参阅下面的所
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"envType": {
"type": "string",
"allowedValues": [ "dev", "prod" ],
"defaultValue": "dev",
"metadata": { "description": "The environment type being created" }
},
"sbSku": {
"type": "string",
"allowedValues": [ "Standard", "Premium" ],
"defaultValue": "Standard",
"metadata": { "description": "The messaging tier for service Bus namespace" }
}
},
"variables": {
"sbNamespaceName": "[concat(parameters('envType'), 'eventbus')]",
"sbVersion": "2017-04-01",
"sbTopics": [
"mytopic1",
"mytopic2",
"mytopic3",
"mytopic4"
],
"sbSubscriptions": [
{ "Name": "mysubA", "Topic": "mytopic1" },
{ "Name": "mysubB", "Topic": "mytopic2" },
{ "Name": "mysubB", "Topic": "mytopic3" },
{ "Name": "mysubC", "Topic": "mytopic4" }
]
},
"resources": [
{
"apiVersion": "[variables('sbVersion')]",
"location": "[resourceGroup().location]",
"name": "[variables('sbNamespaceName')]",
"properties": {},
"sku": {
"name": "[parameters('sbSku')]"
},
"tags": {},
"type": "Microsoft.ServiceBus/Namespaces"
},
{
"type": "Microsoft.ServiceBus/namespaces/topics",
"name": "[concat(variables('sbNamespaceName'), '/', variables('sbTopics')[copyIndex()])]",
"apiVersion": "[variables('sbVersion')]",
"location": "[resourceGroup().location]",
"dependsOn": [
"[variables('sbNamespaceName')]"
],
"properties": {
"defaultMessageTimeToLive": "P14D",
"maxSizeInMegabytes": 1024,
"requiresDuplicateDetection": false,
"enablePartitioning": true
},
"copy": {
"name": "topiccopy",
"count": "[length(variables('sbTopics'))]",
"mode": "Serial",
"batchSize": 3
}
},
{
"type": "Microsoft.ServiceBus/namespaces/topics/subscriptions",
"name": "[concat(variables('sbNamespaceName'), '/', variables('sbSubscriptions')[copyIndex()].Topic, '/', variables('sbSubscriptions')[copyIndex()].Name)]",
"apiVersion": "[variables('sbVersion')]",
"location": "[resourceGroup().location]",
"dependsOn": [
"[resourceId('Microsoft.ServiceBus/namespaces', variables('sbNamespaceName'))]",
"[resourceId('Microsoft.ServiceBus/namespaces/topics', variables('sbNamespaceName'), variables('sbSubscriptions')[copyIndex()].Topic)]"
],
"properties": {
"maxDeliveryCount": 10
},
"copy": {
"name": "subscriptioncopy",
"count": "[length(variables('sbSubscriptions'))]"
}
}
],
"outputs": {
"Topics": {
"type": "array",
"value": "[concat(variables('sbTopics'))]"
},
"Subscriptionss": {
"type": "array",
"value": "[concat(variables('sbSubscriptions'))]"
}
}
}
好吧,我不知道问题是什么,但是这有效:
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"envType": {
"type": "string",
"allowedValues": [
"dev",
"prod",
"zlp"
],
"defaultValue": "zlp",
"metadata": {
"description": "The environment type being created"
}
},
"sbSku": {
"type": "string",
"allowedValues": [
"Standard",
"Premium"
],
"defaultValue": "Standard",
"metadata": {
"description": "The messaging tier for service Bus namespace"
}
}
},
"variables": {
"sbNamespaceName": "[concat(parameters('envType'), 'eventbus')]",
"sbVersion": "2017-04-01",
"sbTopics": [
"mytopic1",
"mytopic2",
"mytopic3",
"mytopic4"
],
"sbSubscriptions": [
{
"Name": "mysubA",
"Topic": "mytopic1"
},
{
"Name": "mysubB",
"Topic": "mytopic2"
},
{
"Name": "mysubB",
"Topic": "mytopic3"
},
{
"Name": "mysubC",
"Topic": "mytopic4"
}
]
},
"resources": [
{
"apiVersion": "[variables('sbVersion')]",
"location": "[resourceGroup().location]",
"name": "[variables('sbNamespaceName')]",
"properties": {},
"sku": {
"name": "[parameters('sbSku')]"
},
"tags": {},
"type": "Microsoft.ServiceBus/Namespaces"
},
{
"type": "Microsoft.ServiceBus/namespaces/topics",
"name": "[concat(variables('sbNamespaceName'), '/', variables('sbTopics')[copyIndex()])]",
"apiVersion": "[variables('sbVersion')]",
"location": "[resourceGroup().location]",
"dependsOn": [
"[variables('sbNamespaceName')]"
],
"properties": {
"defaultMessageTimeToLive": "P14D",
"maxSizeInMegabytes": 1024,
"requiresDuplicateDetection": false,
"enablePartitioning": true
},
"copy": {
"name": "topiccopy",
"count": "[length(variables('sbTopics'))]"
}
},
{
"type": "Microsoft.ServiceBus/namespaces/topics/subscriptions",
"name": "[concat(variables('sbNamespaceName'), '/', variables('sbSubscriptions')[copyIndex()].Topic, '/', variables('sbSubscriptions')[copyIndex()].Name)]",
"apiVersion": "[variables('sbVersion')]",
"location": "[resourceGroup().location]",
"dependsOn": [
"topiccopy"
],
"properties": {
"maxDeliveryCount": 10
},
"copy": {
"name": "subscriptioncopy",
"count": "[length(variables('sbSubscriptions'))]"
}
}
]
}
您也可以使用它来调试:
Test-AzureRmResourceGroupDeployment -verbose or -debug