0
0
Azurecloud~5 mins

Service Bus queues concept in Azure - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes apps need to talk to each other without being connected at the same time. Service Bus queues help by holding messages safely until the receiving app is ready to get them.
When you want to send orders from a website to a processing system without losing any orders.
When different parts of your app run at different speeds and need a way to pass messages reliably.
When you want to make sure messages are processed one at a time in the order they were sent.
When you need to keep messages safe even if the receiving app is temporarily down.
When you want to separate sending and receiving parts of your system to make it easier to manage.
Config File - main.tf
main.tf
provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "East US"
}

resource "azurerm_servicebus_namespace" "example" {
  name                = "examplesbnamespace"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  sku                 = "Standard"
}

resource "azurerm_servicebus_queue" "example" {
  name                = "examplequeue"
  namespace_name      = azurerm_servicebus_namespace.example.name
  resource_group_name = azurerm_resource_group.example.name
  max_size_in_megabytes = 1024
  enable_partitioning = true
}

This Terraform file creates an Azure resource group, a Service Bus namespace, and a queue inside that namespace.

resource_group: Holds all resources in one place.

servicebus_namespace: The container for queues and topics.

servicebus_queue: The queue where messages are stored until processed.

Partitioning helps scale the queue for better performance.

Commands
This command sets up Terraform in the current folder, downloading the Azure provider so you can create resources.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/azurerm... - Installing hashicorp/azurerm v3.64.0... - Installed hashicorp/azurerm v3.64.0 (signed by HashiCorp) Terraform has been successfully initialized!
This command creates the resource group, Service Bus namespace, and queue in Azure as defined in the config file.
Terminal
terraform apply -auto-approve
Expected OutputExpected
azurerm_resource_group.example: Creating... azurerm_resource_group.example: Creation complete after 2s azurerm_servicebus_namespace.example: Creating... azurerm_servicebus_namespace.example: Creation complete after 15s azurerm_servicebus_queue.example: Creating... azurerm_servicebus_queue.example: Creation complete after 5s Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
-auto-approve - Skip manual approval to apply changes immediately
This Azure CLI command checks the details of the queue to confirm it was created and see its settings.
Terminal
az servicebus queue show --resource-group example-resources --namespace-name examplesbnamespace --name examplequeue
Expected OutputExpected
{ "name": "examplequeue", "maxSizeInMegabytes": 1024, "enablePartitioning": true, "status": "Active", "lockDuration": "PT1M" }
--resource-group - Specifies the Azure resource group name
--namespace-name - Specifies the Service Bus namespace
--name - Specifies the queue name
Key Concept

If you remember nothing else from this pattern, remember: Service Bus queues hold messages safely until the receiver is ready, helping apps talk smoothly even if they work at different times.

Common Mistakes
Trying to send messages to a queue before it is created.
The messages will fail because the queue does not exist yet.
Always create and verify the queue first before sending messages.
Not enabling partitioning on large queues.
Without partitioning, the queue can become slow or hit size limits.
Enable partitioning to allow the queue to scale and handle more messages.
Using the wrong resource group or namespace name in commands.
Commands will fail because they cannot find the queue.
Double-check resource group and namespace names before running commands.
Summary
Use Terraform to create an Azure resource group, Service Bus namespace, and queue.
Run 'terraform apply' to deploy the queue and then verify it with Azure CLI.
Service Bus queues safely store messages until the receiving app is ready to process them.