admin管理员组

文章数量:1292285

I'm using Azure and Terraform to manage a Service Bus with three queues: A, B, and C. I want to trigger an Azure Function (in .NET) only when Queue A sends a message to the Dead-Letter Queue (DLQ). The function should execute some logic, such as sending an HTTP request to an API to notify the user about a failure.

Terraform Configuration (Simplified) Here’s a simplified version of my Terraform configuration:

resource "azurerm_servicebus_namespace" "servicebus_namespace" {
  name                = "sb-bwp-${var.environment}-${var.location}-001"
  location            = var.location
  resource_group_name = azurerm_resource_group.resource_group.name
  sku                 = var.environment == "prod" ? "Standard" : "Basic"

  tags = merge(local.azure_common_tags, {
    service = "network",
    team    = "backend"
  })
}

resource "azurerm_servicebus_queue" "shared_servicebus_queue_a" {
  name         = "A"
  namespace_id = azurerm_servicebus_namespace.servicebus_namespace.id

  partitioning_enabled         = true
  batched_operations_enabled   = true
  requires_session             = false
  requires_duplicate_detection = var.environment == "prod"

  lock_duration                           = "PT1M"
  duplicate_detection_history_time_window = "PT10M"
  dead_lettering_on_message_expiration    = false
}

resource "azurerm_servicebus_queue" "shared_servicebus_queue_b" {
  name         = "B"
  namespace_id = azurerm_servicebus_namespace.servicebus_namespace.id

  partitioning_enabled         = true
  batched_operations_enabled   = true
  requires_session             = false
  requires_duplicate_detection = var.environment == "prod"

  lock_duration                           = "PT1M"
  duplicate_detection_history_time_window = "PT10M"
  dead_lettering_on_message_expiration    = false
}

resource "azurerm_servicebus_queue" "shared_servicebus_queue_c" {
  name         = "C"
  namespace_id = azurerm_servicebus_namespace.servicebus_namespace.id

  partitioning_enabled         = true
  batched_operations_enabled   = true
  requires_session             = false
  requires_duplicate_detection = var.environment == "prod"

  lock_duration                           = "PT1M"
  duplicate_detection_history_time_window = "PT10M"
  dead_lettering_on_message_expiration    = false
}

My Questions: How can I configure an Azure Function to trigger only when Queue A's Dead-Letter Queue receives a message? What is the best practice for setting up this event-driven architecture using Terraform? Should I use an Event Grid Subscription, or is there a better approach for handling DLQ triggers? How can I ensure that messages from Queue B and Queue C do not trigger the function? If anyone has experience with this setup, I’d appreciate your insights!

I'm using Azure and Terraform to manage a Service Bus with three queues: A, B, and C. I want to trigger an Azure Function (in .NET) only when Queue A sends a message to the Dead-Letter Queue (DLQ). The function should execute some logic, such as sending an HTTP request to an API to notify the user about a failure.

Terraform Configuration (Simplified) Here’s a simplified version of my Terraform configuration:

resource "azurerm_servicebus_namespace" "servicebus_namespace" {
  name                = "sb-bwp-${var.environment}-${var.location}-001"
  location            = var.location
  resource_group_name = azurerm_resource_group.resource_group.name
  sku                 = var.environment == "prod" ? "Standard" : "Basic"

  tags = merge(local.azure_common_tags, {
    service = "network",
    team    = "backend"
  })
}

resource "azurerm_servicebus_queue" "shared_servicebus_queue_a" {
  name         = "A"
  namespace_id = azurerm_servicebus_namespace.servicebus_namespace.id

  partitioning_enabled         = true
  batched_operations_enabled   = true
  requires_session             = false
  requires_duplicate_detection = var.environment == "prod"

  lock_duration                           = "PT1M"
  duplicate_detection_history_time_window = "PT10M"
  dead_lettering_on_message_expiration    = false
}

resource "azurerm_servicebus_queue" "shared_servicebus_queue_b" {
  name         = "B"
  namespace_id = azurerm_servicebus_namespace.servicebus_namespace.id

  partitioning_enabled         = true
  batched_operations_enabled   = true
  requires_session             = false
  requires_duplicate_detection = var.environment == "prod"

  lock_duration                           = "PT1M"
  duplicate_detection_history_time_window = "PT10M"
  dead_lettering_on_message_expiration    = false
}

resource "azurerm_servicebus_queue" "shared_servicebus_queue_c" {
  name         = "C"
  namespace_id = azurerm_servicebus_namespace.servicebus_namespace.id

  partitioning_enabled         = true
  batched_operations_enabled   = true
  requires_session             = false
  requires_duplicate_detection = var.environment == "prod"

  lock_duration                           = "PT1M"
  duplicate_detection_history_time_window = "PT10M"
  dead_lettering_on_message_expiration    = false
}

My Questions: How can I configure an Azure Function to trigger only when Queue A's Dead-Letter Queue receives a message? What is the best practice for setting up this event-driven architecture using Terraform? Should I use an Event Grid Subscription, or is there a better approach for handling DLQ triggers? How can I ensure that messages from Queue B and Queue C do not trigger the function? If anyone has experience with this setup, I’d appreciate your insights!

Share asked Feb 13 at 10:54 Tiago FerreiraTiago Ferreira 111 bronze badge 0
Add a comment  | 

2 Answers 2

Reset to default 1

You can create a Service Bus trigger for a dead-lettered queue the same way you make it for the active messages queue. We use this on a daily basis to manage the failed messages.

This is part of the function code and besides the app settings there is nothing specific regarding the deployment (no matter what tool you use).

Here is an example for a trigger in C#:

[Function("ServiceBusDeadLetter")]
public async Task RunServiceBusDeadLetterAsync(
    [ServiceBusTrigger("%ServiceBus.TopiName%", "%ServiceBus.SubscriptionName%" + "/$deadletterqueue", Connection = "ServiceBus.Connection")]
    ServiceBusReceivedMessage message, FunctionContext context, ServiceBusMessageActions messageActions)
{
    //do the job
}

There not a dead letter trigger that I am aware of, but you could poll. This will sound a bit strange and there are other alternative,

  1. Create logic app to send poll message to an new queue, say every min
  2. Trigger Azure Function on that new queue
  3. The new function polls the dead letter for the first queue.

Its crude and not ideal

本文标签: