|
| 1 | +--- |
| 2 | +title: Create Service Bus Topics and Subscriptions - Azure PowerShell |
| 3 | +description: Create an Azure Service Bus topic and subscriptions with message filters using Azure PowerShell. Follow step-by-step commands in Azure Cloud Shell. |
| 4 | +ms.date: 04/06/2026 |
| 5 | +ms.topic: quickstart |
| 6 | +author: spelluru |
| 7 | +ms.author: spelluru |
| 8 | +ms.devlang: azurepowershell |
| 9 | +ms.custom: |
| 10 | + - mode-api |
| 11 | + - devx-track-azurepowershell |
| 12 | +ai-usage: ai-assisted |
| 13 | + |
| 14 | +#customer intent: As a developer, I want to create a Service Bus topic with subscriptions using Azure PowerShell so that I can set up publish/subscribe messaging for my distributed application. |
| 15 | +--- |
| 16 | + |
| 17 | +# Quickstart: Create a Service Bus topic and subscriptions using Azure PowerShell |
| 18 | + |
| 19 | +In this quickstart, you use Azure PowerShell to create a Service Bus topic and then create subscriptions to that topic. You also create filters for each subscription to route messages based on custom properties. |
| 20 | + |
| 21 | +## What are Service Bus topics and subscriptions? |
| 22 | + |
| 23 | +Service Bus topics and subscriptions support a *publish/subscribe* messaging communication model. When using topics and subscriptions, components of a distributed application don't communicate directly with each other; instead they exchange messages via a topic, which acts as an intermediary. |
| 24 | + |
| 25 | +:::image type="content" source="./media/service-bus-java-how-to-use-topics-subscriptions/service-bus-topics-subscriptions.png" alt-text="Diagram that shows publishers sending messages to a Service Bus topic, which distributes copies to multiple subscriber queues." lightbox="./media/service-bus-java-how-to-use-topics-subscriptions/service-bus-topics-subscriptions.png"::: |
| 26 | + |
| 27 | +In contrast with Service Bus queues, in which each message is processed by a single consumer, topics and subscriptions provide a one-to-many form of communication, using a publish/subscribe pattern. It's possible to register multiple subscriptions to a topic. When a message is sent to a topic, it's then made available to each subscription to handle/process independently. A subscription to a topic resembles a virtual queue that receives copies of the messages that were sent to the topic. You can optionally register filter rules for a topic on a per-subscription basis, which allows you to filter or restrict which messages to a topic are received by which topic subscriptions. |
| 28 | + |
| 29 | +Service Bus topics and subscriptions enable you to scale to process a large number of messages across a large number of users and applications. |
| 30 | + |
| 31 | +## Prerequisites |
| 32 | + |
| 33 | +- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/pricing/purchase-options/azure-account?cid=msft_learn). |
| 34 | +- Azure Cloud Shell or Azure PowerShell installed locally. This quickstart uses Azure Cloud Shell. |
| 35 | + |
| 36 | +## Create a Service Bus topic and subscriptions |
| 37 | + |
| 38 | +Each subscription to a topic can receive a copy of each message. Topics are fully protocol and semantically compatible with Service Bus queues. Service Bus topics support a wide array of selection rules with filter conditions, with optional actions that set or modify message properties. Each time a rule matches, it produces a message. |
| 39 | + |
| 40 | +1. Sign in to the [Azure portal](https://portal.azure.com). |
| 41 | + |
| 42 | +1. Launch Azure Cloud Shell by selecting the icon shown in the following image. |
| 43 | + |
| 44 | + :::image type="content" source="~/reusable-content/ce-skilling/azure/media/cloud-shell/launch-cloud-shell-button.png" alt-text="Button to launch the Azure Cloud Shell." border="false" link="https://shell.azure.com"::: |
| 45 | + |
| 46 | +1. In the bottom Cloud Shell window, switch from **Bash** to **PowerShell**. |
| 47 | + |
| 48 | + :::image type="content" source="./media/service-bus-quickstart-powershell/cloud-power-shell.png" alt-text="Screenshot of the Cloud Shell toolbar showing how to switch from Bash to PowerShell mode."::: |
| 49 | + |
| 50 | +1. Run the following command to create an Azure resource group. Update the resource group name and the location if you want. |
| 51 | + |
| 52 | + ```azurepowershell-interactive |
| 53 | + New-AzResourceGroup -Name MyResourceGroup -Location eastus |
| 54 | + ``` |
| 55 | +
|
| 56 | +1. Run the following command to create a Service Bus messaging namespace. Update the name of the namespace to be unique. |
| 57 | +
|
| 58 | + ```azurepowershell-interactive |
| 59 | + $namespaceName = "MyNameSpace$(Get-Random)" |
| 60 | + New-AzServiceBusNamespace -ResourceGroupName MyResourceGroup -Name $namespaceName -Location eastus |
| 61 | + ``` |
| 62 | +
|
| 63 | +1. Run the following command to create a topic in the namespace. |
| 64 | +
|
| 65 | + ```azurepowershell-interactive |
| 66 | + New-AzServiceBusTopic -ResourceGroupName MyResourceGroup -NamespaceName $namespaceName -Name MyTopic |
| 67 | + ``` |
| 68 | +
|
| 69 | +1. Create the first subscription to the topic. |
| 70 | +
|
| 71 | + ```azurepowershell-interactive |
| 72 | + New-AzServiceBusSubscription -ResourceGroupName MyResourceGroup -NamespaceName $namespaceName -TopicName MyTopic -Name S1 |
| 73 | + ``` |
| 74 | +
|
| 75 | +1. Create the second subscription to the topic. |
| 76 | +
|
| 77 | + ```azurepowershell-interactive |
| 78 | + New-AzServiceBusSubscription -ResourceGroupName MyResourceGroup -NamespaceName $namespaceName -TopicName MyTopic -Name S2 |
| 79 | + ``` |
| 80 | +
|
| 81 | +1. Create the third subscription to the topic. |
| 82 | +
|
| 83 | + ```azurepowershell-interactive |
| 84 | + New-AzServiceBusSubscription -ResourceGroupName MyResourceGroup -NamespaceName $namespaceName -TopicName MyTopic -Name S3 |
| 85 | + ``` |
| 86 | +
|
| 87 | +1. Create a filter on the first subscription with a filter using custom properties (`StoreId` is one of `Store1`, `Store2`, and `Store3`). |
| 88 | +
|
| 89 | + ```azurepowershell-interactive |
| 90 | + New-AzServiceBusRule -ResourceGroupName MyResourceGroup -NamespaceName $namespaceName -TopicName MyTopic -SubscriptionName S1 -Name MyFilter -SqlExpression "StoreId IN ('Store1','Store2','Store3')" |
| 91 | + ``` |
| 92 | +
|
| 93 | +1. Create a filter on the second subscription with a filter using customer properties (`StoreId = Store4`). |
| 94 | +
|
| 95 | + ```azurepowershell-interactive |
| 96 | + New-AzServiceBusRule -ResourceGroupName MyResourceGroup -NamespaceName $namespaceName -TopicName MyTopic -SubscriptionName S2 -Name MySecondFilter -SqlExpression "StoreId = 'Store4'" |
| 97 | + ``` |
| 98 | +
|
| 99 | +1. Create a filter on the third subscription with a filter using customer properties (`StoreId` not in `Store1`, `Store2`, `Store3`, or `Store4`). |
| 100 | +
|
| 101 | + ```azurepowershell-interactive |
| 102 | + New-AzServiceBusRule -ResourceGroupName MyResourceGroup -NamespaceName $namespaceName -TopicName MyTopic -SubscriptionName S3 -Name MyThirdFilter -SqlExpression "StoreId NOT IN ('Store1','Store2','Store3','Store4')" |
| 103 | + ``` |
| 104 | +
|
| 105 | +1. Run the following command to get the primary connection string for the namespace. You use this connection string to connect to the topic and send and receive messages. |
| 106 | +
|
| 107 | + ```azurepowershell-interactive |
| 108 | + Get-AzServiceBusKey -ResourceGroupName MyResourceGroup -Namespace $namespaceName -Name RootManageSharedAccessKey |
| 109 | + ``` |
| 110 | +
|
| 111 | + Note down the connection string and the topic name. You use them to send and receive messages. |
| 112 | +
|
| 113 | +## Clean up resources |
| 114 | +
|
| 115 | +When you no longer need the Service Bus namespace, topic, and subscriptions, delete them to avoid incurring charges. Run the following command to delete the resource group and all its resources: |
| 116 | +
|
| 117 | +```azurepowershell-interactive |
| 118 | +Remove-AzResourceGroup -Name MyResourceGroup |
| 119 | +``` |
| 120 | + |
| 121 | +## Next steps |
| 122 | + |
| 123 | +To learn how to send messages to a topic and receive those messages via a subscription, see the following article and select your programming language in the TOC. |
| 124 | + |
| 125 | +> [!div class="nextstepaction"] |
| 126 | +> [Publish and subscribe for messages](service-bus-dotnet-how-to-use-topics-subscriptions.md) |
0 commit comments