Sitemap

How to create AWS Kinesis Data Stream from scratch

6 min readNov 21, 2023

This blog will give you a walkthrough of how to create an AWS Kinesis Data Stream from scratch.

Press enter or click to view image in full size
Photo by Daniel Eledut on Unsplash

Head on to the AWS Home Console and search for IAM in the search bar and select IAM Roles.

Press enter or click to view image in full size

Select Trusted Entities

Search for Lambda under “Use Case” and select it.

Press enter or click to view image in full size

Click the orange next button and go to the Add Permissions tab.

Add Permissions

Here, you can add the below permissions:

  • AmazonKinesisFullAccess
  • AmazonS3FullAccess
  • CloudWatchFullAccess
Press enter or click to view image in full size

Name, Review and Create

Add the following:

  • Role name: first-data-stream-role
Press enter or click to view image in full size

Review and leave everything else to the default and hit Create Role!

The Role should then be created.

Create Lambda Function

Search for Lambda function in the Search Bar.

Press enter or click to view image in full size

Hit Create function.

Press enter or click to view image in full size

Create Producer Function

  • Function Name: producer
  • Architecture: x86_64
  • Change default execution role > use an existing role > first-data-stream-role
  • Hit Create Function!
Press enter or click to view image in full size

Producer has now been successfully created.

Create Consumer 1 & Consumer 2 Functions

Consumer1

Press enter or click to view image in full size

Consumer2

Press enter or click to view image in full size

Review producer, consumer1, consumer2

Press enter or click to view image in full size

Create S3 bucket

Search for S3 in the search bar.

Press enter or click to view image in full size

Select Create Bucket

Create bucket with unique name. I have named mine anishkinesis.

AWS Region: Select the one that is closest to you.

Note: So, the region of lambda function and AWS Bucket has to match. But, for now to keep it standard, let’s select US East — 1 for now.

Press enter or click to view image in full size

The bucket will show up in the S3 Buckets Tab.

Press enter or click to view image in full size

Select the bucket you made and Go to the properties tab.

Press enter or click to view image in full size

Create Event Notification on S3 Bucket

Do Ctrl + F on the page and search for Event Notifications. Here, select “Create Event Notifications”

Press enter or click to view image in full size

General Configuration

Input the following:

Press enter or click to view image in full size

Event Types

Under Event Types, make the “All object create events”

Press enter or click to view image in full size

Destination

Next, Ctrl+F and search for the Destination tab.

Select the Lambda Function, and select producer.

Press enter or click to view image in full size

Note: In case producer is not available, go to the Lambda functions tab in AWS and see the location. I had an error where the S3 bucket and Lambda function regions were not matching.

Press enter or click to view image in full size
Press enter or click to view image in full size

Create Kinesis Data Streams

Search for Kinesis on the search bar.

Press enter or click to view image in full size

Select Create Data Stream.

Press enter or click to view image in full size

Since we have 2 consumers, select 2 shards.

Press enter or click to view image in full size

Hit Create and now, the Kinesis Data Stream should be created.

Press enter or click to view image in full size

Now, go to Configuration

Press enter or click to view image in full size

Select Edit Encryption

Press enter or click to view image in full size

Select AWS-managed CMK.

Press enter or click to view image in full size

Write Code for the producer

Now we will write the Producer code in Javascript (Don’t worry if you don’t know JS. This code can be used for all data flows )

Go to the Lambda function for producer and you will see the code window below. Paste the following code. You will see a change in the function overview. You will notice the S3 bucket we attached is now present.

Press enter or click to view image in full size
const AWS = require("aws-sdk");
AWS.config.update({
region: "eu-north-1"
})

const s3 = new AWS.S3();
const kinesis = new AWS.Kinesis();

exports.handler = async (event) => {
console.log(JSON.stringify(event));
const bucketName = event.Records[0].s3.bucket.name;
const keyName = event.Records[0].s3.object.key;
const params = {
Bucket: bucketName,
Key: keyName
}

await s3.getObject(params).promise().then(async (data) => {
const dataString = data.Body.toString();
const payload = {
data: dataString
}

await sendToKinesis(payload, keyName);
}, error => {
console.error(error);

})

};

async function sendToKinesis(payload, partitionKey) {
const params = {
Data: JSON.stringify(payload),
PartitionKey: partitionKey,
StreamName: "datastream1"
}

await kinesis.putRecord(params).promise().then(response => {
console.log(response);
}, error => {
console.error(error);
})
}

Consumer1 Code

Write the code for consumer1

export const handler = async (event) => {
console.log(JSON.stringify(event));
for (const record of event.Records) {
const data = JSON.parse(Buffer.from(record.kinesis.data, 'base64'));
// send emails to clients, publisht the data to social media
console.log('consumer #1', data);
}
};

Deploy the changes. Then, go to add trigger in the consumer1 function overview.

Press enter or click to view image in full size

Select Kinesis.

Press enter or click to view image in full size

Select the right configurations as shown below.

Press enter or click to view image in full size

Upload file to S3 Bucket

Type S3 in the search panel and go to your bucket.

Press enter or click to view image in full size

Make a .txt file with any 2–3 lines of your choice of words. I can show you a sample below.

This is a sample script for Data Engineering
You can reach out to me on LinkedIn
https://www.linkedin.com/in/anishmahapatra/

Go to your bucket, drag and drop the file, or select the upload button and select the file.

You will be able to see the data in the bucket now, as shown below.

Press enter or click to view image in full size

--

--

Anish Mahapatra
Anish Mahapatra

Written by Anish Mahapatra

Senior AI & ML Engineer | Fortune 500 | Senior Technical Writer - Google me. Anish Mahapatra | https://www.linkedin.com/in/anishmahapatra/

No responses yet