Streaming messages from producer to consumer using Amazon MSK and create an event source to msk using Lambda
Tags: aws, awsmsk, awslambda, awsiam
Published: 2021-12-11 18:06:01

“Challenges faced to find the solution of how to stream the messages in the client machine”. I have found Apache Kafka to do the same activity. But as we all know that with streaming of messages, we should have high availability, compatibility and security as well if we are deploying the solution on Cloud. So I have chosen the solution as AWS Managed Streaming for Apache Kafka which is a highly available and secure service in AWS. As per cost perspective, it is cheap service which charges based on Kafka storage and broker hours. Also able to add MSK as an event source as trigger in lambda to get log streams in cloudwatch.

Amazon MSK is a fully managed, highly available, and secure service that makes it easy for developers and DevOps managers to run applications on Apache Kafka in the AWS Cloud without needing Apache Kafka infrastructure management expertise. Amazon MSK operates highly available Apache Kafka clusters, provides security features out of the box, is fully compatible with open-source versions of Apache Kafka allowing existing applications to migrate without code changes, and has built-in AWS integrations that accelerate application development. To learn more, read the [Amazon MSK](https://docs.aws.amazon.com/msk/latest/developerguide/what-is-msk.html). 

Apache Kafka (Kafka) is an open-source platform that enables customers to capture streaming data like clickstream events, transactions, IoT events, application and machine logs, and have applications that perform real-time analytics, run continuous transformations, and distribute this data to data lakes and databases in real time. You can use Kafka as a streaming data store to decouple applications producing streaming data (producers) from those consuming streaming data (consumers).

AWS Lambda is a serverless compute service that lets you run code without provisioning or managing servers, creating workload-aware cluster scaling logic, maintaining event integrations, or managing runtimes. With Lambda, you can run code for virtually any type of application or backend service - all with zero administration. Just upload your code as a ZIP file or container image, and Lambda automatically and precisely allocates compute execution power and runs your code based on the incoming request or event, for any scale of traffic. You can set up your code to automatically trigger from 140 AWS services or call it directly from any web or mobile app. You can write Lambda functions in your favorite language (Node.js, Python, Go, Java, and more) and use both serverless and container tools, such as AWS SAM or Docker CLI, to build, test, and deploy your functions. To learn more, read the [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html).

In this post, you will get to know how to stream messages from producer to consumer using Amazon MSK and create an event source to msk using Lambda. Here I have used managed streaming for apache kafka to stream messages from producer to consumer and also created a trigger as an event source for msk in lambda to get record of messages as log streams in cloudwatch.

#Prerequisites
You’ll need an Amazon EC2 Server for this post. [Getting started with amazon EC2](https://aws.amazon.com/ec2/getting-started/) provides instructions on how to launch an EC2 Server.

You’ll also need a VPC with two public and private subnets. [Getting started with VPC](https://docs.aws.amazon.com/vpc/latest/userguide/what-is-amazon-vpc.html) provides instructions on how to create a VPC, Private and Public Subnet, Internet Gateway, NAT Gateway and Route Table. For this blog, I assume that I have two ec2 servers and a VPC created.

#Architecture Overview
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j5gt4nn1exagv2o7vnhy.png)
The architecture diagram shows the overall deployment architecture with data flow, ec2 server, Amazon VPC, Amazon Managed Streaming for Kafka, AWS Lambda and Amazon CloudWatch.

#Solution overview
The blog post consists of the following phases:
1. Create a MSK Cluster with custom created VPC
2. Installation of Apache Kafka client libraries and tools on the client machine (ec2 servers) and create of topic
3. Setting up lambda function that uses the Amazon MSK Cluster with topic as an event source and create a IAM role with required permissions
4. Testing output with consumer server from producer server and checking for log streams of MSK streams in cloudwatch

I have a VPC and two ec2 server as below →
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wwceacka65mhbvi8ge3k.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/311wiip6uki6gqkf3dr9.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t5p2cw07asw9o6dwmerx.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gk17ku76fvneq8ncsuwv.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ioon0gqa4dqwszsov7l.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g4eoqhv6ewhhnya6umj7.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3u3p2vm36v9mlv3g8tsy.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d4sxj1b7dhicnda0qr0r.png)

##Phase 1: Create a MSK Cluster with custom created VPC 
1. Open the console, click on create cluster named MSk-Cluster. Select the kafka version and  choose msk default configuration option. In the networking section, select the custom vpc created earlier, number of zones as 2, zones and its private subnets, security group.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g9n10b3256xme961buwu.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/93qi3hfd2o0twc0p59vc.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ku35e678m1u35p1ufowy.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xud315epr1gv0k1y4yki.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/78d76t7yvyrhkvu9kn2b.png)
2. In the broker section, select the broker type as required and number of brokers per zone. In storage, input the ebs storage required. In the security section, select TLS and plaintext both methods. And leave other settings as default and you can customize the other settings if required as well.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/34kz5rd1ula8fvufwofr.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ku2u46up798q62snk18.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kl5g1yhi0tw17uzu7eml.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fky0ir1upl4jtlsi9it8.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j69ey8c2vr3fwrh89bd7.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/agi14jxvw5r2ryw9za3l.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hufe3jyia0gzigjpuhdn.png)
3. Once the cluster gets created, we can view the client information as bootstrap servers and apache zookeeper connection. Also able to see the cluster created configurations and broker details.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qjsoldcfye5ky6n6smml.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dbzb03qh9mgbyl9nthlp.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mmykd22uzi61gcd1ibqc.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/atwuw02wsdmyo4vl7p2n.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/erld4vywlbmuco29j7hg.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hglfon79q8rmwetdt7t1.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qi71v1955w6fng0lhp27.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ieezd5vxxqzmthn407v.png)

##Phase 2: Installation of Apache Kafka client libraries and tools on the client machine (ec2 servers) and create of topic
1. Connect the producer and consumer server via ssh. Install java and kafka client libraries. Also configured cacerts as certificates in server for kafka client. And create a topic using topic name and replication factor.  
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jpxkvpu160rc3zj7kg04.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jaihq8x8fzvvdi3d8qf9.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xu45q1avg8cb0o72m3jo.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sjtx80m5drruuca6h5nw.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3xsyojv6zty2e0fz3tu6.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zejw996wcr866e1471vz.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1hbua0xr9jgw9tc2or9i.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p8ksmnbcsamx2h4i64zw.png)

##Phase 3: Setting up lambda function that uses the Amazon MSK Cluster with topic as an event source and create a IAM role with required permissions  
1. Create IAM role with required managed policies and Create a lambda function as MSK-Function with existing IAM role. Add a trigger in lambda with cluster name and topic. Also deployed code for getting records in cloudwatch as in log streams. And also configured VPC in lambda as with custom vpc, private subnets and custom security group.  
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1rp0iwlc45petp8s1kak.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4v9kczpk8wnruogppdw8.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8lde0z3f6o4amnacb5zl.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p4sfajfv91l9kbab3j0y.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4q1jdo7rc7azj23kwlvx.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0cpsp2wh877dgm529eau.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xpgumtz7tyud4iiz41ln.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dv97feta1rxsfqn5m9mq.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vso1bcfykpyobz596p89.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xxdvt4t0ybt9eoou8hn9.png)

##Phase 4: Testing output with consumer server from producer server and checking for log streams of MSK streams in cloudwatch 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w8vk54dr1p4buyui5bwe.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3um7ptm132bvtygvzp69.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qy8wmmiouvuc9e6r36i7.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4avwqxedg1eybm38p29b.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9il127mpxkfeezk3xemp.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jndwu438aziwcf6a535v.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p34suq89kru2v2v9k43a.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cx48gc9q7rzfhewz4f7l.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v8wf0rs8h7jn3j08nl40.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x4db0pkvigg0l0za26k0.png)

#Clean-up
Delete AWS Managed Streaming for Apache Kafka, AWS Lambda, EC2, VPC, IAM, Cloudwatch Log group.

#Pricing
I review the pricing and estimated cost of this example. 
Cost of Managed Streaming for Apache Kafka → 
USD 0.221 per broker hour for Kafka.m5.large:RunBroker = 12.487 hours = $2.76
USD 0.114 per GB-Month for Kafka.Storage.GP2:RunVolume = 1.678 GB-Mo = $0.19
Total = ($2.76+$0.19) =$2.95
Cost of EC2 = $0.43 per 17.455 Hrs + $0.01 per 0.122 GB-Mo = $0.44
Cost of Lambda = $0.0 
Cost of Data Transfer = $0.0 
Cost of Key Management Service = $0.0
Cost of Cloudwatch = $0.0
Cost of Simple Notification Service = $0.0
Total Cost = $(2.95+0.44+0.0+0.0+0.0+0.0+0.0) = $3.39

#Summary
In this post, I showed “how to do streaming of messages from producer to consumer using Amazon MSK and create an event source to msk using Lambda”.

For more details on AWS Managed Streaming for Apache Kafka, Checkout Get started AWS Managed Streaming for Apache Kafka, open the [AWS Managed Streaming for Apache Kafka console](https://ap-south-1.console.aws.amazon.com/msk/home?region=ap-south-1#/home). To learn more, read the [AWS Managed Streaming for Apache Kafka documentation](https://docs.aws.amazon.com/msk/?id=docs_gateway). For more details on AWS Lambda, Checkout Get started AWS Lambda, open the [AWS Lambda console](https://ap-south-1.console.aws.amazon.com/lambda/home?region=ap-south-1#/discover). To learn more, read the [AWS Lambda documentation](https://docs.aws.amazon.com/lambda/?id=docs_gateway).

Thanks for reading!

Connect with me: [Linkedin](https://www.linkedin.com/in/gargee-bhatnagar-6b7223114)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38sdi4nsp89fnui8h7dk.png)