Maayan Hope
Maayan Hope

Reputation: 1601

How to trigger a AWS lambda by sending event to EventBridge

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?

Upvotes: 1

Views: 995

Answers (1)

fedonev
fedonev

Reputation: 25639

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"],

Upvotes: 2

Related Questions