Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Azure ARM Template to create Service Bus Topic Subscription with Sql Filter?

I've been able to figure out how to setup an Azure ARM Template that creates/manages an Azure Service Bus Namespace, Topic and Subscription to receive all messages. However, the Microsoft documentation is extremely lacking still on ARM Tempates, and I am unable to figure out how to define a SqlFilter for the Subscription within the template that you can manage using the .NET SDK.

Does anyone know how to add a Sql Filter to a Service Bus Topic Subscription within an ARM Template?

Here's a link to the ARM Template I have for creating the Service Bus Topic and Subscription without Sql filter:

https://github.com/crpietschmann/azure-quickstart-templates/blob/101-servicebus-topic-subscription/101-servicebus-topic-subscription/azuredeploy.json

Also, here's the source of the ARM Template I'm referring to:

{
  "$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": {
  }
}
like image 406
Chris Pietschmann Avatar asked Dec 28 '25 13:12

Chris Pietschmann


2 Answers

Just add following into your subscription resource to create SQL Filter and Action:

,"resources": [{ "apiVersion": "[variables('sbVersion')]", "name": "$Default", "type": "Rules", "dependsOn": ["[parameters('serviceBusSubscriptionName')]"], "properties": { "filterType": "SqlFilter", "sqlFilter": { "sqlExpression": "1=1", "requiresPreprocessing": false }, "action": { "sqlExpression": "set something = 'something'" } } }]

like image 74
Nido Avatar answered Dec 31 '25 04:12

Nido


A Sql Filter should be inside a Rule, so we should create a rule within the Service Bus Topic Subscription. For example:

      "resources": [
        {
          "apiVersion": "[variables('sbVersion')]",
          "name": "[parameters('serviceBusTopicSubscriptionName')]",
          "type": "Subscriptions",
          "dependsOn": [
            "[parameters('serviceBusTopicName')]"
          ],
          "properties": {
          },
          "resources": [
            {
              "apiVersion": "[variables('sbVersion')]",
              "name": "[parameters('serviceBusTopicSubscriptionRuleName')]",
              "type": "Rules",
              "dependsOn": [
                "[parameters('serviceBusTopicSubscriptionName')]"
              ],
              "properties": {
              },
              "resources": [
              ]
            }
          ]
        }
      ]

I have tried to deploy this template, but I get the following error:

New-AzureRmResourceGroupDeployment : InvalidTemplate: Deployment template validation failed: 'The template resource 'Microsoft.ServiceBus/namespaces/<serviceBusNamespaceName>/Topics/<serviceBusTopicName>/Subscriptions/<serviceBusTopicSubscriptionName>' cannot reference itself. Please see http://aka.ms/arm-template-expressions/#reference for usage details.'.
At line:1 char:1
+ New-AzureRmResourceGroupDeployment -Name ServiceBusTest -ResourceGrou ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [New-AzureRmResourceGroupDeployment], CloudException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Resources.NewAzureResourceGroupDeploymentCommand

From the error message, "'The template resource cannot reference itself", I am guessing that creating Sql Filter for a Topic Subscription is not yet implemented in ARM template.

After some more diggings, I believe that Topic Subscription Rule is not manageable by Resource Manager yet. Here is the things I tried.

  1. I use this PowerShell script to create a Topic Subscription with a rule. I have done some modification to the script by adding a name to the rule, $RuleDescription.Name = "rule1".

  2. The Topic Subscription is successfully created, and I can use the following PowerShell command to get the Topic Subscription.

    Get-AzureRmResource -ResourceGroupName Default-ServiceBus-EastUS `
                       -ResourceType Microsoft.ServiceBus/namespaces/topics/Subscriptions `
                       -ResourceName <namespace>/<topic>/<subscription> `
                       -ApiVersion 2014-09-01
    
  3. When I try to get the Topic Subscription Rule with a similar PowerShell command:

    Get-AzureRmResource -ResourceGroupName Default-ServiceBus-EastUS `
                 -ResourceType Microsoft.ServiceBus/namespaces/topics/Subscriptions/Rules `
                 -ResourceName <namespace>/<topic>/<subscription>/rule1 `
                 -ApiVersion 2014-09-01
    

    I get the following error:

    No HTTP resource was found that matches the request URI
    'https://sbgm.windows.net/subscriptions/<subscriptionid>/resourceGroups/Default-ServiceBus-EastUS/providers/Microsoft.ServiceBus/namespaces/<namespace>/topics/<topic>/Subscriptions/<subscription>/Rules/rule1?api-version=2014-09-01'
    
  4. However, if I use $NamespaceManager.GetRules($TopicPath,$Name), I do get the above rule successfully. That means the rule is created successfully.

like image 43
Jack Zeng Avatar answered Dec 31 '25 02:12

Jack Zeng



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!