# EventBridge

Source: /aws/services/events/

## Introduction

EventBridge provides a centralized mechanism to discover and communicate events across various AWS services and applications.
EventBridge allows you to register, track, and resolve events, which indicates a change in the environment and then applies a rule to route the event to a target.
EventBridge rules are tied to an Event Bus to manage event-driven workflows.
You can use either identity-based or resource-based policies to control access to EventBridge resources, where the former can be attached to IAM users, groups, and roles, and the latter can be attached to specific AWS resources.

LocalStack allows you to use the EventBridge APIs in your local environment to create rules that route events to a target.
The supported APIs are available on our [API Coverage section](#api-coverage), which provides information on the extent of EventBridge's integration with LocalStack.
For information on EventBridge Pipes, please refer to the [EventBridge Pipes](/aws/services/pipes/) documentation.

:::note
LocalStack 2026.04.0 removes the legacy v1 EventBridge provider.
If your configuration sets `PROVIDER_OVERRIDE_EVENTS`, remove it from your configuration profiles, Docker Compose files, CI settings, and other startup environments.
Any value for this variable now points to an invalid provider configuration and prevents the `events` provider from loading.
:::

## Getting Started

This guide is designed for users new to EventBridge and assumes basic knowledge of the AWS CLI and our [`lstk aws`](/aws/developer-tools/running-localstack/lstk/cloud-and-iac-commands/#aws) command.

Start your LocalStack container using your preferred method.
We will demonstrate creating an EventBridge rule to run a Lambda function when a custom event is published to an event bus.

### Create an EventBridge Bus

First, create a custom EventBridge bus using the [`CreateEventBus`](https://docs.aws.amazon.com/cli/latest/reference/events/create-event-bus.html) API:

```bash
lstk aws events create-event-bus \
    --name my-custom-bus
```

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.

### Create a Lambda Function

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

```js showLineNumbers
'use strict';

exports.handler = (event, context, callback) => {
    console.log('LogEventBridgeEvent');
    console.log('Received event:', JSON.stringify(event, null, 2));
    callback(null, 'Finished');
};
```

Run the following command to create a new Lambda function using the [`CreateFunction`](https://docs.aws.amazon.com/cli/latest/reference/lambda/create-function.html) API:

```bash
zip function.zip index.js

lstk aws lambda create-function \
    --function-name events-example \
    --runtime nodejs16.x \
    --zip-file fileb://function.zip \
    --handler index.handler \
    --role arn:aws:iam::000000000000:role/cool-stacklifter
```

The output will consist of the `FunctionArn`, which you will need to add the Lambda function to the EventBridge target.

### Create an EventBridge Rule

Run the following command to create a new EventBridge rule using the [`PutRule`](https://docs.aws.amazon.com/cli/latest/reference/events/put-rule.html) API:

```bash
lstk aws events put-rule \
    --name my-custom-rule \
    --event-bus-name my-custom-bus \
    --event-pattern '{"source":["my-source"],"detail-type":["my-detail-type"]}' \
    --state ENABLED
```

In the above command, we have specified an event pattern that will match events with:
- `source` field equal to `my-source`
- `detail-type` field equal to `my-detail-type`

This rule will trigger whenever an event matching this pattern is published to the custom event bus.

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:

```bash
lstk aws lambda add-permission \
    --function-name events-example \
    --statement-id my-custom-event \
    --action 'lambda:InvokeFunction' \
    --principal events.amazonaws.com \
    --source-arn arn:aws:events:us-east-1:000000000000:rule/my-custom-bus/my-custom-rule
```

### Add the Lambda Function as a Target

Create a file named `targets.json` with the following content:

```json
[
    {
      "Id": "1", 
      "Arn": "arn:aws:lambda:us-east-1:000000000000:function:events-example"
    }
]
```

Finally, add the Lambda function as a target to the EventBridge rule using the [`PutTargets`](https://docs.aws.amazon.com/cli/latest/reference/events/put-targets.html) API:

```bash
lstk aws events put-targets \
    --rule my-custom-rule \
    --event-bus-name my-custom-bus \
    --targets file://targets.json
```

### Send an Event to Trigger the Lambda

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:

```bash
lstk aws events put-events \
    --entries '[{"Source": "my-source", "DetailType": "my-detail-type", "Detail": "{\"key\": \"value\"}", "EventBusName": "my-custom-bus"}]'
```

This event will match the pattern we defined in the rule and should trigger the Lambda function immediately.

### Verify the Lambda invocation

You can verify the Lambda invocation by checking the CloudWatch logs.

Run the following command to list the CloudWatch log groups:

```bash
lstk aws logs describe-log-groups
```

The output will contain the log group name, which you can use to list the log streams:

```bash
lstk aws logs describe-log-streams \
    --log-group-name /aws/lambda/events-example
```

Alternatively, you can fetch LocalStack logs to verify the Lambda invocation:

```bash title="Output"
lstk logs
...
emulator | 2023-07-17T09:37:52.028  INFO --- [   asgi_gw_0] localstack.request.aws     : AWS lambda.Invoke => 202
emulator | 2023-07-17T09:37:52.106  INFO --- [   asgi_gw_0] localstack.request.http    : POST /_localstack_lambda/97e08ac50c18930f131d9dd9744b8df4/invocations/ecb744d0-b3f2-400f-9e49-c85cf12b1e00/logs => 202
emulator | 2023-07-17T09:37:52.114  INFO --- [   asgi_gw_0] localstack.request.http    : POST /_localstack_lambda/97e08ac50c18930f131d9dd9744b8df4/invocations/ecb744d0-b3f2-400f-9e49-c85cf12b1e00/response => 202
...
```

## Supported target types

At this time LocalStack supports the following [target types](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-targets.html#eb-console-targets) for EventBridge rules:

- Lambda function
- SNS Topic
- SQS queue
- StepFunctions StateMachine
- Firehose
- Event bus
- API destination
- Kinesis
- CloudWatch log group
- API Gateway

## Resource Browser

The LocalStack Web Application provides a Resource Browser for managing EventBridge Buses.
You can access the Resource Browser by opening the LocalStack Web Application in your browser, navigating to the **Resources** section, and then clicking on **EventBridge** under the **App Integration** section.

![EventBridge Resource Browser](/images/aws/eventbridge-resource-browser.png)

The Resource Browser allows you to perform the following actions:

- **View the Event Buses**: You can view the list of EventBridge Buses running locally, alongside their Amazon Resource Names (ARNs) and Policies.
- **Create Event Rule**: You can create a new Event Rule by specifying **Name**, **Description**, **Event Pattern**, **Schedule Expressions**, **State**, **Role ARN**, and **Tags**.
- **Trigger Event**: You can trigger an Event by specifying the **Entries** and **Endpoint Id**.
  While creating an Entry, you must specify **Source**, **Event Bus Name**, **Detail**, **Resources**, **Detail Type**, and **Trace Header**.
- **Remove Selected**: You can remove the selected EventBridge Bus.

## API Coverage


### EventBridge API coverage

Source service: `events`. 41 of 57 tracked operations are implemented.

Service documentation: /aws/services/events/
License availability: available starting with the Hobby plan. See /aws/licensing/ for current plan details.

| Operation | Status |
| --- | --- |
| ActivateEventSource | Not implemented |
| CancelReplay | Implemented |
| CreateApiDestination | Implemented |
| CreateArchive | Implemented |
| CreateConnection | Implemented |
| CreateEndpoint | Not implemented |
| CreateEventBus | Implemented |
| CreatePartnerEventSource | Not implemented |
| DeactivateEventSource | Not implemented |
| DeauthorizeConnection | Not implemented |
| DeleteApiDestination | Implemented |
| DeleteArchive | Implemented |
| DeleteConnection | Implemented |
| DeleteEndpoint | Not implemented |
| DeleteEventBus | Implemented |
| DeletePartnerEventSource | Not implemented |
| DeleteRule | Implemented |
| DescribeApiDestination | Implemented |
| DescribeArchive | Implemented |
| DescribeConnection | Implemented |
| DescribeEndpoint | Not implemented |
| DescribeEventBus | Implemented |
| DescribeEventSource | Not implemented |
| DescribePartnerEventSource | Not implemented |
| DescribeReplay | Implemented |
| DescribeRule | Implemented |
| DisableRule | Implemented |
| EnableRule | Implemented |
| ListApiDestinations | Implemented |
| ListArchives | Implemented |
| ListConnections | Implemented |
| ListEndpoints | Not implemented |
| ListEventBuses | Implemented |
| ListEventSources | Not implemented |
| ListPartnerEventSourceAccounts | Not implemented |
| ListPartnerEventSources | Not implemented |
| ListReplays | Implemented |
| ListRuleNamesByTarget | Implemented |
| ListRules | Implemented |
| ListTagsForResource | Implemented |
| ListTargetsByRule | Implemented |
| PutEvents | Implemented |
| PutPartnerEvents | Not implemented |
| PutPermission | Implemented |
| PutRule | Implemented |
| PutTargets | Implemented |
| RemovePermission | Implemented |
| RemoveTargets | Implemented |
| StartReplay | Implemented |
| TagResource | Implemented |
| TestEventPattern | Implemented |
| UntagResource | Implemented |
| UpdateApiDestination | Implemented |
| UpdateArchive | Implemented |
| UpdateConnection | Implemented |
| UpdateEndpoint | Not implemented |
| UpdateEventBus | Implemented |
