本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
Amazon AppSync 的解析器映射模板参考 EventBridge
注意
我们现在主要支持 APPSYNC_JS 运行时系统及其文档。请考虑使用 APPSYNC_JS 运行时系统和此处的指南。
与 EventBridge 数据源一起使用的 Amazon AppSync 解析器映射模板允许您向 Amazon EventBridge 总线发送自定义事件。
请求映射模板
PutEvents
请求映射模板允许您将多个自定义事件发送到 EventBridge事件总线。 映射文档具有以下结构:
{ "version" : "2018-05-29", "operation" : "PutEvents", "events" : [{}] }
以下是用于的请求映射模板的示例 EventBridge:
{ "version": "2018-05-29", "operation": "PutEvents", "events": [{ "source": "com.mycompany.myapp", "detail": { "key1" : "value1", "key2" : "value2" }, "detailType": "myDetailType1" }, { "source": "com.mycompany.myapp", "detail": { "key3" : "value3", "key4" : "value4" }, "detailType": "myDetailType2", "resources" : ["Resource1", "Resource2"], "time" : "2023-01-01T00:30:00.000Z" } ] }
响应映射模板
如果PutEvents
操作成功, EventBridge 则来自的响应将包含在$ctx.result
:
#if($ctx.error) $util.error($ctx.error.message, $ctx.error.type, $ctx.result) #end $util.toJson($ctx.result)
执行 PutEvents
操作时发生的错误(例如 InternalExceptions
或 Timeouts
)将出现在 $ctx.error
中。有关常见错误 EventBridge的列表,请参阅EventBridge 常见错误参考。
result
将采用以下格式:
{ "Entries" [ { "ErrorCode" : String, "ErrorMessage" : String, "EventId" : String } ], "FailedEntryCount" : number }
-
Entries
摄取的事件结果(成功和失败)。如果摄取成功,则在该条目中包含
EventID
。否则,您可以使用ErrorCode
和ErrorMessage
找出条目的问题。对于每条记录,响应元素的索引与请求数组中的索引相同。
-
FailedEntryCount
失败条目数。该值表示为一个整数。
有关响应的更多信息PutEvents
,请参阅PutEvents。
示例响应 1
以下示例是具有两个成功事件的 PutEvents
操作:
{ "Entries" : [ { "EventId": "11710aed-b79e-4468-a20b-bb3c0c3b4860" }, { "EventId": "d804d26a-88db-4b66-9eaf-9a11c708ae82" } ], "FailedEntryCount" : 0 }
示例响应 2
以下示例是具有三个事件的 PutEvents
操作,其中的两个事件是成功事件,一个是失败事件:
{ "Entries" : [ { "EventId": "11710aed-b79e-4468-a20b-bb3c0c3b4860" }, { "EventId": "d804d26a-88db-4b66-9eaf-9a11c708ae82" }, { "ErrorCode" : "SampleErrorCode", "ErrorMessage" : "Sample Error Message" } ], "FailedEntryCount" : 1 }
PutEvents
字段
PutEvents
包含以下映射模板字段:
-
版本
version
字段是所有请求映射模板通用的,用于定义模板使用的版本。该字段为必填。该值2018-05-29
是 EventBridge 映射模板支持的唯一版本。 -
操作
唯一支持的操作是
PutEvents
。通过执行该操作,您可以将自定义事件添加到事件总线中。 -
事件
将添加到事件总线的事件数组。应该为该数组分配 1-10 个项目。
Event
对象是一个有效的 JSON 对象,它具有以下字段:-
"source"
:定义事件源的字符串。 -
"detail"
:可用于附加事件相关信息的 JSON 对象。该字段可以是空映射 ({ }
)。 -
"detailType
:指定事件类型的字符串。 -
"resources"
:指定事件中涉及的资源的 JSON 字符串数组。该字段可以是空数组。 -
"time"
:以字符串形式提供的事件时间戳。这应遵循RFC3339时间戳格式。
-
以下片段是一些有效 Event
对象示例:
示例 1
{ "source" : "source1", "detail" : { "key1" : [1,2,3,4], "key2" : "strval" }, "detailType" : "sampleDetailType", "resources" : ["Resouce1", "Resource2"], "time" : "2022-01-10T05:00:10Z" }
示例 2
{ "source" : "source1", "detail" : {}, "detailType" : "sampleDetailType" }
示例 3
{ "source" : "source1", "detail" : { "key1" : 1200 }, "detailType" : "sampleDetailType", "resources" : [] }