简体   繁体   中英

How to create multiple Service Bus topics using ARM template?

I've looked at the https://github.com/Azure/azure-quickstart-templates/tree/master/101-servicebus-topic example. Now I'm trying to figure out how to create several topics when running the deploy script using nested resource looping ( https://azure.microsoft.com/en-us/documentation/articles/resource-group-create-multiple/#looping-on-a-nested-resource ).

It would be nice to just define an array in the value for serviceBusTopicName and then somehow creating topics by looping through it, instead of doing it manually.

I am getting the following error when trying to deploy.

 Error: Code=InvalidTemplate; Message=Deployment template validation failed: 'The template resource '[parameters('serviceBusTopicName')[copyIndex()]]' at line '72' column '14' is not valid.
 Copying nested resources is not supported. Please see https://aka.ms/arm-copy/#looping-on-a-nested-resource for usage details.'.

This is the template.json that I have tried to get to work.

  "parameters": {
"serviceBusNamespaceName": {
  "type": "string",
  "metadata": {
    "description": "Name of the Service Bus namespace"
  }
},
"serviceBusTopicName": {
  "type": "array",
  "metadata": {
    "description": "Name of the Topic"
  }
},
"serviceBusApiVersion": {
  "type": "string",
  "defaultValue": "2015-08-01",
  "metadata": {
    "description": "Service Bus ApiVersion used by the template"
  },
  "resources": [
{
  "apiVersion": "[variables('sbVersion')]",
  "name": "[parameters('serviceBusNamespaceName')]",
  "type": "Microsoft.ServiceBus/Namespaces",
  "location": "[variables('location')]",
  "kind": "Messaging",
  "sku": {
    "name": "StandardSku",
    "tier": "Standard"
  },
  "resources": [
    {
      "apiVersion": "[variables('sbVersion')]",
      "name": "[parameters('serviceBusTopicName')]",
      "type": "Topics",
      "dependsOn": [
        "[concat('Microsoft.ServiceBus/namespaces/', parameters('serviceBusNamespaceName'))]"
      ],
      "properties": {
        "path": "[parameters('serviceBusTopicName')]"
      },
      "copy": {
        "name": "datasetcopy",
        "count": "[length(parameters('serviceBusTopicName'))]"
      }
    }
  ]
},

parameters.json

{"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
    "serviceBusNamespaceName": {
        "value": "ServiceBus"
    },
  "serviceBusTopicName": {
    "value": [
      "Person",
      "Stadium",
      "Team"
    ]
  },
    "serviceBusApiVersion": {
        "value": "2015-08-01"
    }
}

As @Lain said, copying nested resource isn't supported as of now Check Here . For deploying multiple resource you have to move it to root. I just finished my POC for deploying multiple topics (Just topics assuming Namespace already exists) here is the code. I am passing topic names as a comma separated string:

{
"$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
    "serviceBusNamespaceName": {
        "type": "string",
        "metadata": {
            "description": "Name of the Service Bus namespace"
        }
    },
    "serviceBusTopicName": {
        "type": "string",
        "metadata": {
            "description": "Comma seperated Topic Names"
        }
    },
    "serviceBusApiVersion": {
        "type": "string",
        "defaultValue": "2015-08-01",
        "metadata": {
            "description": "Service Bus ApiVersion used by the template"
        }
    }
},
"variables": {
    "location": "[resourceGroup().location]",
    "sbVersion": "[parameters('serviceBusApiVersion')]",
    "TopicNames": "[split(parameters('serviceBusTopicName'), ',')]"
},
"resources": [{
    "apiVersion": "[variables('sbVersion')]",
    "name": "[concat(parameters('serviceBusNamespaceName'), '/', variables('TopicNames')[copyIndex()])]",
    "type": "Microsoft.ServiceBus/Namespaces/Topics",
    "copy": {
        "name": "TopicNameCopy",
        "count": "[length(variables('TopicNames'))]"
    },
    "properties": {
        "path": "[variables('TopicNames')[copyIndex()]]"
    }
}]
}

The link in the error message goes through this pretty well, but you can't have loops in nested resources, you need to push the resource up to the top level, and then link the resources together using names. This template will do what you seem to be trying to do:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "serviceBusNamespaceName": {
      "type": "string",
      "metadata": {
        "description": "Name of the Service Bus Namespace"
      }
    },
    "serviceBusTopicNames": {
      "type": "array",
      "metadata": {
        "description": "Name of the Service Bus Topic"
      }
    }
  },
  "variables": {
    "sbVersion": "2015-08-01"
  },
  "resources": [
    {
      "apiVersion": "[variables('sbVersion')]",
      "name": "[parameters('serviceBusNamespaceName')]",
      "type": "Microsoft.ServiceBus/namespaces",
      "location": "[resourceGroup().location]",
      "properties": {
      }
    },
    {
      "apiVersion": "[variables('sbVersion')]",
      "name": "[concat(parameters('serviceBusNamespaceName'), '/',  parameters('serviceBusTopicNames')[copyIndex()])]",
      "type": "Microsoft.ServiceBus/namespaces/Topics",
      "dependsOn": [
        "[concat('Microsoft.ServiceBus/namespaces/', parameters('serviceBusNamespaceName'))]"
      ],
      "copy": {
        "name": "datasetcopy",
        "count": "[length(parameters('serviceBusTopicNames'))]"
      },
      "properties": {
        "path": "[parameters('serviceBusTopicNames')[copyIndex()]]"
      },
      "resources": [
      ]
    }
  ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM