Project Overview
Azure Service Bus Namespace: A container for Service Bus resources, allowing you to manage and organize multiple messaging components, such as topics and queues.
Service Bus Topic: A publish-subscribe messaging pattern where messages can be sent to a topic and delivered to multiple subscribers.
Shared Access Policies: Configured to manage access rights for sending, receiving, and managing messages on topics. Pay attention that you can only create 12 Shared Access Policies by topic.
Prerequisites
- An Azure account.
- Azure CLI installed and configured on your machine.
- Terraform installed on your machine.
- Azure Subscription ID for the Terraform provider configuration.
Step 1: Create a Project Directory
Create a new directory for your Terraform project:
mkdir azure-service-bus-example
cd azure-service-bus-example
Step 2: Create a main.tf
, variables.tf
and terraform.tfstate
Create a file named main.tf
in your project directory with the following content:
# Configure the Azure provider
provider "azurerm" {
features {}
subscription_id = var.subscription_id # Use a variable for subscription ID
}
# Create a resource group
resource "azurerm_resource_group" "resourcegroup" {
name = var.resource_group_name
location = var.location
}
# Create an Azure Service Bus namespace
resource "azurerm_servicebus_namespace" "namespace" {
name = var.service_bus_namespace_name
location = azurerm_resource_group.resourcegroup.location
resource_group_name = azurerm_resource_group.resourcegroup.name
sku = "Premium"
}
# Create a Service Bus Topic
resource "azurerm_servicebus_topic" "topic" {
name = var.service_bus_topic_name
namespace_id = azurerm_servicebus_namespace.namespace.id
max_size_in_megabytes = 1024
requires_duplicate_detection = true
support_ordering = true
}
# Create a Subscription for the Topic
resource "azurerm_servicebus_subscription" "subscription" {
name = var.service_bus_subscription_name
topic_id = azurerm_servicebus_topic.topic.id
max_delivery_count = 10
lock_duration = "PT5M"
}
# Create a Shared Access Policy for the topic
resource "azurerm_servicebus_topic_authorization_rule" "rule" {
name = "${var.service_bus_subscription_name}-policy"
topic_id = azurerm_servicebus_topic.topic.id
listen = true
}
# Outputs
output "topic_name" {
value = azurerm_servicebus_topic.topic.name
}
output "subscription_name" {
value = azurerm_servicebus_subscription.subscription.name
}
Add a file named variables.tf
in your project directory for variables:
# Variables
variable "subscription_id" {
description = "Azure subscription ID"
type = string
}
variable "resource_group_name" {
description = "Name of the resource group"
type = string
default = "resource-group-name" # Default value can be overridden
}
variable "location" {
description = "Azure location for the resources"
type = string
default = "West Europe" # Default location
}
variable "service_bus_namespace_name" {
description = "Name of the Service Bus namespace"
type = string
default = "servicebus-namespace-name" # Default namespace name
}
variable "service_bus_topic_name" {
description = "Name of the Service Bus topic"
type = string
default = "topic-name" # Default topic name
}
variable "service_bus_subscription_name" {
description = "Name of the Service Bus subscription"
type = string
default = "subscription-name" # Default subscription name
}
Create a file named terraform.tfstate
in your project directory for providing informations :
# Replace with your actual subscription ID
subscription_id = "11111111-2222-3333-4444-555555555555"
resource_group_name = "my-custom-resource-group"
location = "West Europe"
service_bus_namespace_name = "my-servicebus-namespace"
service_bus_topic_name = "my-topic"
service_bus_subscription_name = "my-subscription"
Step 3: Initialize Terraform
Run the following command to initialize your Terraform project and validate your project :
terraform init
terraform validate
Step 4: Plan you infrastructure
To generate a plan, run the following command in your terminal:
terraform plan
Step 5: Create the Infrastructure
Run the following command to create the resources defined in main.tf
:
terraform apply
Terraform will prompt you to confirm the changes. Type yes
to proceed.
After the infrastructure is created, you can access the Service Bus topic using the Azure Portal or via the Azure CLI.
Step 6: Clean Up
To delete the resources created by Terraform, run:
terraform destroy
Conclusion
In this example, you set up an Azure Service Bus namespace and a topic using Terraform. This setup provides a foundation for building scalable applications that communicate via topic, enabling decoupling of services and handling of asynchronous workloads.
Top comments (0)