All Products
Search
Document Center

EventBridge:Event transformation

Last Updated:Jun 13, 2024

EventBridge supports the transformation of event content. Specifically, EventBridge can transform events in the standard CloudEvents format to the event type that an event target can receive before the events are routed to the event target. This topic describes the event transformation methods provided by EventBridge.

Background information

EventBridge supports the following event transformation methods:

Complete Event

If you specify Complete Event as the transformation method, EventBridge does not transform an event and routes the complete event in the CloudEvents format to the event target. The following table describes a sample event for which the Complete Event transformation method is used.

Note

For information about CloudEvents, see CloudEvents 1.0.

Event before transformation

Transformation method

Event after transformation

{
    "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

{
    "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
    }
}

Partial Event

If you specify Partial Event as the transformation method, EventBridge extracts fields from an event in the CloudEvents format by using JSONPath and routes the specified content of the event to the event target. The following table describes a sample event for which the Partial Event transformation method is used.

Event before transformation

Transformation method

Event after transformation

{
    "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
    }
}

Partial Event

$.data.name
Note
  • You can define only one variable if you use the Partial Event transformation method.

  • value: The value can be up to 10,240 characters in length.

test

Constant

If you specify Constant as the transformation method, EventBridge routes constants in an event to an event target, regardless of what content the event contains. In this case, events are used as triggers. The following table describes a sample event for which the Constant transformation method is used.

Event before transformation

Transformation method

Event after transformation

{
    "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
    }
}

Constant

test1
Note

value: The value can be up to 10,240 characters in length.

test1

Template

If you specify Template as the transformation method, EventBridge extracts fields from an event in the CloudEvents format by using JSONPath, stores the values in variables, and then routes the event in the format that is defined in the template to the event target. The following table describes a sample event for which the Template transformation method is used.

Event before transformation

Transformation method

Event after transformation

{
    "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
    }
}

Template

  • Variable

    {
        "name":"$.data.name",
        "constant":"Please deal with it timely."
    }
    Note
    • The values that are extracted by using JSONPath can be parameters or fixed values.

    • A nested structure is not supported when you define parameters.

    • value: The value can be up to 10,240 characters in length.

  • Template

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

    template: The value can be up to 10,240 characters in length.

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

You can specify a custom template in the string or JSON format, transform an event based on the template, and route the event to an event target. EventBridge supports the following types of templates:

Table 1. Simple string

Event before transformation

Transformation method

Event after transformation

{
    "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",
        "state":"RUNNING"
    }
}

Template

  • Variable

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

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

Table 2. Simple JSON

Event before transformation

Transformation method

Event after transformation

{
    "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",
        "state":"RUNNING"
    }
}

Template

  • Variable

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

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

Table 3. JSON that includes variables and constants

Event before transformation

Transformation method

Event after transformation

{
    "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",
        "state":"RUNNING"
    }
}

Template

  • Variable

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

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

Table 4. Nested JSON

Event before transformation

Transformation method

Event after transformation

{
    "id": "45ef4dewdwe1-7c35-447a-bd93-fab****",
    "source": "acs.imm",
    "specversion": "1.0",
    "subject": "acs.imm:cn-hangzhou:123456789098****:215672",
    "time": "2020-11-19T21:04:41+08:00",
    "type": "imm:FileMeta:Index",
    "aliyunaccountid": "123456789098****",
    "aliyunpublishtime": "2020-11-19T21:04:42.179PRC",
    "aliyuneventbusname": "default",
    "aliyunregionid": "cn-hangzhou",
    "aliyunpublishaddr": "172.25.XX.XX",
    "data": {
        "ProjectName": "test_project",
        "DatasetName": "test_dataset",
        "RequestId": "D2A3AE88-D17C-4CCC-B149-6651115C****",
        "StartTime": "2021-07-22T14:03:15.489885+08:00",
        "EndTime": "2021-07-22T14:05:15.489885+08:00",
        "Message": "InvalidParameter",
        "Success": false,
        "Files": [
            {
                "URI": "oss://bucket/file",
                "CustomId": "defaultId",
                "CustomLabels": {
                    "Key1": "Val1",
                    "Key2": "Val2"
                },
                "Error": "InternalError"
            }
        ]
    }
}

Template

  • Variable

    {   
        "ProjectName":"$.data.ProjectName",
        "Files":"$.data.Files"
    }
  • Template

    {
        "ProjectName":"${ProjectName}",
        "Files":${Files}
    }
{  
    "ProjectName":"test_project",
  "Files": [
        {
            "URI": "oss://bucket/file",
            "CustomId": "defaultId",
            "CustomLabels": {
                "Key1": "Val1",
                "Key2": "Val2"
            },
            "Error": "InternalError"
        }
    ]
}

The names of the functions that support the processing of special characters are jsonEscape and htmlEscape.

  • The jsonEscape function is used to escape special characters in JSON strings to prevent embedded special characters from corrupting the JSON format.

    The following sample code provides an example on variables that contain double quotation marks (""):

    {
      "var" : "\"abc\""
    }

    The following code shows how to use the jsonEscape function in the template:

    {
      "text": "var is ${jsonEscape(var)}"
    }

    Escaped output:

    {
      "text": "var is \"abc\""
    }
  • The htmlEscape function is used to escape special characters in HTML. After the special characters are escaped, they are output as ordinary strings. This prevents security risks caused by malicious script injections.

    The following sample code provides an example on variables that contain HTML content:

    {
      "var" : "<script>alert('unsafe');</script>"
    }

    The following code shows how to use the htmlEscape function in the template:

    <p>User Input: ${htmlEscape(var)}</p>

    Escaped output:

    <p>User Input: &lt;script&gt;alert('unsafe');&lt;/script&gt;</p>