ServiceBus 的 Azure ARM 模板:无法引用自身

Azure ARM template for ServiceBus: cannot reference itself

我编写了一个 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

它returns:

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) 引起的。这正在 'resources'.

下面的第三个对象中处理

如果有人能看到我的疏漏,将不胜感激。

PS。如果有人知道如何让 Azure 工具在扩展 "copy" 节点和函数(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'))]"
        }
    }
}

好的,我不知道问题是什么 is\was,但这有效:

{
    "$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