Skip to main content

Cloud

Azure ARM Template: Create Service Bus Topic with Subscription

The previous article in this series covered how to setup an Azure Resource Group project with ARM Template that creates an Azure Service Bus Namespace. This article build upon the previous, to create an Azure Service Bus Topic with a Subscription to catch all messages.
Instead of starting from scratch, this article builds on the previous “Azure ARM Template: Create Service Bus Namespace” article. If you are unfamiliar with this please go read that article, then come back here. Also, if you are somewhat unfamiliar with Azure Resource Manager and ARM Templates, then I recommend you start by reading the following additional posts from earlier in this ARM Template series:

Now that you’re familiar with Azure Resource Manager and ARM Templates, let’s start expanding on the Create Service Bus article into creating an Azure Service Bus Topic with Subscription.

Create Service Bus Namespace

For additional clarity within this article, we’ll start by showing the ARM Template from the previous article that creates an Azure Service Bus Namespace. This is the most simplistic ARM Template possible for achieving this to help make things as clear as possible.

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
  },
  "variables": {
  },
  "resources": [
    {
      "apiVersion": "2015-08-01",
      "name": "SuperSBNamespace",
      "type": "Microsoft.ServiceBus/namespaces",
      "location": "[resourceGroup().location]",
      "properties": {
        "messagingSku": "1"
      },
      "resources": [
      ]
    }
  ],
  "outputs": {
  }
}

Add Service Bus Topic

To create an Azure Service Bus Topic, the above ARM Template needs to be modified to also include a short snippet that defines the Service Bus Topic to add. This snippet will also need to be added to the “resources” section of the Service Bus Namespace block so that Azure knows the Service Bus Topic is to be created within the Service Bus Namespace.
Here’s just the snippet necessary for the Azure Service Bus Topic that will need to be added:

{
  "apiVersion": "2015-08-01",
  "name": "MyTopic",
  "type": "Topics",
  "dependsOn": [
    "Microsoft.ServiceBus/namespaces/SuperSBNamespace"
  ],
  "properties": {
    "path": "MyTopic"
  },
  "resources": [
  ]
}

Notice that the name of the Service Bus Topic (“MyTopic” in this example) is set as both the “name” of the resource as well as the “path” property of the Topic itself.
The full ARM Template for everything is listed down towards the bottom of this article.

Add Topic Subscription

The next step is to add a snippet to the “resources” of the Azure Service Bus Topic to describe the Subscription to be created. This needs to be added to the “resources” section for the Topic declaration so that Azure knows to create the Subscription on this Service Bus Topic.
Here’s just the snippet necessary for defining the Subscription to be added:

{
    "apiVersion": "2015-08-01",
    "name": "MySubscription",
    "type": "Subscriptions",
    "dependsOn": [
      "MyTopic"
    ],
    "properties": {
    },
    "resources": [
    ]
}

The name of the Azure Service Bus Topic Subscription is set using the “name”; in this example it’s set to “MySubscription”. Also, the “dependsOn” needs an element defined with the name of the Service Bus Topic this Subscription is to be created under; in this example it’s “MyTopic”.
This Subscription doesn’t specify any kind of Message Filter. When declared as shown this Subscription will catch all messages send to the Service Bus Topic since it doesn’t have any kind of Filter applied.
The full ARM Template for everything is listed down towards the bottom of this article.

Parameterized Full Example

Now, let’s put the above examples together into a single, full ARM Template that created an Azure Service Bus with Topic and Subscription on the Topic.
We wont go into full detail here on how to parameterize an ARM Template. That is covered in  little detail within the articles on defining Web App Connection Strings and Web App Application Settings within an ARM Template.
After taking what was learned from the previous articles to parameterize this ARM Template, in addition to setup a couple variables to clean things up, here’s a full ARM Template example that will setup an Azure Service Bus Namespace with Topic and Subscription as shown above.

{
  "$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"
      }
    },
    "serviceBusTopicName": {
      "type": "string",
      "metadata": {
        "description": "Name of the Service Bus Topic"
      }
    },
    "serviceBusTopicSubscriptionName": {
      "type": "string",
      "metadata": {
        "description": "Name of the Service Bus Topic Subscription"
      }
    }
  },
  "variables": {
    "sbVersion": "2015-08-01"
  },
  "resources": [
    {
      "apiVersion": "[variables('sbVersion')]",
      "name": "[parameters('serviceBusNamespaceName')]",
      "type": "Microsoft.ServiceBus/namespaces",
      "location": "[resourceGroup().location]",
      "properties": {
      },
      "resources": [
        {
            "apiVersion": "[variables('sbVersion')]",
            "name": "[parameters('serviceBusTopicName')]",
            "type": "Topics",
            "dependsOn": [
                "[concat('Microsoft.ServiceBus/namespaces/', parameters('serviceBusNamespaceName'))]"
            ],
            "properties": {
                "path": "[parameters('serviceBusTopicName')]"
            },
            "resources": [
                {
                    "apiVersion": "[variables('sbVersion')]",
                    "name": "[parameters('serviceBusTopicSubscriptionName')]",
                    "type": "Subscriptions",
                    "dependsOn": [
                        "[parameters('serviceBusTopicName')]"
                    ],
                    "properties": {
                    },
                    "resources": [
                    ]
                }
            ]
        }
      ]
    }
  ],
  "outputs": {
  }
}

As you can see, this full ARM Template has 3 parameters. Using these parameters you can explicitly specify the names to use for the Azure Service Bus Namespace, Topic and Subscription when deploying into Azure.
If you have any questions regarding any article within this series, please post a comment on that specific post. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Chris Pietschmann

Chris is an Azure Solutions Architect with more than 14 years of experience building Enterprise systems using a wide array of Microsoft technologies, including Bing Maps and the Microsoft Azure cloud. He has been a 5 time recipient of the Microsoft MVP award. Chris has successfully implemented numerous Enterprise solutions on the Microsoft Azure Cloud Platform since its initial release all the way back in 2010.

More from this Author

Categories
Follow Us
TwitterLinkedinFacebookYoutubeInstagram