Route messages from ApsaraMQ for RabbitMQ to Function Compute

Updated at:

EventBridge connects ApsaraMQ for RabbitMQ to Function Compute so that every message published to a RabbitMQ queue automatically triggers a function invocation. This eliminates the need to poll queues or maintain custom message consumers.

Before you begin

Make sure the following resources are ready.

EventBridge

Function Compute

ApsaraMQ for RabbitMQ

Step 1: Add a custom event source

Register your RabbitMQ instance as an event source in EventBridge.

  1. Log on to the EventBridge console.

  2. In the left-side navigation pane, click Event Buses.

  3. In the top navigation bar, select a region.

  4. On the Event Buses page, click the name of the custom event bus that you created.

  5. In the left-side navigation pane, click Event Sources.

  6. On the Event Source page, click Add Event Source.

  7. In the Add Custom Event Source panel, configure the following settings and click OK:

    • Name and Description: Enter a name and a description for the event source.

    • Event Provider: Select ApsaraMQ for RabbitMQ.

    • Select the instance, vhost, exchange, and queue that you created for ApsaraMQ for RabbitMQ.

Step 2: Create an event rule

Define an event rule to filter events from the RabbitMQ source and route them to your Function Compute function.

Important

Event targets must reside in the same region as the event rule.

  1. Log on to the EventBridge console.

  2. In the left-side navigation pane, click Event Buses.

  3. In the top navigation bar, select a region.

  4. On the Event Buses page, click the name of the event bus that you want to manage.

  5. In the left-side navigation pane, click Event Rules.

  6. On the page that appears, click Create Rule.

  7. Complete the following steps in the Create Rule panel:

Configure Basic Info

Enter a rule name in the Name field and a description in the Description field. Click Next Step.

Configure Event Pattern

  1. Set Event Source Type to Custom Event Source.

  2. From the Event Source drop-down list, select the event source that you created in Step 1.

  3. Specify an event pattern in the Pattern Content editor. For syntax details, see Event patterns.

  4. Click Next Step.

Configure Targets

Configure the Function Compute target. A single rule supports up to five event targets.

Parameter

Description

Service Type

Select Function Compute.

Service

Select the service that you created in Function Compute.

Function

Select the function that you created in Function Compute.

Event

Select Template to transform the event before delivery. See the example below.

Service Version and Alias

Choose Default Version (fixed to LATEST), Specified Version, or Specified Alias. For details, see Manage versions and Manage aliases.

Invocation Mode

Select Synchronous or Asynchronous. For details, see Synchronous invocations and Asynchronous invocations.

Event template example

Use variables to extract fields from the event and inject them into a template string.

Sample variables:

{
  "source": "$.source",
  "type": "$.type"
}

Sample template:

The event comes from ${source},event type is ${type}.

For more information, see Event transformation.

After you configure all target parameters, click Create.

Step 3: Publish a test event

Send messages to your RabbitMQ exchange to trigger the pipeline. The following Java example publishes 10 test messages to the exchange and queue that EventBridge monitors.

Replace the placeholders with your actual values:

Placeholder

Description

Example

<host-name>

Endpoint of the ApsaraMQ for RabbitMQ instance

xxx.xxx.aliyuncs.com

<user-name>

Static username of the instance

--

<password>

Static password of the instance

--

<virtual-host>

Vhost name

--

<exchange-name>

Exchange name

--

<queue-name>

Queue name

--

<routing-key>

Routing key for the binding

--

<exchange-type>

Exchange type

--

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.TimeoutException;

public class ProducerTest {
    public static void main(String[] args) throws IOException, TimeoutException {
        // The endpoint of the ApsaraMQ for RabbitMQ instance.
        String hostName = "<host-name>";
        // The static username and password of the ApsaraMQ for RabbitMQ instance.
        String userName = "<user-name>";
        String passWord = "<password>";
        // The vhost of the ApsaraMQ for RabbitMQ instance.
        String virtualHost = "<virtual-host>";

        // In production environments, create and reuse connections
        // to avoid frequent setup and teardown overhead.
        Connection connection = createConnection(hostName, userName, passWord, virtualHost);
        Channel channel = connection.createChannel();

        // The binding between the exchange and the queue.
        String exchangeName = "<exchange-name>";
        String queueName = "<queue-name>";
        String routingKey = "<routing-key>";
        // The exchange type.
        String exchangeType = "<exchange-type>";

        // In production environments, create the exchange and queue
        // in the ApsaraMQ for RabbitMQ console instead of declaring
        // them in code. Declaring them in code triggers API throttling.
        channel.exchangeDeclare(exchangeName, exchangeType, true, false, false, null);
        channel.queueDeclare(queueName, true, false, false, new HashMap<String, Object>());
        channel.queueBind(queueName, exchangeName, routingKey);

        // Send 10 test messages.
        for (int i = 0; i < 10; i++) {
            AMQP.BasicProperties props = new AMQP.BasicProperties.Builder().messageId(UUID.randomUUID().toString()).build();
            channel.basicPublish(exchangeName, routingKey, true, props,
                    ("Sample message body-" + i).getBytes(StandardCharsets.UTF_8));
            System.out.println("[SendResult] Message sent successfully, messageId: " + props.getMessageId() + ", exchange: " + exchangeName + ", routingKey: " + routingKey);
        }
        connection.close();
    }

    public static Connection createConnection(String hostName, String userName, String passWord, String virtualHost) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(hostName);
        factory.setUsername(userName);
        factory.setPassword(passWord);
        // Enable automatic connection recovery.
        factory.setAutomaticRecoveryEnabled(true);
        factory.setNetworkRecoveryInterval(5000);
        factory.setVirtualHost(virtualHost);
        // Port 5672 for non-encrypted connections.
        // Port 5671 for encrypted connections.
        factory.setPort(5672);
        // Adjust timeouts based on your network environment.
        factory.setConnectionTimeout(30 * 1000);
        factory.setHandshakeTimeout(30 * 1000);
        factory.setShutdownTimeout(0);
        Connection connection = factory.newConnection();
        return connection;
    }
}

Verify the result

Check the Function Compute logs to confirm that the function received the events.

  1. Log on to the Function Compute console.

  2. In the left-side navigation pane, click Services & Functions.

  3. In the top navigation bar, select the region where your service is deployed.

  4. On the Services page, find the target service and click Functions in the Actions column.

  5. Click the name of the target function.

  6. On the Function Details page, click the Logs tab.

A successful invocation produces log entries similar to the following:

FC Invoke Start RequestId: c2be67a7-fh1a-9619-ei4c-3c04gcf6****
2020-11-19T11:11:34.161Z c2be67a7-fh1a-9619-ei4c-3c04gcf6c**** [verbose] Receive Event v2 ==> The event comes from aliyun.ui,event type is ui:Created:PostObject.
2020-11-19T11:11:34.167Z c2be67a7-fh1a-9619-ei4c-3c04gcf6c****
FC Invoke End RequestId: c2be67a7-fh1a-9619-ei4c-3c04gcf6c****

The Receive Event v2 line confirms that EventBridge delivered the RabbitMQ message to your function.

FAQ

How do I troubleshoot a failed event delivery?

Check the delivery status in the EventBridge console:

  1. Open the EventBridge console.

  2. Navigate to your event bus and locate the Event Trace section.

  3. In Event Delivery, review the response details for the failed event.

The response message indicates the root cause. Fix the issue based on the specific error.

What does the "[500]ConnectErrorconnectiontimedout" error mean?

This error means EventBridge cannot reach the Function Compute endpoint. To diagnose:

  1. Log on to the Function Compute console and run the target function manually.

  2. Check the execution duration:

    • Longer than 15 seconds: A network connectivity issue is likely. Check your network configuration.

    • Shorter than 15 seconds: Verify that the Function Compute endpoint for the target region is accessible.

  3. If the regional endpoint is unreachable, contact Function Compute support.