如何通过将事件发送到EventBridge来触发AWS Lambda

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

How to trigger a AWS lambda by sending event to EventBridge

问题

以下是翻译好的部分:

  1. 可以模拟此事件的命令行触发吗?
    也许通过运行 aws events put-events --entries file://putevents.json 来触发它。
    (在putevents.json文件中,应该写入什么内容?)

  2. 可以从JavaScript代码中模拟此事件吗?

英文:

I have a AWS lambda that the trigger for activating it is an event from EventBridge (rule)

The rule looks like this:

{
  "detail-type": ["ECS Task State Change"],
  "source": ["aws.ecs"],
  "detail": {
    "stopCode": ["EssentialContainerExited", "UserInitiated"],
    "clusterArn": ["arn:aws:ecs:.........."],
    "containers": {
      "name": ["some name"]
    },
    "lastStatus": ["DEACTIVATING"],
    "desiredStatus": ["STOPPED"]
  }
}

This event is normally triggered when ECS task status is changed (in this case when a task is killed)

My questions are:

  1. Can I simulate this event from command line?
    maybe by running aws events put-events --entries file://putevents.json
    (What should I write in the putevents.json file?)

  2. Can I simulate this event from Javascript code?

答案1

得分: 1

TL;DR 是的,是的,只要你处理用户生成事件不能具有以 aws 开头的 source 的限制。


使用 PutEvents API 向 EventBridge 发送自定义事件。该API在CLI中和SDK中都可用(请参阅 AWS JS SDK)。您传递到 entries 参数中的自定义事件列表 必须具有 至少三个字段:

[
  {
    "source": "my-custom-event", // 不能以 aws 开头!!,
    "detail-type": "ECS Task State Change",
    "detail": {}  // 从ECS示例事件文档中复制
  }
]

ECS 任务状态更改事件 在ECS文档中的示例可作为自定义事件的方便模板。您可以安全地修剪任何不需要用于模式匹配的非必需字段。

不允许自定义事件模仿 aws 系统事件来源。因此,请修改您的规则以匹配您的自定义来源名称:

"source": ["aws.ecs", "my-custom-event"],
英文:

TL;DR Yes and yes, provided you deal with with the limitation that user-generated events cannot have a source that begins with aws.


Send custom events to EventBridge with the PutEvents API. The API is available in the CLI as well as in the SDKs (see AWS JS SDK). The list of custom events you pass in the entries parameter must have three fields at a minimum:

[
  {
    "source": "my-custom-event", // cannot start with aws !!,
    "detail-type": "ECS Task State Change",
    "detail": {}  // copy from the ECS sample events docs
  }
]

The ECS task state change event samples in the ECS documentation make handy templates for your custom events. You can safely prune any non-required field that you don't need for pattern matching.

Custom events are not permitted to mimic the aws system event sources. So amend your rule to also match on your custom source name:

"source": ["aws.ecs", "my-custom-event"],

huangapple
  • 本文由 发表于 2023年2月8日 23:33:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388111.html
匿名

发表评论

匿名网友

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

确定