Tutorial: Send events to an Amazon Kinesis stream using EventBridge - Amazon EventBridge

Tutorial: Send events to an Amazon Kinesis stream using EventBridge

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.

Prerequisites

In this tutorial you 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.

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

  1. Open the Amazon EventBridge console at https://console.aws.amazon.com/events/.

  2. In the navigation pane, choose Rules.

  3. Choose Create rule.

  4. Enter a name and description for the rule.

  5. For Define pattern, do the following:

    1. Choose Event pattern.

    2. Choose Pre-defined pattern by service.

    3. For Service provider, choose AWS.

    4. For Service Name, choose EC2.

    5. For Event type, choose Instance State-change Notification.

    6. Choose Specific state(s), running.

  6. For Select event bus, choose AWS default event bus. When an AWS service in your account emits an event, it goes to your account’s default event bus.

  7. For Targets, choose Kinesis stream.

  8. For Stream, select the stream that you created in step 1.

  9. Choose Create a new role for this specific resource.

  10. Choose Create.

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

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. Launch an instance. For more information, see Launch Your Instance in the Amazon EC2 User Guide for Linux Instances.

  3. Open the Amazon EventBridge console at https://console.aws.amazon.com/events/.

  4. In the navigation pane, choose Rules.

    Choose the name of the rule that you created and choose Metrics for the rule.

  5. (Optional) When you're finished, terminate the instance. For more information, see Terminate Your Instance in the Amazon EC2 User Guide for Linux Instances.

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

  1. 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=" }
  2. 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.

  3. 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

  1. Open the Rules page of the EventBridge console.

  2. Select the rule that you created.

  3. Choose Delete.

  4. Choose Delete.

To delete the Kinesis stream

  1. Open the Data streams page of the Kinesis console.

  2. Select the stream that you created.

  3. Choose Actions, Delete.

  4. Enter delete in the fiekd and choose Delete.