Skip to content

Commit 12197a7

Browse files
committed
rework eventbridge getting started guide
1 parent 99e4483 commit 12197a7

File tree

1 file changed

+39
-12
lines changed

1 file changed

+39
-12
lines changed

src/content/docs/aws/services/events.mdx

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,28 @@ The native EventBridge provider, introduced in [LocalStack 3.5.0](https://discus
2727
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.
2828

2929
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.
3142

3243
### Create a Lambda Function
3344

3445
To create a new Lambda function, create a new file called `index.js` with the following code:
3546

36-
```js showshowLineNumbers
47+
```js showLineNumbers
3748
'use strict';
3849

3950
exports.handler = (event, context, callback) => {
40-
console.log('LogScheduledEvent');
51+
console.log('LogEventBridgeEvent');
4152
console.log('Received event:', JSON.stringify(event, null, 2));
4253
callback(null, 'Finished');
4354
};
@@ -64,22 +75,27 @@ Run the following command to create a new EventBridge rule using the [`PutRule`]
6475

6576
```bash
6677
awslocal events put-rule \
67-
--name my-scheduled-rule \
68-
--schedule-expression 'rate(2 minutes)'
78+
--name my-custom-rule \
79+
--event-bus-name my-custom-bus \
80+
--event-pattern '{"source":["my-source"],"detail-type":["my-detail-type"]}' \
81+
--state ENABLED
6982
```
7083

71-
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.
7389

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:
7591

7692
```bash
7793
awslocal lambda add-permission \
7894
--function-name events-example \
79-
--statement-id my-scheduled-event \
95+
--statement-id my-custom-event \
8096
--action 'lambda:InvokeFunction' \
8197
--principal events.amazonaws.com \
82-
--source-arn arn:aws:events:us-east-1:000000000000:rule/my-scheduled-rule
98+
--source-arn arn:aws:events:us-east-1:000000000000:rule/my-custom-bus/my-custom-rule
8399
```
84100

85101
### Add the Lambda Function as a Target
@@ -99,14 +115,25 @@ Finally, add the Lambda function as a target to the EventBridge rule using the [
99115

100116
```bash
101117
awslocal events put-targets \
102-
--rule my-scheduled-rule \
118+
--rule my-custom-rule \
119+
--event-bus-name my-custom-bus \
103120
--targets file://targets.json
104121
```
105122

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:
126+
127+
```bash
128+
awslocal events put-events \
129+
--entries '[{"Source": "my-source", "DetailType": "my-detail-type", "Detail": "{\"key\": \"value\"}", "EventBusName": "my-custom-bus"}]'
130+
```
131+
132+
This event will match the pattern we defined in the rule and should trigger the Lambda function immediately.
133+
106134
### Verify the Lambda invocation
107135

108136
You can verify the Lambda invocation by checking the CloudWatch logs.
109-
However, wait at least 2 minutes after running the last command before checking the logs.
110137

111138
Run the following command to list the CloudWatch log groups:
112139

0 commit comments

Comments
 (0)