Tablestore is a distributed NoSQL data storage service built on the Apsara system. A Tablestore trigger connects Tablestore as an event source for Function Compute. When data changes in a Tablestore table, the trigger automatically invokes your function to process the incremental data — no polling required.
Typical use case: a data source writes to Table A. The update triggers a function to cleanse the data and write the results to Table B, ready for direct reads. The entire pipeline runs as an elastic, scalable serverless application.
Prerequisites
Before you begin, ensure that you have:
A function in Function Compute. See Create a function.
A Tablestore instance. See Create instances.
A table in that instance. See Create tables.
Limitations
Tablestore triggers are supported in the following regions: China (Beijing), China (Hangzhou), China (Shanghai), China (Shenzhen), Japan (Tokyo), Singapore, Germany (Frankfurt), and China (Hong Kong).
The Tablestore table must reside in the same region as the function.
Function execution invoked by a Tablestore trigger cannot exceed 1 minute.
To access Tablestore over an internal network, use a Virtual Private Cloud (VPC) endpoint in the format:
{instance}.{region}.vpc.tablestore.aliyuncs.com.
Usage notes
Avoid invocation loops. If a function writes back to the same table that triggered it — for example, Table A triggers Function B, and Function B updates Table A — the trigger fires again, creating an infinite loop. Design your data flow so that function output goes to a different table.
Retry behavior on failure. If a function execution fails, the trigger retries until the log data in Tablestore expires.
A function execution failure occurs in one of these situations:
A function instance starts but the code does not run as expected. In this case, fees are charged for the instance.
A function instance fails to start (for example, due to a startup command error). In this case, no fees are charged.
To stop infinite retries, disable the Stream feature for the data table. Before disabling Stream, confirm that no other triggers are using the same table.
Step 1: Enable the Stream feature for the data table
The Tablestore Stream feature captures incremental data changes and delivers them to your function. Enable it on the data table before creating the trigger.
Log on to the Tablestore console.
In the top navigation bar, select a region.
On the Overview page, click the instance alias or click Manage Instance in the Actions column.
On the Instance Details tab, click the Data Tables tab. Then click the table name and select the Stream tab, or click
and select Stream.On the Stream tab, click Enable next to Stream Information.
In the Enable Stream dialog box, set the Log Expiration Time parameter and click Enable. The value must be a non-zero integer in hours. Maximum: 168 hours.
The Log Expiration Time cannot be modified after it is set. Choose carefully.
Step 2: Create a Tablestore trigger
Log on to the Function Compute console. In the left-side navigation pane, click Functions.
In the top navigation bar, select a region. On the Functions page, click the function you want to configure.
On the function details page, click the Configurations tab. In the left-side navigation pane, click Triggers, then click Create Trigger.
In the Create Trigger panel, configure the following parameters and click OK.
| Parameter | Description | Example |
|---|---|---|
| Trigger type | The type of trigger. Select Tablestore. | Tablestore |
| Name | The name of the trigger. | Tablestore-trigger |
| Version or alias | The function version or alias to bind. Default: LATEST. To bind a different version or alias, select it from the Version or Alias drop-down list on the function details page. See Manage versions and Manage aliases. | LATEST |
| Instance | The name of the existing Tablestore instance. | d00dd8xm\*\*\*\* |
| Table | The name of the existing table. | mytable |
| Role name | The RAM role that grants Function Compute permission to read from the Tablestore Stream. Select AliyunTableStoreStreamNotificationRole. | AliyunTableStoreStreamNotificationRole |
If this is the first time you create a trigger of this type, click Authorize Now in the dialog box that appears.
After the trigger is created, it appears on the Triggers tab. To modify or delete a trigger, see Trigger management.
Step 3: Configure test parameters
To test the function before data arrives from Tablestore, configure a test event that matches the Tablestore trigger payload format.
On the Code tab of the function details page, click the
icon next to Test Function and select Configure Test Parameters.In the Configure Test Parameters panel, click Create New Test Event or Modify Existing Test Event, enter an event name and the event content, then click OK.
The Tablestore trigger encodes incremental data in Concise Binary Object Representation (CBOR) format and passes it to your function as an event. The following example shows a payload with three record types — PutRow, UpdateRow, and DeleteRow — so you can see how the structure varies by operation:
{
"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
}
]
},
{
"Type": "UpdateRow",
"Info": {
"Timestamp": 1506416600000000
},
"PrimaryKey": [
{
"ColumnName": "pk_0",
"Value": 1506416585881590900
}
],
"Columns": [
{
"Type": "Put",
"ColumnName": "attr_0",
"Value": "updated_value",
"Timestamp": 1506416600000
},
{
"Type": "DeleteOneVersion",
"ColumnName": "attr_1",
"Timestamp": 1506416585741
}
]
},
{
"Type": "DeleteRow",
"Info": {
"Timestamp": 1506416700000000
},
"PrimaryKey": [
{
"ColumnName": "pk_0",
"Value": 1506416585881590900
}
],
"Columns": []
}
]
}The following table describes the event fields.
| Field | Description |
|---|---|
Version | The payload version. Value: Sync-v1 (string). |
Records | An array of incremental data rows. Each element contains Type, Info, PrimaryKey, and Columns. |
Type (row) | The operation type for the row. Valid values: PutRow, UpdateRow, DeleteRow (string). |
Info | Row metadata. Contains Timestamp: the UTC time when the row was last modified (INT64). |
PrimaryKey | An array of primary key columns. Each element contains ColumnName (string) and Value (INTEGER, STRING, or BLOB). |
Columns | An array of attribute columns. Each element contains: Type (operation on the column: Put, DeleteOneVersion, or DeleteAllVersions), ColumnName (string), Value (INTEGER, BOOLEAN, DOUBLE, STRING, or BLOB), and Timestamp (UTC time of last modification, INT64). |
Step 4: Write and test function code
After the trigger is created, write your function code to process the Tablestore event payload. The function is invoked automatically when data in the table changes.
On the function details page, click the Code tab, enter your function code, and click Deploy. The following Python example reads Tablestore event records and extracts primary key and attribute column values:
import logging import cbor import json def get_attribute_value(record, column): attrs = record[u'Columns'] for x in attrs: if x[u'ColumnName'] == column: return x['Value'] def get_pk_value(record, column): attrs = record[u'PrimaryKey'] for x in attrs: if x['ColumnName'] == column: return x['Value'] def handler(event, context): logger = logging.getLogger() logger.info("Begin to handle event") #records = cbor.loads(event) records = json.loads(event) for record in records['Records']: logger.info("Handle record: %s", record) pk_0 = get_pk_value(record, "pk_0") attr_0 = get_attribute_value(record, "attr_0") return 'OK'For examples in Node.js, PHP, Java, and C# runtimes, see Use Tablestore to trigger Function Compute in Node.js, PHP, Java, and C# runtimes.
Click Test Function.
After execution, view the results on the Code tab.
FAQ
Why does trigger creation fail in a specific region?
Tablestore triggers are only available in certain regions. Check the Limitations section to confirm that your region is supported.
Why can't I find my Tablestore table when creating a trigger?
The Tablestore table must reside in the same region as the function. If the table is in a different region, it won't appear in the trigger configuration.
Why do I keep seeing "Invocation canceled by client" errors?
This error typically means the client-side timeout is shorter than the actual function execution duration. Increase the client timeout period. For details, see What do I do if the client is disconnected and the message "Invocation canceled by client" is reported?
Why isn't my Tablestore trigger firing after data is written to the table?
Check the following:
Confirm that the Stream feature is enabled for the table. See Step 1: Enable the Stream feature for the data table.
Confirm that the correct role (
AliyunTableStoreStreamNotificationRole) was configured when creating the trigger. See Step 2: Create a Tablestore trigger.Check the function run logs to see if execution is failing. If the function fails repeatedly, it retries until the log data expires. See What do I do if a trigger cannot trigger function execution?