Edit

Share via


RabbitMQ trigger for Azure Functions overview

Use the RabbitMQ trigger to respond to messages from a RabbitMQ queue.

Note

The RabbitMQ bindings are only fully supported on Elastic Premium and Dedicated (App Service) plans. Flex Consumption and Consumption plans aren't yet supported.

RabbitMQ bindings aren't supported by the Azure Functions v1.x runtime.

For information on setup and configuration details, see the overview.

Example

You can create a C# function by using one of the following C# modes:

  • Isolated worker model: Compiled C# function that runs in a worker process that's isolated from the runtime. An isolated worker process is required to support C# functions running on long-term support (LTS) and non-LTS versions for .NET and the .NET Framework.
  • In-process model: Compiled C# function that runs in the same process as the Azure Functions runtime.
  • C# script: Used primarily when you create C# functions in the Azure portal.
[Function(nameof(RabbitMQFunction))]
[RabbitMQOutput(QueueName = "destinationQueue", ConnectionStringSetting = "RabbitMQConnection")]
public static string Run([RabbitMQTrigger("queue", ConnectionStringSetting = "RabbitMQConnection")] string item,
    FunctionContext context)
{
    var logger = context.GetLogger(nameof(RabbitMQFunction));

    logger.LogInformation(item);

    var message = $"Output message created at {DateTime.Now}";
    return message;
}

The following Java function uses the @RabbitMQTrigger annotation from the Java RabbitMQ types to describe the configuration for a RabbitMQ queue trigger. The function grabs the message placed on the queue and adds it to the logs.

@FunctionName("RabbitMQTriggerExample")
public void run(
    @RabbitMQTrigger(connectionStringSetting = "rabbitMQConnectionAppSetting", queueName = "queue") String input,
    final ExecutionContext context)
{
    context.getLogger().info("Java HTTP trigger processed a request." + input);
}

The following example shows a RabbitMQ trigger binding in a function.json file and a JavaScript function that uses the binding. The function reads and logs a RabbitMQ message.

Here's the binding data in the function.json file:

{​​
    "bindings": [
        {​​
            "name": "myQueueItem",
            "type": "rabbitMQTrigger",
            "direction": "in",
            "queueName": "queue",
            "connectionStringSetting": "rabbitMQConnectionAppSetting"
        }​​
    ]
}​​

Here's the JavaScript script code:

module.exports = async function (context, myQueueItem) {​​
    context.log('JavaScript RabbitMQ trigger function processed work item', myQueueItem);
}​​;

The following example demonstrates how to read a RabbitMQ queue message via a trigger.

A RabbitMQ binding is defined in function.json where type is set to RabbitMQTrigger.

{​​
    "scriptFile": "__init__.py",
    "bindings": [
        {​​
            "name": "myQueueItem",
            "type": "rabbitMQTrigger",
            "direction": "in",
            "queueName": "queue",
            "connectionStringSetting": "rabbitMQConnectionAppSetting"
        }​​
    ]
}​​
import logging
import azure.functions as func

def main(myQueueItem) -> None:
    logging.info('Python RabbitMQ trigger function processed a queue item: %s', myQueueItem)

PowerShell examples aren't currently available.

Attributes

Both isolated worker process and in-process C# libraries use RabbitMQTriggerAttribute to define the function, where specific properties of the attribute depend on the extension version.

The attribute's constructor accepts these parameters:

Parameter Description
QueueName Name of the queue from which to receive messages.
HostName This parameter is no longer supported and is ignored. It will be removed in a future version.
ConnectionStringSetting The name of the app setting that contains the connection string for your RabbitMQ server. This setting only takes an app setting key name, you can't directly set a connection string value. For more information, see Connections.
UserNameSetting This parameter is no longer supported and is ignored. It will be removed in a future version.
PasswordSetting This parameter is no longer supported and is ignored. It will be removed in a future version.
Port Gets or sets the port used. Defaults to 0, which points to the RabbitMQ client's default port setting of 5672.

Annotations

The RabbitMQTrigger annotation allows you to create a function that runs when a RabbitMQ message is created.

The annotation supports the following configuration options:

Parameter Description
queueName Name of the queue from which to receive messages.
connectionStringSetting The name of the app setting that contains the connection string for your RabbitMQ server. This setting only takes an app setting key name, you can't directly set a connection string value. For more information, see Connections.
disableCertificateValidation Boolean value that can be set to true indicating that certificate validation should be disabled. Default value is false. Not recommended for production. Does not apply when SSL is disabled.

Configuration

The following table explains the binding configuration properties that you set in the function.json file.

function.json property Description
type Must be set to RabbitMQTrigger.
direction Must be set to in.
name The name of the variable that represents the queue in function code.
queueName Name of the queue from which to receive messages.
connectionStringSetting The name of the app setting that contains the connection string for your RabbitMQ server. This setting only takes an app setting key name, you can't directly set a connection string value. For more information, see Connections.
disableCertificateValidation Boolean value that can be set to true indicating that certificate validation should be disabled. Default value is false. Not recommended for production. Does not apply when SSL is disabled.

When you're developing locally, add your application settings in the local.settings.json file in the Values collection.

See the Example section for complete examples.

Usage

The parameter type supported by the RabbitMQ trigger depends on the C# modality used.

The RabbitMQ bindings currently support only string and serializable object types when running in an isolated process.

The queue message is available via context.bindings.<NAME> where <NAME> matches the name defined in function.json. If the payload is JSON, the value is deserialized into an object.

Connections

Important

The RabbitMQ binding doesn't support Microsoft Entra authentication and managed identities. You can use Azure Key Vault to centrally managed your RabbitMQ connection strings. To learn more, see Manage Connections.

Starting with version 2.x of the extension, hostName, userNameSetting, and passwordSetting are no longer supported to define a connection to the RabbitMQ server. You must instead use connectionStringSetting.

The connectionStringSetting property can only accept the name of a key-value pair in app settings. You can't directly set a connection string value in the binding.

For example, when you have set connectionStringSetting to rabbitMQConnection in your binding definition, your function app must have an app setting named rabbitMQConnection that returns either a connection value like amqp://myuser:***@contoso.rabbitmq.example.com:5672 or an Azure Key Vault reference.

When running locally, you must also have the key value for connectionStringSetting defined in your local.settings.json file. Otherwise, your app can't connect to the service from your local computer and an error occurs.

Dead letter queues

Dead letter queues and exchanges can't be controlled or configured from the RabbitMQ trigger. To use dead letter queues, pre-configure the queue used by the trigger in RabbitMQ. Refer to the RabbitMQ documentation.

Enable Runtime Scaling

In order for the RabbitMQ trigger to scale out to multiple instances, the Runtime Scale Monitoring setting must be enabled.

In the portal, this setting can be found under Configuration > Function runtime settings for your function app.

VNETToggle

In the Azure CLI, you can enable Runtime Scale Monitoring by using this command:

az resource update -resource-group <RESOURCE_GROUP> -name <APP_NAME>/config/web \
    --set properties.functionsRuntimeScaleMonitoringEnabled=1 \
    --resource-type Microsoft.Web/sites

Monitoring a RabbitMQ endpoint

To monitor your queues and exchanges for a certain RabbitMQ endpoint:

Related article