Use Azure ARM Template to create Service Bus Topic

2020-02-29 11:30发布

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": {
  }
}

6条回答
做个烂人
2楼-- · 2020-02-29 12:07

Currently, ARM Templates do not support creating/managing Azure Service Bus Topic Subscription Filters.

查看更多
一夜七次
3楼-- · 2020-02-29 12:14

Use the Service Bus Explorer app. If you already know what SQL filter that you want to create, I suggest you to download and use this application. Download it from here

  1. You only need to connect to you service bus, the add rules to the subscriptions.
  2. Then, go to Azure portal and retrieve the ARM template for that particular service bus.
  3. You'll be able to see how the SQL filters are built.

This is how you Add Rules to a subscription:

enter image description here

This is how you view the Rules that you've created:

enter image description here

It's pretty simple to use the Service Bus Explorer app. Since it's user interactive, you can always configure your service bus then go to Azure Portal to retrieve the ARM template.

查看更多
家丑人穷心不美
4楼-- · 2020-02-29 12:15

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.

查看更多
再贱就再见
5楼-- · 2020-02-29 12:17

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'" } } }]

查看更多
三岁会撩人
6楼-- · 2020-02-29 12:20

The Subscription syntax to add a Sql filter has changed recently.

<snip>
"apiVersion": "2017-04-01",
"name": "[parameters('serviceBusSubscriptionName')]",
<snip>
"resources": [
    {
      "apiVersion": "2017-04-01",
      "name": "[parameters('serviceBusRuleName')]",
      "type": "Rules",
      "dependsOn": [
        "[parameters('serviceBusSubscriptionName')]"
      ],
      "properties": {
        "filterType": "SqlFilter",
        "sqlFilter": {
          "sqlExpression": "FilterTag = 'true'",
          "requiresPreprocessing": "false"
        },
        "action": {
          "sqlExpression": "set FilterTag = 'true'"
        }
      }
    }
]

You can find the latest examples in this ARM template:
https://github.com/Azure/azure-quickstart-templates/blob/master/201-servicebus-create-topic-subscription-rule/azuredeploy.json

查看更多
可以哭但决不认输i
7楼-- · 2020-02-29 12:24

This is now possible as per the following quick start template which illustrates adding a SQL Filter :

https://github.com/Azure/azure-quickstart-templates/blob/master/201-servicebus-create-topic-subscription-rule/azuredeploy.json

Also if you are looking to add a Correlation Filter via ARM, I've been able to do so by setting the Rules resource as follows:

 "resources": [
    {
      "apiVersion": "[variables('sbVersion')]",
      "name": "$Default",
      "type": "Rules",
      "dependsOn": [
        "[parameters('serviceBusSubscriptionName')]"
      ],
      "properties": {
        "filter": {
          "correlationId": "[parameters('correlationId')]"
        }
      }
    }
  ]
查看更多
登录 后发表回答