Data extraction

更新时间:
复制 MD 格式

When you route events through EventBridge, you may need to reshape or filter the event payload before it reaches the target. EventBridge provides four transformation methods that convert events from the standard CloudEvents format into the structure the target expects:

MethodUse caseWhat gets routed
Complete eventRoute the full event payload without changesThe entire CloudEvents-format event
Partial eventExtract a single field from the eventThe value at the specified JSONPath
Fixed valueUse the event as a trigger onlyA predefined constant value
TemplateReshape the event into a custom formatA string or JSON structure with variable substitution

Transformation pipeline

EventBridge receives events in the standard CloudEvents 1.0 format. When an event matches a rule, EventBridge applies the transformation method you configured, then delivers the result to the event target:

Event source  -->  EventBridge  -->  Transform  -->  Event target
               (CloudEvents 1.0)   (your method)

The following sample event is used throughout this topic to demonstrate each method:

{
    "id": "7adc8c1a-645d-4476-bdef-5d6fb57f****",
    "source": "acs.oss",
    "specversion": "1.0",
    "type": "oss:ObjectCreated:PostObject",
    "datacontenttype": "application/json",
    "dataschema": "http://example.com/test.json",
    "subject": "acs:oss:cn-hangzhou:1234567:xls-papk/game_apk/123.jpg",
    "time": "2020-08-17T16:04:46.149Asia/Shanghai",
    "aliyuneventbusname": "demo-bus",
    "aliyunregionid": "Shanghai",
    "data": {
        "name": "test",
        "scope": 100
    }
}

Complete event

Route the full CloudEvents-format event to the target without any transformation. The event target receives the exact payload shown in the sample event above.

Partial event

Extract a single field from the event by using a JSONPath expression. Only the extracted value is routed to the event target.

Constraints:

ParameterConstraint
JSONPath expressionOnly one expression per transformation
Extracted valueUp to 1,024 characters

Example: Extract the name field from the data object in the sample event.

JSONPath$.data.name
Outputtest

Fixed value

Use the event purely as a trigger. EventBridge ignores the event content and routes a predefined constant value to the event target.

Constraints:

ParameterConstraint
Fixed valueUp to 1,024 characters

Example: Set test1 as the fixed value. Regardless of the event content, the event target receives:

test1

Template

Build a custom output by extracting event fields into named variables with JSONPath, then substituting those variables into a template. The rendered result is routed to the event target.

Constraints:

ParameterConstraint
Variable value (JSONPath or fixed)Up to 1,024 characters
Template bodyUp to 10,240 characters
Variable definitionsNested structures not supported

Define variables

Map variable names to JSONPath expressions or fixed strings in the Parameters field:

{
    "name": "$.data.name",
    "constant": "Please deal with it timely."
}

Each variable value can be either:

  • A JSONPath expression that extracts a value from the event (for example, $.data.name)

  • A fixed string (for example, "Please deal with it timely.")

Example: Using the parameters above with the sample event:

Template:

The instance is broken, which name is ${name}, ${constant}

Output:

The instance is broken, which name is test, Please deal with it timely.

Reference variables in the template

Use ${variable_name} syntax in the Template field. EventBridge resolves each variable at runtime before routing the output.

Three output formats are supported:

Note

The following examples assume the event data field contains {"name": "test", "state": "RUNNING"}.

Simple string

Parameters:

{
    "name": "$.data.name",
    "state": "$.data.state"
}

Template:

"name ${name} is in ${state}"

Output:

"name test is in RUNNING"

Simple JSON

Parameters:

{
    "name": "$.data.name",
    "state": "$.data.state"
}

Template:

{
    "name": "${name}",
    "state": "${state}"
}

Output:

{
    "name": "test",
    "state": "RUNNING"
}

JSON with variables and fixed values

Parameters:

{
    "name": "$.data.name",
    "state": "$.data.state"
}

Template:

{
    "name": "${name}",
    "state": [
        9,
        "${state}",
        true
    ],
    "Transformed": "Yes"
}

Output:

{
    "name": "test",
    "state": [
        9,
        "RUNNING",
        true
    ],
    "Transformed": "Yes"
}