Use Function Compute

Updated at:

When your application needs to react to Tablestore data changes in real time — such as syncing records to another service or triggering notifications — polling is inefficient and adds latency. Function Compute integrates with Tablestore Stream through an event-driven trigger, so your function runs automatically whenever a row is inserted, updated, or deleted.

How it works

A Tablestore trigger connects Function Compute (FC) to your data table through Tablestore Stream. When your table is modified — a row inserted, updated, or deleted — Tablestore Stream captures the change in real time. The trigger detects new stream records and automatically invokes your function with the event payload.

image

This eliminates polling. Common use cases include:

  • Data synchronization: copy stream records to Object Storage Service (OSS) to build a permanent audit trail.

  • Event notifications: fire a function when a user profile is updated to push real-time alerts to downstream services.

  • Incremental data archiving: archive changed records to a data warehouse on every write.

    In addition to event functions, Function Compute also supports other types, such as Web functions and task functions. Different function types suit different business scenarios and technical architectures. For a detailed comparison and guidance on selecting the appropriate type, see Function selection .

Access Tablestore using an event function

The following steps guide you through enabling Tablestore Stream, creating an event function, and configuring a trigger.

Before you begin, make sure you have:

  • A Tablestore instance and data table in a supported region.

  • Access to the Function Compute console with permission to create functions and triggers.

    Step 1: Enable Tablestore Stream

    A Tablestore trigger relies on Tablestore Stream to capture incremental data. Enable Stream on your target table before creating the trigger.

    Tablestore triggers are available in the following regions: China (Beijing), China (Hangzhou), China (Shanghai), China (Shenzhen), Japan (Tokyo), Singapore, Germany (Frankfurt), and China (Hong Kong).
    1. Log on to the Tablestore console. In the top navigation bar, select the region where your instance is located, and then click the alias of the target instance.

    2. On the Instance Details page, click the name of the target data table in the Tables list.

    3. Click the Tunnels tab, and then click Enable to the right of Stream Information.

    4. In the Enable Stream dialog box, set the log expiration time, and then click Enable.

      The log expiration time must be a non-zero integer in hours, up to a maximum of 168. This setting cannot be changed once configured. Set this value with caution.

    Step 2: Create and configure an event function

    The Tablestore trigger can only reference instances and tables in the same region as the function.

    1. Log on to the Function Compute console. In the top navigation bar, select the same region as your Tablestore instance.

    2. In the left-side navigation pane, choose Function Management > Functions.

    3. Click Create Function, select Event Function as the function type, and then click Create Event Function.

    4. Configure the following parameters. For full details, see Create an event function.

      • Function Name: A unique name within the same account and region, following the naming conventions.

      • Runtime: Select Built-in Runtime and choose a language and version. This topic uses Python 3.12 as an example.

      • Code Upload Method: Select Upload ZIP. Download the sample code: tablestore_trigger_example.zip.

    5. Click Create.

    Step 3: Test and validate the event function

    Test the function before configuring the trigger. Tablestore triggers use Concise Binary Object Representation (CBOR) encoding at runtime, but the console accepts JSON for simulation.

    1. On the Function Details page, click the arrow next to Test Function and select Configure Test Parameters.

    2. Enter an Event Name such as test-event, paste the following sample event, and click OK.

      {
          "Version": "Sync-v1",
          "Records": [
              {
                  "Type": "PutRow",
                  "Info": {
                      "Timestamp": 1506416585740836
                  },
                  "PrimaryKey": [
                      {
                          "ColumnName": "pk_0",
                          "Value": 1506416585881590900
                      },
                      {
                          "ColumnName": "pk_1",
                          "Value": "2017-09-26 17:03:05.8815909 +0800 CST"
                      },
                      {
                          "ColumnName": "pk_2",
                          "Value": 1506416585741000
                      }
                  ],
                  "Columns": [
                      {
                          "Type": "Put",
                          "ColumnName": "attr_0",
                          "Value": "hello_table_store",
                          "Timestamp": 1506416585741
                      },
                      {
                          "Type": "Put",
                          "ColumnName": "attr_1",
                          "Value": 1506416585881590900,
                          "Timestamp": 1506416585741
                      }
                  ]
              }
          ]
      }
    3. Click Test Function. A successful run returns OK. View the execution output on the Log Output tab.

      2025-11-24 16:02:59 1-69241131-15e0c7d6-8ec1eb81c700 [INFO] Begin to handle event
      2025-11-24 16:02:59 1-69241131-15e0c7d6-8ec1eb81c700 [INFO] --------------------------------------------------
      2025-11-24 16:02:59 1-69241131-15e0c7d6-8ec1eb81c700 [INFO] Event Type: PutRow
      2025-11-24 16:02:59 1-69241131-15e0c7d6-8ec1eb81c700 [INFO] Timestamp: 1506416585740836
      2025-11-24 16:02:59 1-69241131-15e0c7d6-8ec1eb81c700 [INFO] Primary Keys:
      2025-11-24 16:02:59 1-69241131-15e0c7d6-8ec1eb81c700 [INFO]   Name: pk_0, Value: 1506416585881590900
      2025-11-24 16:02:59 1-69241131-15e0c7d6-8ec1eb81c700 [INFO]   Name: pk_1, Value: 2017-09-26 17:03:05.8815909 +0800 CST
      2025-11-24 16:02:59 1-69241131-15e0c7d6-8ec1eb81c700 [INFO]   Name: pk_2, Value: 1506416585741000
      2025-11-24 16:02:59 1-69241131-15e0c7d6-8ec1eb81c700 [INFO] Attribute Columns:
      2025-11-24 16:02:59 1-69241131-15e0c7d6-8ec1eb81c700 [INFO]   Type: Put, Name: attr_0, Value: hello_table_store, Timestamp: 1506416585741
      2025-11-24 16:02:59 1-69241131-15e0c7d6-8ec1eb81c700 [INFO]   Type: Put, Name: attr_1, Value: 1506416585881590900, Timestamp: 1506416585741
      2025-11-24 16:02:59 1-69241131-15e0c7d6-8ec1eb81c700 [INFO] --------------------------------------------------

    Step 4: Configure and validate the trigger

    After the function test passes, configure the Tablestore trigger. The trigger invokes your function automatically whenever the data table changes.

    1. On the Function Details page, click the Triggers tab, and then click Create Trigger.

    2. Configure the following trigger parameters.

      • Trigger Type: Select Tablestore.

      • Name: Enter a custom name for the trigger, such as test_trigger.

      • Instance: Select the target Tablestore instance.

      • Table: Select the target data table.

    3. Click OK.

    4. Write a row to the data table — for example, insert a row using the Tablestore console.

    5. On the Function Details page, click the Logs tab to view invocation logs. In the Actions column, click Request Logs for a specific request to view its details.

      2025-11-24 16:30:12 1-69241793-15071122-c8e16b8b5ce5 [INFO] Begin to handle event
      2025-11-24 16:30:12 1-69241793-15071122-c8e16b8b5ce5 [INFO] --------------------------------------------------
      2025-11-24 16:30:12 1-69241793-15071122-c8e16b8b5ce5 [INFO] Event Type: PutRow
      2025-11-24 16:30:12 1-69241793-15071122-c8e16b8b5ce5 [INFO] Timestamp: 1763973009228992
      2025-11-24 16:30:12 1-69241793-15071122-c8e16b8b5ce5 [INFO] Primary Keys:
      2025-11-24 16:30:12 1-69241793-15071122-c8e16b8b5ce5 [INFO]   Name: id, Value: 8
      2025-11-24 16:30:12 1-69241793-15071122-c8e16b8b5ce5 [INFO]   Name: name, Value: test-name
      2025-11-24 16:30:12 1-69241793-15071122-c8e16b8b5ce5 [INFO] Attribute Columns:
      2025-11-24 16:30:12 1-69241793-15071122-c8e16b8b5ce5 [INFO]   Type: Put, Name: age, Value: 18, Timestamp: 1763973009229
      2025-11-24 16:30:12 1-69241793-15071122-c8e16b8b5ce5 [INFO] --------------------------------------------------

Limitations

  • Avoid circular invocations. A function triggered by a change to Table A must not write back to Table A. Doing so causes infinite invocations.

  • Execution failures trigger retries. If a function fails, Function Compute retries it until the stream data in Tablestore expires.

    Note
    • Function execution can fail in two ways, with different billing implications:

      • Code throws an exception: The instance has already started, so you are charged for the execution duration.

      • Instance fails to start: If an incorrect startup command or similar issue prevents the instance from starting, you are not charged.

      • Deleting a trigger does not stop running instances: Any in-flight function instances continue until they complete or time out.

    • To stop an infinite retry loop, disable Tablestore Stream for the data table. Before disabling, confirm that no other triggers rely on the same table to avoid disrupting other workflows.

FAQ

  • Why can't I create a Tablestore trigger? Verify that your Tablestore instance is in a supported region. Tablestore triggers are not available in all regions.

  • Why is my data table not listed when I create the trigger? The function and the data table must be in the same region. Confirm that you selected the correct region in the Function Compute console.

  • **Why do I keep getting an Invocation canceled by client error?** The client-side timeout is shorter than the function's execution time. Increase the client-side timeout period. For details, see What do I do if a client disconnects and reports an 'Invocation canceled by client' error?.

  • Why doesn't the trigger fire when new data is added? Check the following in order:

    • Confirm that Tablestore Stream is enabled for the data table.

    • Verify that the trigger role is correct. The default role is AliyunTableStoreStreamNotificationRole.

    • Check the function logs for execution errors. If the function keeps failing, Function Compute retries it until the stream data expires.