Tutorial: Send events to Amazon Kinesis using EventBridge schemas
You can send AWS API call events in EventBridge to an Amazon Kinesis stream, create Kinesis Data Streams applications, and process large amounts of data. In this tutorial, you create a Kinesis stream, and then create a rule in the EventBridge console that sends events to that stream when an Amazon EC2 instance stops.
Steps:
Prerequisites
In this tutorial, you'll use the following:
Use the AWS CLI to work with Kinesis streams.
To install the AWS CLI, see the Installing, updating, and uninstalling the AWS CLI version 2.
Note
This tutorial uses AWS events and the built in aws.events
schema registry.
You can also create an EventBridge rule based on the schema of your custom events by adding them
to a custom schema registry manually, or by using schema discovery.
For more information on schemas, see Amazon EventBridge schemas. For more information on creating a rule using other event pattern options, see Creating rules that react to events in Amazon EventBridge.
Step 1: Create an Amazon Kinesis stream
To create a stream, at a command prompt, use the create-stream
AWS CLI command.
aws kinesis create-stream --stream-name
test
--shard-count 1
When the stream status is ACTIVE
, the stream is ready. To check the
stream status, use the describe-stream
command.
aws kinesis describe-stream --stream-name
test
Step 2: Create a rule
Create a rule to send events to your stream when you stop an Amazon EC2 instance.
To create a rule
Open the Amazon EventBridge console at https://console.aws.amazon.com/events/
. -
In the navigation pane, choose Rules.
-
Choose Create rule.
-
Enter a name and description for the rule. For example, name the rule
TestRule
-
For Event bus, select default.
-
For Rule type, choose Rule with an event pattern.
-
Choose Next.
-
For Event source, choose AWS events or EventBridge partner events.
-
For Creation method, choose Use schema.
-
For Event pattern, do the following:
-
For Schema type, choose Select schema from Schema registry.
-
For Schema registry, choose aws.events from the drop-down list.
-
For Schema, choose aws.ec2@EC2InstanceStateChangeNotification from the drop-down list.
EventBridge displays the event schema under Models.
EventBridge displays a red asterisk next to any properties that are required for the event, not for the event pattern.
-
In Models, set the following event filter properties:
Select + Edit next to the state property.
Leave Relationship empty. For Value, enter
running
. Choose Set.Select + Edit next to the source property.
Leave Relationship empty. For Value, enter
aws.ec2
. Choose Set.Select + Edit next to the detail-type property.
Leave Relationship empty. For Value, enter
EC2 Instance State-change Notification
. Choose Set.
-
To view the event pattern you've constructed, choose Generate event pattern in JSON
EventBridge displays the event pattern in JSON:
{ "detail": { "state": ["running"] }, "detail-type": ["EC2 Instance State-change Notification"], "source": ["aws.ec2"] }
-
-
Choose Next.
-
For Target types, choose AWS service.
-
For Select a target, choose Kinesis stream from the drop-down list.
-
For Stream, select the Kinesis stream that you created in the Step 1: Create an Amazon Kinesis stream section. In this example, select
test
. -
For Execution role, choose Create a new for role for this specific resource.
-
Choose Next.
-
Choose Next.
-
Review the details of the rule and choose Create rule.
Step 3: Test the rule
To test your rule, stop an Amazon EC2 instance. Wait a few minutes for the instance to stop, and then check your CloudWatch metrics to verify that your function ran.
To test your rule by stopping an instance
Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/
. -
Launch an instance. For more information, see Launch Your Instance in the Amazon EC2 User Guide.
Open the Amazon EventBridge console at https://console.aws.amazon.com/events/
. -
In the navigation pane, choose Rules.
Choose the name of the rule that you created and choose Metrics for the rule.
-
(Optional) When you're finished, terminate the instance. For more information, see Terminate Your Instance in the Amazon EC2 User Guide.
Step 4: Verify that the event was sent
You can use the AWS CLI to get the record from the stream to verify that the event was sent.
To get the record
-
To start reading from your Kinesis stream, at a command prompt, use the
get-shard-iterator
command.aws kinesis get-shard-iterator --shard-id shardId-000000000000 --shard-iterator-type TRIM_HORIZON --stream-name
test
The following is example output.
{ "ShardIterator": "AAAAAAAAAAHSywljv0zEgPX4NyKdZ5wryMzP9yALs8NeKbUjp1IxtZs1Sp+KEd9I6AJ9ZG4lNR1EMi+9Md/nHvtLyxpfhEzYvkTZ4D9DQVz/mBYWRO6OTZRKnW9gd+efGN2aHFdkH1rJl4BL9Wyrk+ghYG22D2T1Da2EyNSH1+LAbK33gQweTJADBdyMwlo5r6PqcP2dzhg=" }
-
To get the record, use the following
get-records
command. Use the shard iterator from the output in the previous step.aws kinesis get-records --shard-iterator
AAAAAAAAAAHSywljv0zEgPX4NyKdZ5wryMzP9yALs8NeKbUjp1IxtZs1Sp+KEd9I6AJ9ZG4lNR1EMi+9Md/nHvtLyxpfhEzYvkTZ4D9DQVz/mBYWRO6OTZRKnW9gd+efGN2aHFdkH1rJl4BL9Wyrk+ghYG22D2T1Da2EyNSH1+LAbK33gQweTJADBdyMwlo5r6PqcP2dzhg=
If the command is successful, it requests records from your stream for the specified shard. You can receive zero or more records. Any records returned might not represent all records in your stream. If you don't receive the data that you expect, keep calling
get-records
. -
Records in Kinesis are encoded in Base64. Use a Base64 decoder to decode the data so that you can verify that it's the event that was sent to the stream in JSON form.
Step 5: Clean up your resources
You can now delete the resources that you created for this tutorial, unless you want to retain them. By deleting AWS resources that you are no longer using, you prevent unnecessary charges to your AWS account.
To delete the EventBridge rule(s)
-
Open the Rules page
of the EventBridge console. -
Select the rule(s) that you created.
-
Choose Delete.
-
Choose Delete.
To delete the Kinesis stream(s)
-
Open the Data streams page
of the Kinesis console. -
Select the stream(s) that you created.
-
Choose Actions, Delete.
-
Enter delete in the fiekd and choose Delete.