本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
JavaScript EventBridge 数据源的解析器函数参考
与 EventBridge 数据源一起使用的Amazon AppSync 解析器函数请求和响应允许您向 Amazon EventBridge 总线发送自定义事件。
请求
请求处理程序允许您将多个自定义事件发送到 EventBridge 事件总线:
export function request(ctx) { return { "operation" : "PutEvents", "events" : [{}] } }
EventBridge PutEvents
请求的类型定义如下:
type PutEventsRequest = { operation: 'PutEvents' events: { source: string detail: { [key: string]: any } detailType: string resources?: string[] time?: string // RFC3339 Timestamp format }[] }
响应
如果PutEvents
操作成功, EventBridge 则来自的响应将包含在ctx.result
:
export function response(ctx) { if(ctx.error) util.error(ctx.error.message, ctx.error.type, ctx.result) else return ctx.result }
执行诸如InternalExceptions
或之类的PutEvents
操作时发生的错误Timeouts
将出现在ctx.error
。有关常见错误 EventBridge的列表,请参阅EventBridge 常见错误参考资料。
result
将具有以下类型定义:
type PutEventsResult = { Entries: { ErrorCode: string ErrorMessage: string EventId: string }[] FailedEntry: number }
-
参赛作品
摄取的事件会导致成功和失败。如果摄取成功,则条目
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
field
-
Version
该
version
字段与所有请求映射模板相同,它定义了模板使用的版本。此字段为必填。该值2018-05-29
是 EventBridge 映射模板支持的唯一版本。 -
操作
唯一支持的操作是
PutEvents
。此操作允许您将自定义事件添加到事件总线。 -
事件
一系列将添加到事件总线的事件。此数组应分配 1-10 个项目。
Event
对象具有以下字段:-
"source"
:定义事件源的字符串。 -
"detail"
:用于附加关于事件信息的 JSON 对象。此字段可以是空地图 ({ }
)。 -
"detailType
:标识事件类型的字符串。 -
"resources"
:用于标识事件中涉及的资源的 JSON 资源的 JSON 资源的 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" : [] }