Wednesday, October 28, 2026
HomeIoTMethods to combine AWS IoT Core with Amazon MSK

Methods to combine AWS IoT Core with Amazon MSK

[ad_1]

Put up by Milo Oostergo, Principal Options Architect and DoronĀ Bleiberg, Senior Resolution Architect, AWS Startups

Introduction

Monitoring IoT units in actual time can present useful insights that may enable you to preserve the reliability, availability, and efficiency of your IoT units. AWS IoT Core supplies integrations with Amazon Kinesis Information Streams andĀ Amazon Managed Streaming for Apache Kafka (ā€œAmazon MSKā€) to arrange real-time streaming information pipelines. Amazon MSK is fashionable selection for patrons who’re acquainted with Kafka, want infinite message retention, and are searching for the bottom latency. On this weblog publish, we describe learn how to arrange AWS IoT Core to stream occasions toĀ Amazon MSK and customary asks from our clients.

Overview

The diagram beneath illustrates the parts and performance you may construct following this weblog publish or utilizing this pattern AWS CloudFormation template. As a part of this resolution, MQTT messages streamed to AWS IoT Core are routed to Amazon Managed Streaming for Apache Kafka (Amazon MSK) utilizing AWS IoT Guidelines actions. Entry to the Amazon MSK cluster is managed utilizing username and password which might be securely saved in AWS Secrets and techniques Supervisor and encrypted utilizing AWS Key Administration Service.

Walkthrough

Solution overview

Conditions

Step 1: Establishing an Amazon MSK cluster

To ship messages from IoT units to Amazon MSK utilizing AWS IoT Core rule actions, you might want to allow authentication in your Amazon MSK cluster. IoT rule actions can authenticate together with your Amazon MSK cluster with username and password authentication utilizing the SASL framework or through the use of TLS consumer authentication via AWS Certificates Supervisor. On this weblog publish, we arrange the cluster utilizing SASL/SCRAM authentication technique. As soon as a cluster is created, you may’t modify the the authentication settings.

To create the Amazon MSK cluster with authentication enabled

  1. From the Amazon MSK console, select Create Cluster.
  2. Choose, enter a cluster title, and hold the really helpful Apache Kafka model.Amazon MSK create cluster
  3. In Networking, choose your VPC and select ā€œ2ā€ for Variety of Availability Zones. From the drop-downs, choose the 2 Availability Zones within the VPC, and select the personal subnets for every.
  4. In Entry management technique, select SCRAM/SASL authentication.Selecting the SASL/SCRAM authentication method for your Amazon MSK cluster
  5. Maintain the present defaults and select Create cluster. It takes as much as quarter-hour to create the cluster and the standing is displayed within the Cluster Abstract panel.

Step 2: Create credentials in AWS Secrets and techniques Supervisor Ā 

After the cluster is efficiently created, we create a set of credentials that can be utilized by the IoT rule to attach with the Amazon MSK cluster. The credentials have to be saved in AWS Secrets and techniques Supervisor and related to the cluster. Earlier than we create the credentials in AWS Secrets and techniques Supervisor, we first create a customer-managed key in AWS Key Administration Service (KMS). Secrets and techniques encrypted with a AWS managed CMK can’t be used with an Amazon MSK cluster.

  1. Open the AWS KMS console and select Create key.
  2. Select symmetric key and observe the wizard to create the important thing. You don’t must outline the important thing administrative permissions or key utilization permissions at this level. We set this up later.Now that the KMS key’s created, we will retailer the credentials in AWS Secrets and techniques Supervisor.
  1. Open the AWS Secrets and techniques Supervisor console and select Retailer a brand new credential.
  2. Select Different kind of secrets and techniques (e.g. API key) for the key kind.Store your secret in AWS Secrets Manager
  3. Enter the person and password information, which have to be within the following format:
    {
       "username": "msk",
       "password": "msk-secret"
    }

  4. Choose the shopper managed key you created in earlier step.
  5. To affiliate secrets and techniques with the Amazon MSK cluster, the key title will need to have the prefix AmazonMSK_. On this instance, we use the title AmazonMSK_secret.
  6. File the ARN (Amazon Useful resource Title) worth on your secret.

Step 3: Affiliate secret with Amazon MSK cluster

As soon as the key is created in AWS Secrets and techniques Supervisor, we will affiliate the key with our Amazon MSK cluster.

  1. Return to the Amazon MSK console and choose your cluster.
  2. Select Affiliate secrets and techniques and copy-paste the ARN of the key you created in earlier step.

Associate secret with Amazon MSK cluster

Step 4: Arrange AWS Id and Entry Administration (IAM) function and coverage for AWS IoT rule

To grant AWS IoT entry to stream information to our Amazon MSK cluster,Ā you have to create an IAM function with a coverage that enables entry to the required AWS sources.

To create an IAM function utilizing AWS CLI

  1. Save the next belief coverage doc, which grants AWS IoT permission to imagine the function, to a file named iot-role-trust.json:
    {
    Ā Ā "Model":"2012-10-17",
    Ā Ā "Assertion":[{
    Ā Ā  Ā  Ā "Effect": "Allow",
    Ā Ā  Ā  Ā "Principal": {
    Ā Ā  Ā  Ā  Ā "Service": "iot.amazonaws.com"
    Ā Ā  Ā  Ā },
    Ā Ā  Ā  Ā "Action": "sts:AssumeRole"
    Ā Ā }]
    }

  2. Use the create-role command to create an IAM function specifying the iot-role-trust.json file. Make certain to switch the AWS account id and area placeholders.aws iam create-role --role-name IoT-Rule-MSK-Function --assume-role-policy-document file://iot-role-trust.json
  3. Save the next JSON right into a file named iot-msk-policy.json.
    {
       "Model":"2012-10-17",
       "Assertion":[
          {
             "Effect":"Allow",
             "Action":[
                "ec2:CreateNetworkInterface",
                "ec2:DescribeNetworkInterfaces",
                "ec2:CreateNetworkInterfacePermission",
                "ec2:DeleteNetworkInterface",
                "ec2:DescribeSubnets",
                "ec2:DescribeVpcs",
                "ec2:DescribeVpcAttribute"
             ],
             "Useful resource":"*"
          },
          {
             "Impact":"Enable",
             "Motion":[
                "secretsmanager:GetSecretValue",
                "secretsmanager:DescribeSecret"
             ],
             "Useful resource":"Ā ""arn:aws:secretsmanager:area:123456789012:AmazonMSK_*"
          ]
       }
    ]
    }

    This JSON is an instance coverage doc that gives entry to create and handle elastic community interfaces in your Amazon Digital Non-public Cloud and retrieve the credentials to succeed in your Kafka brokers.

  4. Use the create-policy command to outline the actions and sources that AWS IoT Core can entry upon assuming the function, by passing within the iot-msk-policy.json file:aws iam create-policy --policy-name IoT-Rule-MSK-policyĀ --policy-document file://iot-msk-policy.json
  5. Use the attach-role-policy command to connect your coverage and grant AWS IoT entry. Substitute the placeholder ARN by the coverage ARN returned within the earlier step.aws iam attach-role-policy --role-name IoT-Rule-MSK-Function --policy-arn "arn:aws:iam::123456789012:coverage/IoT-Rule-MSK-policy"

    To grant the IAM function entry to the KMS key
    As a way to decrypt the key saved in AWS Secrets and techniques Supervisor, we should add the IAM function to the listing of key customers for the Buyer Managed KMS key we earlier created.
    1. Go to the AWS KMS console and choose the KMS key you created within the earlier step.
    2. In Key customers add the IAM function IoT-Rule-MSK-Function.

Step 5 – Create VPC vacation spot for AWS IoT core

Create a vacation spot to your VPC the place Apache Kafka clusters reside. This vacation spot is used to route messages from units to your Amazon MSK cluster.

  1. Go to AWS IoT console, select Act, after which select Locations.
  2. Select Create a VPC vacation spot.
  3. Choose the VPC and identical subnets which might be used on your Amazon MSK cluster.
  4. Choose safety group that’s used on your Amazon MSK cluster.
  5. Choose the IoT-Rule-MSK-Function you created within the earlier step.

Step 6 – Create AWS IoT rule

  1. Go to AWS IoT console, select Act, select Guidelines, and create a brand new rule.
  2. In Actions select Add motion and choose Kafka.
  3. Choose the VPC vacation spot you created within the earlier step.
  4. Specify the Kafka matter.
  5. Specify the TLS bootstrap servers of your Amazon MSK cluster. You’ll be able to view the bootstrap server URLs in consumer info of your MSK cluster particulars.View client information to connect to your Amazon MSK cluster
  6. As we arrange our Amazon MSK cluster with the SCRAM SASL authentication technique, choose SSL_SASL as safety.protocol and choose SCRAM-SHA512 as sasl.mechanism.
  7. Specify the next variable in sasl.scram.username and substitute the title AmazonMSK_secret with the title of the key you saved in step 2.${get_secret(ā€˜AmazonMSK_secret', 'SecretString', ā€˜username’, 'arn:aws:iam::123456789012:function/iot-msk-role)}
  8. Specify the next variable in sasl.scram.password and save the IoT rule motion.${get_secret(ā€˜AmazonMSK_secret', 'SecretString', ā€˜password, 'arn:aws:iam::123456789012:function/iot-msk-role)}Testing the AWS IoT rule
    At this level, you may have created the Amazon MSK cluster and arrange an AWS IoT Core rule with the required IAM permissions. To confirm IoT occasions are streaming to your Amazon MSK cluster, you may join a Kafka client to your bootstrap servers and ship an occasion to your IoT matter utilizing the MQTT check consumer out there within the AWS IoT console.Sending a MQTT test messageThe Kafka client related to your cluster can now obtain messages on the Amazon MSK matter. To be taught how one can connect with your Amazon MSK cluster, see the part Connecting to your cluster with a username and password within the Amazon MSK developer information.Receiving IoT messages in your Kafka consumerEstablishing the permissions incorrectly is a typical situation leading to clients not receiving occasions on their Amazon MSK cluster. Ā When AWS IoT is unable to ship occasions, the principles engine triggers an Error motion. For instance, you may arrange an error motion to ship the occasions to Amazon CloudWatch Logs and specify the CloudWatch log group to which the IoT rule motion sends the info. When an error happens whereas processing your rule, you may view the stream of log occasions within the log group in CloudWatch Logs.

Cleansing up

Should you adopted together with this resolution, full the next steps to keep away from incurring undesirable fees to your AWS account.

AWS IoT Core

  • Within the Act part, delete the rule and VPC vacation spot.

Amazon MSK

AWS KMS

  • Delete the Buyer Managed Key used to encrypt the secrets and techniques saved in AWS Secrets and techniques Supervisor.

AWS Secrets and techniques Supervisor

  • Delete the key created to authenticate together with your Amazon MSK cluster.

AWS IAM

  • Delete the insurance policies and roles created alongside the way in which.

Amazon CloudWatch

  • Delete the related Log teams.

Conclusion

On this publish, we gave you an outline of learn how to construct a real-time streaming information pipeline on your IoT units by integrating AWS IoT Core with Amazon MSK. We confirmed the way you setup Amazon MSK and IoT rule actions to ship messages to Apache Kafka.

Concerning the authors

Milo Oostergo
Milo is a Principal Resolution Architect for AWS Startups crew in Amsterdam. Earlier than becoming a member of the Startup crew, he labored as PrincipalĀ Product Supervisor on numerous AWS providers.

DoronĀ Bleiberg
Doron is a seniorĀ Resolution Architect for AWS Startups crew in Israel, specializing in AWS IoT providers

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments