We have used terraform for managing azure service bus for ages and I really like the approach. Ive been meaning to blog more about it for a while and its on my backlog. One of the things I need to do to save myself some time is make a few notes on some of the common snippets I regularly need, in particular for message filters for a subscription.

Subscribe to Message on a Topic via Label

In the below example I want to subscribe to a message on a topic and have a filter so I only get messages where the label is ORDER_LINE_PUBLISHED. The correlation filter below lets be define the property of interest.

resource "azurerm_servicebus_subscription_rule" "my_topic_rule_for_systema" {
  name                = "ORDER_LINE_PUBLISHED"
  topic_name          = azurerm_servicebus_subscription.my_topic.topic_name
  subscription_name   = azurerm_servicebus_subscription.my_subscription.name
 

  resource_group_name = data.azurerm_servicebus_namespace.my_namespace.resource_group_name
  namespace_name      = data.azurerm_servicebus_namespace.my_namespace.name
  filter_type         = "CorrelationFilter"
  correlation_filter {
    label          = "ORDER_LINE_PUBLISHED"
  }
}

Set Properties on a message via a Topic Subscription Rule

In this scenario I have a subscription to a topic which will forward a message to a queue for messages to be processed. When I define the subscription rule then I want to set an action to add some additional properties to the forwarded message. The below snippet defines the config of the rule which is associated to the subscription. The key bit below is the action property which sets the properties.

resource "azurerm_servicebus_subscription_rule" "my_topic_rule_for_system_a" { 
  name                = "All"
  topic_name          = azurerm_servicebus_subscription.my_topic.topic_name
  subscription_name   = azurerm_servicebus_subscription.my_subscription.name

  resource_group_name = data.azurerm_servicebus_namespace.my_namespace.resource_group_name
  namespace_name      = data.azurerm_servicebus_namespace.my_namespace.name
  filter_type         = "SqlFilter"
  sql_filter          = "1 = 1"
  action              = "SET From = 'SystemA'; SET Maturity = 'Raw'"
}

Subscribe to Message with a Custom Property

In this case I have used a SQL filter which looks for a property on a message called VOLUMETRICS and if that property has the string value TRUE then I will get the message.

resource "azurerm_servicebus_subscription_rule" "my_topic_rule_for_systema" {
  name                = "SystemA"
  topic_name          = azurerm_servicebus_subscription.my_topic.topic_name
  subscription_name   = azurerm_servicebus_subscription.my_subscription.name
  

  resource_group_name = data.azurerm_servicebus_namespace.my_namespace.resource_group_name
  namespace_name      = data.azurerm_servicebus_namespace.my_namespace.name

  filter_type         = "SqlFilter"
  sql_filter          = "[VOLUMETRICS] = 'TRUE'"
}

Subscribe to a Message where custom Property not equal to value

In this example I am using a property called InitiatorSystem and if the value is NOT equal to the string 2 then I will get the message.

resource "azurerm_servicebus_subscription_rule" "my_topic_rule_for_systema" {
  name                = "SystemA"
  resource_group_name = data.azurerm_servicebus_namespace.my_namespace.resource_group_name
  namespace_name      = data.azurerm_servicebus_namespace.my_namespace.name
  topic_name          = azurerm_servicebus_subscription.my_topic.topic_name
  subscription_name   = azurerm_servicebus_subscription.my_subscription.name
  filter_type         = "SqlFilter"
  sql_filter          = "InitiatorSystem != '2'"
}

Subscribe to a Message where a property is in a list of values

In this example I want my subscription to get messages where the custom property is 1 of a list of values.

resource "azurerm_servicebus_subscription_rule" "my_topic_rule_for_systema" {
  name                = "SystemA"
  resource_group_name = data.azurerm_servicebus_namespace.my_namespace.resource_group_name
  namespace_name      = data.azurerm_servicebus_namespace.my_namespace.name
  topic_name          = azurerm_servicebus_topic.my_topic.name
  subscription_name   = azurerm_servicebus_subscription.my_subscription.name
  filter_type         = "SqlFilter"
  sql_filter          = "MyProperty IN ('VALUE1','VALUE2','VALUE3')"
}

Combine Custom and System Properties

In this example I want to combine the use of custom and system properties using the SQL Filter. In this case I want a message if the label is TYPE1 OR TYPE2 and if the custom InitiatorSystem property is NOT 2.

resource "azurerm_servicebus_subscription_rule" "my_topic_rule_for_systema" {
  name                = "SystemA"
  resource_group_name = data.azurerm_servicebus_namespace.my_namespace.resource_group_name
  namespace_name      = data.azurerm_servicebus_namespace.my_namespace.name
  topic_name          = azurerm_servicebus_topic.my_topic.name
  subscription_name   = azurerm_servicebus_subscription.my_subscription.name
  filter_type         = "SqlFilter"
  sql_filter          = "sys.Label = 'TYPE1' OR sys.Label = 'TYPE2' AND InitiatorSystem != '2'"
}

The above example could also be done with the list approach like one of the earlier examples too.



resource "azurerm_servicebus_subscription_rule" "my_topic_rule_for_systema" {
  name                = "SystemA"
  resource_group_name = data.azurerm_servicebus_namespace.my_namespace.resource_group_name
  namespace_name      = data.azurerm_servicebus_namespace.my_namespace.name
  topic_name          = azurerm_servicebus_topic.my_topic.name
  subscription_name   = azurerm_servicebus_subscription.my_subscription.name
  filter_type         = "SqlFilter"
  sql_filter          = "sys.Label IN ('TYPE1','TYPE2') AND InitiatorSystem != '2'"
}

 

Buy Me A Coffee