1

I am trying to automate the infrastructure of my application. As part of that, I am creating Service Bus related resources (Namespace, Topics, Subscriptions) only if they do not exist. My subscriptions will only have 1 rule. So every time the script runs, it will delete all existing rules and create this rule from scratch.

Here's the pseudo code I am writing:

$topic = Get-AzServiceBusTopic -Name $TopicName -NamespaceName $NamespaceName -ResourceGroupName $ResourceGroupName -ErrorVariable notPresent -ErrorAction SilentlyContinue
if ($null -eq $topic -or $notPresent)
{
    $topic = New-AzServiceBusTopic -Name TopicName -NamespaceName NamespaceName -ResourceGroupName ResourceGroupName -ErrorAction Stop
}
$subscription = Get-AzServiceBusSubscription -Name $SubscriptionName -TopicName $TopicName -NamespaceName $NamespaceName -ResourceGroupName $ResourceGroupName -ErrorVariable notPresent -ErrorAction SilentlyContinue
if ($null -eq $subscription -or $notPresent)
{
    $subscription = New-AzServiceBusSubscription -Name SubscriptionName -TopicName TopicName -NamespaceName NamespaceName -ResourceGroupName ResourceGroupName -ErrorAction Stop
}
# forcefully delete existing rules
Get-AzServiceBusRule -SubscriptionName SubscriptionName -TopicName TopicName -NamespaceName NamespaceName -ResourceGroupName ResourceGroupName | Remove-AzServiceBusRule
# create rule
$rule = New-AzServiceBusRule -Name "`$Default"  -SubscriptionName SubscriptionName -TopicName TopicName -NamespaceName NamespaceName -ResourceGroupName ResourceGroupName -FilterType SqlFilter -SqlExpression SqlFilterExpression -ErrorAction Stop

Randomly I am seeing New-AzServiceBusRule Cmdlet call fails with the following error:

The messaging entity 'namespacename:Topic:topicname|subscriptionname|$Default' already exists. To know more visit | https://aka.ms/sbResourceMgrExceptions. TrackingId:823eeae9-8776-46e7-90de-5ec305e14bb5_B26, SystemTracker:NoSystemTracker, | Timestamp:2023-05-13T10:33:37

According to the documentation of New-AzServiceBusRule, the cmdlet either creates a new rule and updates an existing rule. If that is the case, then why am I getting the resource exists exception.

I even tried waiting for a second before deleting all existing rules and creating the new rule but that did not help either.

Interesting thing is that it happens randomly. For some of the Subscriptions and Rules, the code works just fine and then randomly for one odd Subscription, it would fail. At times, it will not fail at all!

How can I prevent this from happening?

Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241

1 Answers1

1

The messaging entity 'namespacename:Topic:topicname|subscriptionname|$Default' already exists.

This error shouldn't come, as per Document and my analysis below is clearly says that this error will not come : With $Deafult:

enter image description here

enter image description here

enter image description here

One can clearly see I hadn't received any error for different rules too that are already existing with me.

If whenever I see these errors in other commands i integrate below commands into my script:

$emo=Get-AzServiceBusRule -SubscriptionName "rithwik" -TopicName "rithwik" -NamespaceName "rithwik1" -ResourceGroupName "rgname"
foreach ($e in $emo.Name)
{
    Remove-AzServiceBusRule -SubscriptionName "rithwik" -TopicName "rithwik" -NamespaceName "rithwik1" -ResourceGroupName "rg name" -Name $e
}
$rule = New-AzServiceBusRule -Name "vammo"  -SubscriptionName "rithwik" -TopicName "rithwik" -NamespaceName "rithwik1" -ResourceGroupName "rgname" -FilterType SqlFilter -SqlExpression '1=1' -ErrorAction Stop

Even with $default not getting any error :

enter image description here

If the issue still persists , I would suggest you to raise a support request.

RithwikBojja
  • 5,069
  • 2
  • 3
  • 7
  • 1
    Thank you for the answer. I will wait to hear someone from Service Bus team to look into this otherwise I will raise a support ticket as suggested by you. – Gaurav Mantri May 15 '23 at 05:15