使用Node.js中的eventbridge sdk在Lambda上通过eventbus触发Putevent。

huangapple go评论82阅读模式
英文:

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&lt;string, BlockType&gt;
): Promise&lt;void&gt; {
  const params = {
    Entries: [
      {
    Source: &#39;events.mysource.update&#39;,
    DetailType: &#39;UpdateHasMade&#39;,
    Detail: JSON.stringify(event),
    EventBusName: &#39;arn:aws:events:ca-central-1:1111111111:event-bus/my-eventbus&#39;
  },
    ],
  };

  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 
}

huangapple
  • 本文由 发表于 2023年4月17日 15:00:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032439.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定