After the Message Queue for RabbitMQ event source is integrated with Function Compute by using EventBridge, the Message Queue for RabbitMQ trigger or the RabbitMQ trigger can trigger associated functions. The functions can be used to perform custom operations on the messages that are published to Message Queue for RabbitMQ. This topic describes how to create RabbitMQ triggers, write functions, and test functions in the Function Compute console.

Overview

After you submit a request to create a trigger in the Function Compute console, Function Compute creates EventBridge resources that are named in the following formats based on the trigger configurations:
  • Event bus: RabbitMQ-Function name-Trigger name
  • Event source: RabbitMQ-Function name-Trigger name
  • Event rule: Service name-Function name-Trigger name
After the trigger is created, you can view information about the trigger in the Function Compute console. You can also view information about the created resources in the EventBridge console. When an event of the type specified by the event source is delivered to the event bus, the function that is associated with the trigger is triggered to execute once.
Note The names of the created EventBridge resources must meet naming rules. Otherwise, you cannot create the trigger. For more information, see Limits.

Usage notes

  • In EventBridge, the number of custom event buses in the same region cannot exceed 10 and the number of event rules that are created on each custom bus cannot exceed 10. If these limits are exceeded, you cannot create EventBridge triggers.
  • The created Message Queue for RabbitMQ instances and the Function Compute functions must be in the same region.
  • You cannot create RabbitMQ triggers by using Serverless Devs or SDKs.

Prerequisites

Step 1: Create a trigger

  1. Log on to the Function Compute console.
  2. In the left-side navigation pane, click Services and Functions.
  3. In the top navigation bar, select the region where the service resides.
  4. On the Services page, find the service that you want to manage and click Functions in the Actions column.
  5. On the Functions page, click the name of the function that you want to manage.
    hanshuguanli
  6. On the function details page, click the Triggers tab, select the version or alias from the Version or Alias drop-down list, and then click Create Trigger.
  7. In the Create Trigger panel, enter the relevant information. Click OK.
    Create Trigger panel
    Parameter Description Example
    Trigger Type Select Message Queue for RabbitMQ from the drop-down list. Message Queue for RabbitMQ
    Name The name of the trigger. Enter a custom trigger name. rabbitmq-trigger
    Version or Alias The default value is LATEST. If you want to create a trigger of another version or alias, switch to the trigger of the specified version or alias in the upper-right corner of the function details page. For more information about versions and aliases of a service, see Manage versions and Manage aliases. LATEST
    RabbitMQ Instance Select a Message Queue for RabbitMQ instance. amqp-cn-i7m2l6m2****
    Vhost Select a vhost of the Message Queue for RabbitMQ instance. myhost-1
    Queue Select a queue of the Message Queue for RabbitMQ instance. myqueue-1
    Invocation Method The function invocation method. Valid values:
    • Synchronous Invocation: After an event triggers the execution of a function, Function Compute returns the execution result when the execution is complete. This is the default value. For more information, see Synchronous invocations.
    • Asynchronous Invocation: After an event triggers the execution of a function, Function Compute immediately returns a response and ensures at least one successful execution of the function. However, the detailed execution result is not returned. This invocation method is applicable to a function that has relatively higher scheduling latency. For more information, see Asynchronous invocations.
    Synchronous Invocation

    After the trigger is created, the created trigger is displayed in the Trigger Name list.

Step 2: Write a function

After you create a RabbitMQ trigger, you can write the function code.

On the function details page, click the Function Code tab and write code in the code editor.

The following sample code shows an example:

'use strict';
/*
To enable the initializer feature
please implement the initializer function as below:
exports.initializer = (context, callback) => {
  console.log('initializing');
  callback(null, '');
};
*/
exports.handler = (event, context, callback) => {
  console.log("event: %s", event);
  handle_event(event);
  callback(null, 'return result');
}

Step 3: Test the function

After you write the function, you must debug the function to verify that the code is correct. When Message Queue for RabbitMQ events are delivered to Function Compute by using EventBridge, function execution is triggered.

A Message Queue for RabbitMQ event is passed to the function as an input parameter. You can manually pass an event to the function to trigger the function and test whether the code of the function is correct.

  1. On the Function Details page, click the Function Code tab, and then click the xialatubiao icon. From the drop-down list, select Configure Test Parameters.
  2. In the Configure Test Parameters dialog box, click the Create New Test Event or Edit Existing Test Event tab, and specify the Event Name and event content. Click OK.

    The event parameter is an input parameter of Function Compute. The following code shows the format of the event parameter:

      {
        "id":"bj694332-4cj1-389e-9d8c-b137h30b****",
        "source":"RabbitMQ-Function-rabbitmq-trigger",
        "specversion":"1.0",
        "type":"amqp:Queue:SendMessage",
        "datacontenttype":"application/json;charset=utf-8",
        "subject":"acs:amqp:cn-hangzhou:164901546557****:/instances/amqp-cn-tl32e756****/vhosts/eb-connect/queues/housekeeping",
        "time":"2021-08-12T06:56:40.709Z",
        "aliyunaccountid":"164901546557****",
        "aliyunpublishtime":"2021-10-15T08:58:55.140Z",
        "aliyunoriginalaccountid":"164901546557****",
        "aliyuneventbusname":"RabbitMQ-Function-rabbitmq-trigger",
        "aliyunregionid":"cn-chengdu",
        "aliyunpublishaddr":"42.120.XX.XX",
        "data":{
            "envelope":{
                "deliveryTag":98,
                "exchange":"",
                "redeliver":false,
                "routingKey":"housekeeping"
            },
            "body":{
                "Hello":"RabbitMQ"
            },
            "props":{
                "contentEncoding":"UTF-8",
                "messageId":"f7622d51-e198-41de-a072-77c1ead7****"
            }
        }
    }
    The following table describes the parameters in the data field. For more information about the parameter definition in the CloudEvents specification, see Overview.
    Parameter Type Example Description
    body Map The content of the message.
    Hello String EventBridge The user data.
    props Map The properties of the message.
    contentEncoding String utf-8 The format in which the content of the message is encoded.
    messageId String f7622d51-e198-41de-a072-77c1ead7**** The ID of the message. The ID of each message is unique.
    envelope Map The envelope information about the message.
    deliveryTag Int 98 The tag of the message.
    exchange String N/A The name of the exchange that sent the message.
    redeliver Boolean false Indicates whether the message can be resent. Valid values:
    • true: The message can be resent.
    • false: The message cannot be resent.
    routingKey String housekeeping The routing rule of the message.
  3. On the function details page, click the Function Code tab, and then click Test Function.

Check the result

On the Function Code tab, you can view a successful command.

result

References

To modify or delete a created trigger, see Manage triggers.