Configuring an EventBridge Rule for EC2 Instance State Changes
Begin by opening the Amazon EventBridge console and navigating to the Event buses section. Every AWS account has a default event bus. Although you can create custom event buses, this demo uses the default.
- Event Pattern: The rule triggers when an incoming event matches a defined pattern.
- Schedule: The rule executes based on a cron-like schedule.

Defining the Event Pattern
Select AWS services as the event source, choose EC2 as the service, and pick EC2 instance status, instance state change notification as the event type. By default, this pattern covers any state change:Keep the event pattern broad during testing for ease of debugging. You can narrow it down once you’re confident the event rule works.
Specifying the Target
In the next step, define the target for the rule. For this demo, select a Lambda function (for example, one named “test one”). Use the sample Lambda function code provided below to log event data and inspect details via CloudWatch logs:Testing the EC2 Instance State Change
To verify your setup, open the EC2 console and change the state of a running instance. For instance, select the instance, then choose Instance state > Stop instance. Allow a few seconds for the instance state to update.
Creating and Testing a Custom Event
Now, let’s demonstrate how to generate custom events from your application. We have set up a basic Node.js API using the AWS SDK to publish events to EventBridge. In this example, a POST request to the/signup endpoint triggers an event.
Node.js API Setup
Below is the code snippet for creating the API:{ "username": "user2", "password": "password123" }), the API sends an event to EventBridge with the source "my-app" and the detail type "New User", along with the user details.
Configuring the Custom Event Rule
- Open the EventBridge console and create a new rule (e.g., “MyAPIEvent”).
-
Use the following custom event pattern:
- Set the target for the rule to another Lambda function (for example, “test two”).
- Review the configuration and click Create rule.
Manually Testing the Custom Event
To test the custom event directly via the EventBridge console:- Navigate to Event Bus and click Send event.
-
Select the default bus, set the Source to
"my-app", and the Detail type to"New User". -
In the Detail section, provide a JSON object such as:
- Click Send event.
Testing the API Integration
Finally, start your Node.js API and send a POST request to the/signup endpoint containing the new user’s information. For example, using a REST client, submit the following JSON:
"username": "user2".



Ensure that each step has been tested thoroughly to confirm that both AWS service events and custom application events are processed properly by your Lambda functions.