英文:
Putevent on eventbus with Lambda with eventbridge sdk Node.js
问题
我想要的只是通过 Lambda 使用事件总线 SDK 在事件总线上放置一些事件,然后稍后可以进行处理的活动。
这个函数在本地机器上调用时测试正常,但在 AWS 上,事件丢失并且未被事件总线拾取。
分享基本代码:
function _eventData(event: any): EventBusEvent {
var eventData: BusEvent = {
Source: 'events.mysource.update',
DetailType: 'UpdateHasMade',
Detail: JSON.stringify(event),
EventBusName: 'arn:aws:events:ca-central-1:1111111111:event-bus/my-eventbus'
};
return {
Entries: [eventData],
};
}
// 创建 Amazon EventBridge 服务客户端对象。
export const ebClient = new EventBridgeClient({ region: 'ca-central-1' });
export async function dispatchEvent(event: any) {
const eventData = _eventData(event);
await ebClient
.send(new PutEventsCommand(eventData))
.then(data => {
console.log(data);
console.log('Success, event sent');
})
.catch(err => {
console.log('Error');
console.log(err);
});
}
还检查了上述 Lambda 的角色策略,对于 EventBridge 和 EventBridgeSchemas,它具有所有权限。
日志中未观察到任何错误,想知道可能是什么问题。
解决方法:
设法解决了这个问题,将 eventbridge 客户端调用保留在处理程序中,而不是从单独的函数中调用它。
英文:
All I wanted is to put some events on the event bus with Lambda using event bridge sdk which then can be picked up for the later activity.
This function tested fine when invoked on the local machine, somehow on AWS the event is lost and not picked up by event bus.
Sharing the basic code:
function _eventData(event: any): EventBusEvent {
var eventData: BusEvent = {
Source: 'events.mysource.update',
DetailType: 'UpdateHasMade',
Detail: JSON.stringify(event),
EventBusName: 'arn:aws:events:ca-central-1:1111111111:event-bus/my-eventbus'
};
return {
Entries: [eventData],
};
}
// Create an Amazon EventBridge service client object.
export const ebClient = new EventBridgeClient({ region: 'ca-central-1' });
export async function dispatchEvent(event: any) {
const eventData = _eventData(event);
await ebClient
.send(new PutEventsCommand(eventData))
.then(data => {
console.log(data);
console.log('Success, event sent');
})
.catch(err => {
console.log('Error');
console.log(err);
});
Have also checked for the Role policy for the above lambda, it has all permissions for
EventBridge and EventBridgeSchemas`
There is no error observed as such in logs, wondering what might be the issue here.
Solution:
Managed to resolve this issue with keeping eventbridge client call in handler itself rather than calling it from separate function.
答案1
得分: 1
以下是您要翻译的内容:
"Maybe there is something wrong with your function implementation. Following official aws docs I got this working for myself. Maybe it helps.
export async function sendSlackMessageToCustomEventBus(
slackMessage: Record<string, BlockType>
): Promise
const params = {
Entries: [
{
Source: 'events.mysource.update',
DetailType: 'UpdateHasMade',
Detail: JSON.stringify(event),
EventBusName: 'arn:aws:events:ca-central-1:1111111111:event-bus/my-eventbus'
},
],
};
const eventBridgeClient = new EventBridgeClient({ region: region });
const eventBridgeCommand = new PutEventsCommand(params);
const result = await eventBridgeClient.send(eventBridgeCommand); // you do not have to store result in variable, its for testing
}
英文:
Maybe there is something wrong with your function implementation. Following official aws docs I got this working for myself. Maybe it helps.
export async function sendSlackMessageToCustomEventBus(
slackMessage: Record<string, BlockType>
): Promise<void> {
const params = {
Entries: [
{
Source: 'events.mysource.update',
DetailType: 'UpdateHasMade',
Detail: JSON.stringify(event),
EventBusName: 'arn:aws:events:ca-central-1:1111111111:event-bus/my-eventbus'
},
],
};
const eventBridgeClient = new EventBridgeClient({ region: region });
const eventBridgeCommand = new PutEventsCommand(params);
const result = await eventBridgeClient.send(eventBridgeCommand); // you do not have to store result in variable, its for testing
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论