Guided Lab: Getting Started with Amazon EventBridge Events
Description
Amazon EventBridge is a serverless event bus service that enables you to connect your applications with data from various sources. It simplifies building event-driven architectures by routing events from sources like AWS services, custom applications, and SaaS applications to targets such as AWS Lambda functions, Amazon SNS topics, and more.
In this lab, you will learn how to use Amazon EventBridge to trigger AWS services, specifically an AWS Lambda function. The Lambda function will be designed to receive and print EC2 instance state change events. By the end of this lab, you will have a working example of how EventBridge can monitor AWS resources and trigger actions based on events.
Prerequisites
This lab assumes you have a basic knowledge and understanding of Amazon EvnetBridge, AWS Lambda, and Amazon EC2 Service.
If you find any gaps in your knowledge, consider taking the following lab:
Objectives
By the end of this lab, you will:
- Understand the basics of Amazon EventBridge and its role in event-driven architectures.
- Create an AWS Lambda function that processes events.
- Set up an Amazon EventBridge rule to trigger the Lambda function based on EC2 instance state changes.
- Test the setup by performing actions on an EC2 instance and observing the Lambda function’s responses.
Lab Steps
Create the AWS Lambda Function
1. Navigate to the AWS Lambda Console.
2. Create a new Lambda function using the following configurations:
- Choose Author from scratch.
- Function name: myLambdaFunction
- Select Python 3.12 as the runtime.
- Execution role:
- Select Use an Existing Role: PlayCloud-Sanbox
- Click Create function
3. Replace the existing code with the following:
import json
def lambda_handler(event, context):
# Print the event as a single line JSON string
print("Received event: " + json.dumps(event, separators=(',', ':')))
return {
'statusCode': 200,
'body': json.dumps('Event processed.')
}
This function logs the incoming event in a single line and confirms its successful processing with a simple return message. It’s designed to be triggered by EventBridge and to display event details in a compact, readable format in CloudWatch Logs.
4. Click Deploy to save the changes.
Create the Amazon EventBridge Rule
1. Navigate to the EventBridge Dashboard.
2. Click on Rules in the left navigation pane.
3. Click Create rule.
4. Define Rule Details.
- Rule details:
- Name: EC2StateChangeRule
- Description: (Optional) Triggers Lambda function on EC2 instance state changes.
- Event bus:
- Keep it as default.
- Rule type:
- Select a Rule with an event pattern.
Hint: Click on the Info to read about the definition of an Event bus
- Select a Rule with an event pattern.
and Rule Type
- Click on Next.
5. Build the Event Pattern
NOTE: An event pattern in Amazon EventBridge defines the criteria for selecting specific events to match and process. It allows you to filter events based on their source and content. When an event matches the defined pattern, EventBridge routes it to the specified target, such as an AWS Lambda function, based on the details you specify.
In this lab, the event pattern will be set up to match EC2 instance state changes. Whenever an EC2 instance changes state (e.g., starts, stops, or terminates), EventBridge will trigger the Lambda function to log the event.
a. Under Event source:
-
-
- Choose AWS events or EventBridge partner events.
-
b. Explore the Sample event – optional.
-
-
- Select a Sample event type like AWS events and Sample events like EC2 Instance Launch Successful
-
-
-
- Review and familiarize yourself with different Event JSON patterns.
-
c. Under Creation method: select Use pattern from
d. Under the Event pattern, follow the following configurations:
-
-
- Event source: AWS services
- AWS service: EC2
- Event type: select EC2 Instance State-change Notification
- Event Type Specification 1: Choose Any state to capture all state transitions (start, stop, terminated, etc.).
Optionally, you could specify particular states by selecting Specific state(s). But for the simplicity of this lab, choose Any instance - Event Type Specification 2: Choose Any instance to capture state changes for all EC2 instances.
Optionally, you could specify particular instance IDs by selecting Specific instance Id(s). However, for the purposes of this lab, use Any instance
-
e. Click Next.
6. Select Target(s)
NOTE: In Amazon EventBridge, a target is the AWS resource or service that will receive the event when the pattern matches. Targets can be services such as Lambda functions, Step Functions, SNS topics, SQS queues, and more. Defining a target enables you to create a workflow or automated response based on specific events. In this lab, the target will be a Lambda function.
a. Under Target 1, select AWS service.
b. Select a target: Select Lambda function
-
-
- Select the Lambda Function created earlier, i.e. myLambdaFunction
-
c. Click on Next
7. Configure Tags (Optional)
We will skip adding tags in this lab. Tags can be useful for resource management and cost allocation but are not required for this exercise.
8. Review and Create
a. Review all the settings to ensure they are correct.
b. Click Create rule.
Testing the Setup
Step1: Create an EC2 Instance
1. Navigate the EC2 Dashboard.
2. Launch an EC2 Instance using the following configurations:
- Name: Name your instance (e.g., TestInstance).
- AMI: Amazon Linux
- Instance type: Select t2.micro.
- Key pair: Proceed without a key pair
- Leave the rest as default
3. Click Launch instance.
Step 2: Monitor Lambda Function Logs
1. Navigate to the Lambda Function (myLambdaFunction) created earlier.
2. Navigate to the Monitor tab and click on View CloudWatch logs.
3. You will be redirected to the CloudWatch logs for this lambda function.
4. Scroll down and view the latest log stream.
5. You should see logs indicating that the Lambda function was triggered by the EC2 instance entering the pending and running states.
Step 3: Stop the EC2 Instance
1. Search the EC2 console in the search bar. Open it in a new tab.
2. Select your instance.
3. Click on Instance state > Stop instance.
4. Confirm the action.
5. Wait for the instance to stop.
Step 4: Check Lambda Logs Again
1. Navigate back to the CloudWatch logs tab for your Lambda function.
2. Refresh the page.
Note: If you can’t see a new log, the logs might be written on a different log stream. Go back to the /aws/lambda/myLambdaFunction page. You should see a new Log stream. Click on it
a. You should see logs showing the stopping and stopped events.
Step 5: Start the EC2 Instance
1. In the EC2 console, select your instance.
2. Click on Instance state > Start instance.
3. Wait for the instance to enter the running state.
Step 6: Verify Lambda Logs
1 Similar to the previous step, refresh the CloudWatch log stream.
2. Confirm that the Lambda function captured the pending and running events again.

Step 7: Terminate the EC2 Instance
1. In the EC2 console, select your instance.
2. Click on Instance state > Terminate instance.
3. Confirm the action.
4. Wait for the instance to be terminated.
Step 8: Final Log Verification
1. Refresh the CloudWatch log stream one last time.
2. Verify that the Lambda function logs the shutting-down and terminated events.
Note: If you can’t see a new log, the logs might be written on a different log stream. Go back to the /aws/lambda/myLambdaFunction page. You should see a new log stream. Click on it.
Congratulations! You’ve successfully set up an Amazon EventBridge rule to monitor EC2 instance state changes and trigger an AWS Lambda function in response. You observed how EventBridge captures events from AWS services and routes them to specified targets, enabling you to build reactive, event-driven applications.
By understanding the integration between EventBridge and Lambda, you can extend this pattern to monitor other AWS services and trigger actions based on a wide range of events, enhancing the automation and responsiveness of your applications.
As a final step, remember it’s always a best practice to clean up resources after completing a lab. Deleting unused resources will help maintain a clutter-free AWS environment.
Happy learning!!!