Step 4: Create a topic in the Amazon MSK cluster - Amazon Managed Streaming for Apache Kafka

Step 4: Create a topic in the Amazon MSK cluster

In this step of Getting Started Using Amazon MSK, you install Apache Kafka client libraries and tools on the client machine, and then you create a topic.

Warning

Apache Kafka version numbers used in this tutorial are examples only. We recommend that you use the same version of the client as your MSK cluster version. An older client version might be missing certain features and critical bug fixes.

To find the version of your MSK cluster
  1. Open the Amazon MSK console at https://console.aws.amazon.com/msk/.

  2. In the navigation bar, choose the Region where you created the MSK cluster.

  3. Choose the MSK cluster.

  4. Note the version of Apache Kafka used on the cluster.

  5. Replace instances of Amazon MSK version numbers in this tutorial with the version obtained in Step 3.

To create a topic on the client machine
  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. In the navigation pane, choose Instances. Then select the check box beside the name of the client machine that you created in Step 3: Create a client machine.

  3. Choose Actions, and then choose Connect. Follow the instructions in the console to connect to your client machine.

  4. Install Java on the client machine by running the following command:

    sudo yum -y install java-11
  5. Run the following command to download Apache Kafka.

    wget https://archive.apache.org/dist/kafka/{YOUR MSK VERSION}/kafka_2.13-{YOUR MSK VERSION}.tgz

    For example, if you want to use Amazon MSK with Apache Kafka version 3.5.1, run the following command.

    wget https://archive.apache.org/dist/kafka/3.5.1/kafka_2.13-3.5.1.tgz
    Note

    If you want to use a mirror site other than the one used in this command, you can choose a different one on the Apache website.

  6. Run the following command in the directory where you downloaded the TAR file in the previous step.

    tar -xzf kafka_2.13-{YOUR MSK VERSION}.tgz
  7. Go to the kafka_2.13-{YOUR MSK VERSION}/libs directory, then run the following command to download the Amazon MSK IAM JAR file. The Amazon MSK IAM JAR makes it possible for the client machine to access the cluster.

    wget https://github.com/aws/aws-msk-iam-auth/releases/download/v2.3.0/aws-msk-iam-auth-2.3.0-all.jar

    Using this command, you can also download the latest version of aws-msk-iam-auth-*-all.jar.

  8. Go to the kafka_2.13-{YOUR MSK VERSION}/config directory. Copy the following property settings and paste them into a new file. Name the file client.properties and save it.

    security.protocol=SASL_SSL sasl.mechanism=AWS_MSK_IAM sasl.jaas.config=software.amazon.msk.auth.iam.IAMLoginModule required; sasl.client.callback.handler.class=software.amazon.msk.auth.iam.IAMClientCallbackHandler
  9. Open the Amazon MSK console at https://console.aws.amazon.com/msk/.

  10. Wait for the status of your cluster to become Active. This might take several minutes. After the status becomes Active, choose the cluster name. This takes you to a page containing the cluster summary.

  11. Choose View client information.

  12. Copy the connection string for the private endpoint.

    You will get three endpoints for each of the brokers. You only need one broker endpoint for the following step.

  13. Run the following command, replacing BootstrapServerString with one of the broker endpoints that you obtained in the previous step.

    <path-to-your-kafka-installation>/bin/kafka-topics.sh --create --bootstrap-server BootstrapServerString --command-config client.properties --replication-factor 3 --partitions 1 --topic MSKTutorialTopic
    Note

    Before running this command, you must add the AWS MSK IAM JAR file to your classpath to avoid the Class not found error. You can do this by setting the CLASSPATH environment variable, as shown in the following command.

    export CLASSPATH=<path-to-your-kafka-installation>/libs/aws-msk-iam-auth-2.3.0-all.jar

    Alternatively, you can also modify your command to explicitly add the JAR to the classpath, as shown in the following example.

    classpath=<path-to-your-kafka-installation>/libs/aws-msk-iam-auth-2.3.0-all.jar \ <path-to-your-kafka-installation>/bin/kafka-topics.sh --create \ --bootstrap-server BootstrapServerString \ --command-config client.properties \ --replication-factor 3 \ --partitions 1 \ --topic MSKTutorialTopic

    For example, if Apache Kafka is installed in /home/ec2-user/kafka_2.13-2.8.1, and your broker endpoint is myBrokerEndpoint-1.myCluster.abc123.kafka.us-east-1.amazonaws.com:9098, you'd run the following command.

    export CLASSPATH=/home/ec2-user/kafka_2.13-2.8.1/libs/aws-msk-iam-auth-2.3.0-all.jar /home/ec2-user/kafka_2.13-2.8.1/bin/kafka-topics.sh --create --bootstrap-server myBrokerEndpoint-1.myCluster.abc123.kafka.us-east-1.amazonaws.com:9098 --command-config client.properties --replication-factor 3 --partitions 1 --topic MSKTutorialTopic

    If the command succeeds, you see the following message: Created topic MSKTutorialTopic.

    If the command is unsuccessful or you run into an error, see Troubleshoot your Amazon MSK cluster for troubleshooting information.

Next Step

Step 5: Produce and consume data