Amazon EventBridge — The Biggest Serverless Service Announcement in AWS??
So what is this EventBridge? A newly serverless service! One more big change in serverless world?? so let’s checkout!
Amazon EventBridge is a serverless event bus service that makes it easy to connect your applications with data from a variety of sources.
EventBridge delivers a stream of real-time data from your own applications, SOFTWARE-AS-A-Service (SAAS) applications, and AWS services and routes that data to targets such as AWS Lambda.
You can set up routing rules to determine where to send your data to build application architectures that react in real time to all of your data sources. EventBridge allows you to build event driven architectures, which are loosely coupled and distributed.
FYI: EventBridge was formerly called Amazon CloudWatch Events.
Also good to knowthat, Existing CloudWatch Events users can access their existing default bus, rules, and events in the new EventBridge console and in the CloudWatch Events console. EventBridge uses the same CloudWatch Events API, so all of your existing CloudWatch Events API usage remains the same.
But what makes it different?
EventBridge includes new features that enable you to receive events from SAAS partners and your own applications.
Also you can configure the following AWS resources as targets for EventBridge:
- Lambda functions
- Amazon EC2 instances
- Streams in Amazon Kinesis Data Streams
- Delivery streams in Amazon Kinesis Data Firehose
- Log groups in Amazon CloudWatch Logs
- Amazon ECS tasks
- Systems Manager Run Command
- Systems Manager Automation
- SNS, SQS, Pipelines in CICD
- Step Functions
There are small-small concepts which you should know before we go in deep with EventBridge. Let’s look into those.
1- Events: In basic terms, An event indicates a change in an environment. So what kind of change it is? This can be an AWS environment, an SAAS partner service or application, or one of your own custom applications or services.
Let’s take an example for it!
AWS EC2 generates an event when the state of an EC2 instance changes from pending to running, and Amazon EC2 ASG generates events when it launches or terminates instances. AWS CloudTrail publishes events when you make API calls. You can also set up scheduled events that are generated on a periodic basis.
2- Rules — A rule matches incoming events and routes them to targets for processing(same as CloudWatch Events). A single rule can route to multiple targets(It can be Lambda, SNS, SQS etc), all of which are processed in parallel.
3- Targets- As I mentioned above in Rules, A target processes events. Targets can include Amazon EC2 instances, Lambda functions, Kinesis streams, Amazon ECS tasks, Step Functions state machines, Amazon SNS topics, Amazon SQS queues, and built-in targets. A target receives events in JSON format.
4- Event buses — An event bus receives events. When you create a rule, you associate it with a specific event bus, and the rule is matched only to events received by that event bus.
“Your account has one default event bus, which receives events from AWS services.” You can create custom event buses to receive events from your custom applications.
Let’s start with Hands-On
Getting Started with Amazon EventBridge:
In this hands-on, we’ll create a Rule which will trigger AWS resource.
Creating an EventBridge Rule That Triggers on an Event from an AWS Resource:
Go to AWS Console & Search for EventBridge, which looks like.
Click on Create Rule, Give the Name of Rule & Select the Define Pattern.
There is 2 types of pattern.
1- Event Pattern (AWS Service, Service partners & all events)
2- Schedule (Corn Job)
Here we’re selecting Event Pattern.
When we select the event pattern, there is 2 type of Event Matching Pattern. Pre-defined pattern by service & Custom pattern.
In Custom pattern, we can write code in our language.
In Pre-defined pattern by service, we have 3 Types of Service provider. AWS, All Events, Service Partners.
AWS service provider has its own list of services, where as service partners have following list of service.
I’m selecting AWS service provider & Service type EC2, we’re selecting Instance state change, so here state can be any or can mention some specific also like running, stopped, terminating. I’m selecting any, also we can choose for any instance or can mention some specific instance.
Now, For Select event bus, choose the event bus that you want to associate with this rule. If you want this rule to trigger on matching events that come from your own AWS account, select AWS default event bus. When an AWS service in your account emits an event, it always goes to your account’s default event bus.
For Select targets, choose the AWS service that is to act when an event of the selected type is detected. Here, I have selected the SNS topic as target & already have 1 Topic i.e test in my account which has email subscription.
Click on Create to create this rule.
If we see other options available left top side then we have Events Bus in Events.
Every account has one default bus. Event buses receive events from a variety of sources and match them to rules in your account.
I have started this default bus which has Schema Discovery in started mode.
If we see in Rules of Default event bus, we have new rule “Schemas” which is used to route Events to Schema Discoverer.
Let’s go to test our rule i.e “TestRule”, we have already one instance running in EC2 & it’s in running state. Let’s go ahead & stop it.
As we have already seen that we have added SNS as target above which has email subscription. Now when I stopped the instance, it will trigger that rule & send an email.
Important: One more point I want to add here is Log the State of an Amazon EC2 Instance Using EventBridge. In this case, you have to change your target type from SNS to lambda.
You can create an AWS Lambda function that logs the changes in state for an Amazon EC2 instance.
Go to Lambda console & create a new function.
Select “Use a Blueprint” & search by “hello-world” keyword in this.
Give the basic information about lambda i.e Function name & create function.
I have edited the EventBridge Rule & set lambda as target.
Now we’ll stop our instance & check in AWS Cloudwatch log groups.
So we can log our ec2 instance state change in this way.
If we go to IAM role & check, AWS has automatically create one IAM role for us which has different Roles action like Delete Rule, Put Rule etc.
Note: You can also receive Events from an SaaS Partner. To be able to receive events from SaaS partner applications and services, you must have a partner event source offered to you from the partner. You can then create a partner event bus and match it to the corresponding partner event source.
In the navigation pane, choose Partner event sources.
As we see, we have one default event bus, which receives events emitted by AWS services. You can also configure your custom applications to send events to the default event bus.
You can create two types of additional event buses in your account:
Partner event buses: which can receive events from applications and services created by AWS software as a service (SAAS) partners. To receive events from SAAS partners, you need to create a partner event bus for each partner event source that you want to receive events from.
Custom event buses: which can receive events from your custom applications and services. Each event bus in your account can have up to 100 EventBridge rules associated with it, so if your account has many rules, you might want to create custom event buses to associate with some of the rules for your custom application events.
That’s all for EventBridge. Hope it’ll be useful for you.