You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/aws/services/events.mdx
+39-12Lines changed: 39 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,17 +27,28 @@ The native EventBridge provider, introduced in [LocalStack 3.5.0](https://discus
27
27
This guide is designed for users new to EventBridge and assumes basic knowledge of the AWS CLI and our [`awslocal`](https://github.com/localstack/awscli-local) wrapper script.
28
28
29
29
Start your LocalStack container using your preferred method.
30
-
We will demonstrate creating an EventBridge rule to run a Lambda function on a schedule.
30
+
We will demonstrate creating an EventBridge rule to run a Lambda function when a custom event is published to an event bus.
31
+
32
+
### Create an EventBridge Bus
33
+
34
+
First, create a custom EventBridge bus using the [`CreateEventBus`](https://docs.aws.amazon.com/cli/latest/reference/events/create-event-bus.html) API:
35
+
36
+
```bash
37
+
awslocal events create-event-bus \
38
+
--name my-custom-bus
39
+
```
40
+
41
+
While you can always use the default event bridge bus automatically available and configured for your account in any region, custom event buses are much more commonly used in practice and provide better organization for your events.
31
42
32
43
### Create a Lambda Function
33
44
34
45
To create a new Lambda function, create a new file called `index.js` with the following code:
In the above command, we have specified a schedule expression of `rate(2 minutes)`, which will run the rule every two minutes.
72
-
It means that the Lambda function will be invoked every two minutes.
84
+
In the above command, we have specified an event pattern that will match events with:
85
+
-`source` field equal to `my-source`
86
+
-`detail-type` field equal to `my-detail-type`
87
+
88
+
This rule will trigger whenever an event matching this pattern is published to the custom event bus.
73
89
74
-
Next, grant the EventBridge service principal (`events.amazonaws.com`) permission to run the rule, using the [`AddPermission`](https://docs.aws.amazon.com/cli/latest/reference/events/add-permission.html) API:
90
+
Next, grant the EventBridge service principal (`events.amazonaws.com`) permission to run the rule, using the [`AddPermission`](https://docs.aws.amazon.com/cli/latest/reference/lambda/add-permission.html) API:
@@ -99,14 +115,25 @@ Finally, add the Lambda function as a target to the EventBridge rule using the [
99
115
100
116
```bash
101
117
awslocal events put-targets \
102
-
--rule my-scheduled-rule \
118
+
--rule my-custom-rule \
119
+
--event-bus-name my-custom-bus \
103
120
--targets file://targets.json
104
121
```
105
122
123
+
### Send an Event to Trigger the Lambda
124
+
125
+
Now, send an event that matches the rule pattern to trigger the Lambda function using the [`PutEvents`](https://docs.aws.amazon.com/cli/latest/reference/events/put-events.html) API:
0 commit comments