All Products
Search
Document Center

Function Compute:ApsaraMQ for RabbitMQ triggers

Last Updated:Feb 02, 2024

After ApsaraMQ for RabbitMQ is integrated with Function Compute as an event source by using EventBridge, you can use ApsaraMQ for RabbitMQ triggers to trigger the execution of the associated functions to perform custom operations on the messages that are published to ApsaraMQ for RabbitMQ. This topic describes how to create an ApsaraMQ for RabbitMQ queue trigger, configure the input parameters of the associated function, write function code, and test the function in the Function Compute console.

Overview

After you submit a request to create a trigger in the Function Compute console, Function Compute automatically creates event streams in EventBridge based on the configurations of the trigger.

After the trigger is created, you can view the information about the trigger in the Function Compute console. You can also view the information about the created resources in the EventBridge console. When a message is enqueue in the source ApsaraMQ for RabbitMQ instance, the associated function in Function Compute is triggered. During function execution, message events are pushed to the function for processing separately or in batches based on your batch configuration. ApsaraMQ for RabbitMQ triggers are suitable for scenarios in which end-to-end streaming data is processed.

Usage notes

  • The ApsaraMQ for RabbitMQ instance that is used as the trigger source must reside in the same region as the Function Compute function.

  • If the number of created event streams exceeds the upper limit, you can no longer create ApsaraMQ for RabbitMQ triggers.

For information about the limits on the number of resources that are used to create a trigger in one region by using a single Alibaba Cloud account, see Limits.

Before you start

Step 1: Create a trigger

  1. Log on to the Function Compute console. In the left-side navigation pane, click Functions.

  2. In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.

  3. On the function details page, click the Configurations tab. In the left-side navigation pane, click Triggers. Then, click Create Trigger.

  4. In the Create Trigger panel, follow the on-screen instructions to configure the parameters and click OK.

    The following table describes the basic parameters.

    Parameter

    Description

    Example

    Trigger Type

    Select Message Queue for RabbitMQ from the drop-down list.

    Message Queue for RabbitMQ

    Name

    Enter a trigger name.

    rabbitmq-trigger

    Version or Alias

    Default value: LATEST. If you want to create a trigger for another version or alias, go back to the Function Details page and switch to the specified version or alias in the upper-right corner. For more information about versions and aliases, see Manage versions and Manage aliases.

    LATEST

    RabbitMQ Instance

    Select the ApsaraMQ for RabbitMQ instance that you created.

    amqp-cn-i7m2l6m2****

    Vhost

    Select the vhost that you created on the ApsaraMQ for RabbitMQ instance.

    myhost-1

    Queue

    Select the queue that you created on the ApsaraMQ for RabbitMQ instance.

    myqueue-1

    Invocation Method

    Select a function invocation method.

    Valid values:

    • Sync Invocation: This method is suitable for sequential invocations. When an event or a batch of events triggers the function, Function Compute runs the function and waits for a response before the function processes the next event or batch of events. The maximum payload of a synchronous invocation request is 32 MB. For more information, see Synchronous invocations.

    • Async Invocation: This method allows you to quickly consume events. When an event or a batch of events triggers the function, Function Compute immediately returns a response and continue processing the next event or batch of events. During this process, the function is executed in asynchronous mode. The maximum payload of an asynchronous invocation request is 128 KB. For more information, see Overview.

    Sync Invocation

    Trigger State

    Specify whether to enable the trigger immediately after it is created. By default, Enable Trigger is selected, which indicates that the trigger is enabled immediately after it is created.

    N/A

    For information about advanced configurations such as push settings, retry policies, and dead-letter queues, see Advanced features of triggers.

    After the trigger is created, it is displayed on the Triggers tab. To modify or delete a trigger, see Manage triggers.

Step 2: Configure the input parameters of the associated function

A source event in ApsaraMQ for Kafka is passed to the associated function as an input parameter in the form of event. You can manually pass event to the function to simulate event triggering and test the function to verify whether the code is correct.

  1. On the Code tab of the function details page, click the image.png icon next Test Function and select Configure Test Parameters from the drop-down list.

  2. In the Configure Test Parameters panel, click the Create New Test Event or Modify Existing Test Event tab, enter the event name and event content, and then click OK.

    Sample code of event:

    [
          {
        "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****"
            }
        }
        },
        {
        "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 contained in data. For information about parameters that are defined in the CloudEvents specification, see Overview.

    Parameter

    Type

    Example

    Description

    body

    Map

    The message body.

    Hello

    String

    EventBridge

    The user data.

    props

    Map

    The message attributes.

    contentEncoding

    String

    utf-8

    The format in which the message body is encoded.

    messageId

    String

    f7622d51-e198-41de-a072-77c1ead7****

    The message ID. The ID of each message is unique.

    envelope

    Map

    The information about the message envelope.

    deliveryTag

    Int

    98

    The message tag.

    exchange

    String

    None

    The name of the exchange that sends the message.

    redeliver

    Boolean

    false

    Specifies whether the message can be resent. Valid values:

    • true

    • false

    routingKey

    String

    housekeeping

    The routing rule of the message.

Step 3: Write function code and test the function

After the trigger is created, you can write function code and test the function to verify whether the code is correct. When events that are generated by ApsaraMQ for RabbitMQ are delivered to Function Compute by using EventBridge, the trigger automatically triggers the function.

  1. On the Code tab of the Function Details page, write code in the code editor and click Deploy.

    The following sample code provides an example on how to write function code in Node.js:

    '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);
      // Parse the event parameters and process the event. 
      callback(null, 'return result');
    }
  2. Click Test Function.

Additional information

To modify or delete an existing trigger, see Manage triggers.