【AWS】Amazon MSK ~ 基本編 / Connector ~

■ はじめに

https://dk521123.hatenablog.com/entry/2023/04/21/000000

の続き。

https://dk521123.hatenablog.com/entry/2023/05/14/122215

で、AWS環境に
Amazon MSK(Managed Streaming for apache Kafka)の
Cluster および Connector を構築することができた。
ここで作った MSK Connector に対して、
色々とやった技術事項をまとめておく。

目次

【1】Amazon MSK の IAM アクセス制御
 1)接続設定
 2)JARファイル「aws-msk-iam-authライブラリ」
【2】MSK Connector に接続テストする
 1)ブートストラップサーバのURLを調べる
 2)kafkaでメッセージを送信する
【3】トラブル
 1)エラー「IAMClientCallbackHandler could not be found」が表示

【1】Amazon MSK の IAM アクセス制御

https://docs.aws.amazon.com/ja_jp/msk/latest/developerguide/iam-access-control.html
https://github.com/aws/aws-msk-iam-auth

に記載されている以下が必要。
1)接続設定
2)JARファイル「aws-msk-iam-authライブラリ」

1)接続設定

client.properties

# Sets up TLS for encryption and SASL for authN.
security.protocol=SASL_SSL

# Identifies the SASL mechanism to use.
sasl.mechanism=AWS_MSK_IAM

# Binds SASL client implementation.
sasl.jaas.config=software.amazon.msk.auth.iam.IAMLoginModule required;

# Encapsulates constructing a SigV4 signature based on extracted credentials.
# The SASL client bound by "sasl.jaas.config" invokes this class.
sasl.client.callback.handler.class=software.amazon.msk.auth.iam.IAMClientCallbackHandler

2)JARファイル「aws-msk-iam-authライブラリ」

* 以下から、JARファイル(例「aws-msk-iam-auth-1.1.6-all.jar」)
 を落としてきて、KafkaのLibsフォルダ(例「kafka_2.13-3.4.0\libs」)に
 入れる。

https://github.com/aws/aws-msk-iam-auth/releases

【2】MSK Connector に接続テストする

1)ブートストラップサーバのURLを調べる

[1] Amazon MSK コンソールを開く
[2] MSKクラスターの名前を選択
[3] [Cluster summary] (クラスターの概要) ページで、
 [View client information] (クライアント情報の表示) を選択

 => 以下の公式ドキュメントを参照。

https://docs.aws.amazon.com/ja_jp/msk/latest/developerguide/msk-get-bootstrap-brokers.html

2)kafkaでメッセージを送信する

コマンド例

export KAFKA_HOME=~/kafka_2.13-3.4.0/
export PATH=${KAFKA_HOME}/bin:$PATH
export CONFIG_PATH=~/client.properties

# Set bootstrap server URL
export BOOTSTRAP_SERVER=b-1xxxxx.kafka.us-west-2.amazonaws.com:9094

exprot TARGET_TOPIC=connect-test

# Create TOPIC
kafka-topics.sh --create --topic $TARGET_TOPIC \
 --bootstrap-server $BOOTSTRAP_SERVER
 --partitions 1 --replication-factor 3  \
 --command-config $CONFIG_PATH

# To confirm
kafka-topics --list --bootstrap-server $BOOTSTRAP_SERVER
 --command-config $CONFIG_PATH

# To send message to Topic
kafka-console-producer --topic $TARGET_TOPI \
--bootstrap-server $BOOTSTRAP_SERVER \
--command-config $CONFIG_PATH  < sample_records.json

【3】トラブル

1)エラー「IAMClientCallbackHandler could not be found」が表示

トラブル概要

kafkaコマンド(例えば、「kafka-topics.sh --create ...」)を実行した際に
以下のエラー内容が表示された

エラー内容

Exception in thread "main" org.apache.kafka.common.config.ConfigException: Invalid value 
software.amazon.msk.auth.iam.IAMClientCallbackHandler for configuration sasl.client.callback.handler.class: 
Class software.amazon.msk.auth.iam.IAMClientCallbackHandler could not be found.

原因

* aws-msk-iam-auth ライブラリ(jar)が libsフォルダに入れたのだが
 そのJarファイルがゴミデータだったため

https://github.com/aws/aws-msk-iam-auth#iamclientcallbackhandler-could-not-be-found

That means that this aws-msk-iam-auth library is not on the classpath of the Kafka client.
Please add the aws-msk-iam-auth library to the classpath and try again.

解決案

* ちゃんとWebからきっちり落としてきて
 KafkaのLibsフォルダに入れる

関連記事

Amazon MSK ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2023/04/21/000000
Amazon MSK ~ AWS CLI
https://dk521123.hatenablog.com/entry/2023/06/03/003941
Terraform ~ AWS MSK ~
https://dk521123.hatenablog.com/entry/2023/05/14/122215
Apache Kafka ~ 環境構築編 ~
https://dk521123.hatenablog.com/entry/2023/04/23/235534
Apache Kafka ~ 環境構築 / Docker compose編 ~
https://dk521123.hatenablog.com/entry/2023/04/24/153846
Apache Kafka ~ 基礎知識編 ~
https://dk521123.hatenablog.com/entry/2023/04/26/103421
Apache Kafka ~ Kafkaコマンド ~
https://dk521123.hatenablog.com/entry/2023/05/16/000000
Apache Kafka ~ Kafka Connect ~
https://dk521123.hatenablog.com/entry/2023/04/29/185133
Apache Kafka ~ Kafka Connect / PostgreSQL
https://dk521123.hatenablog.com/entry/2023/05/02/233806
Apache Kafka ~ Strimzi ~
https://dk521123.hatenablog.com/entry/2023/05/08/000133
Apache Kafka ~ パフォーマンスチューニング ~
https://dk521123.hatenablog.com/entry/2023/05/28/151212
ScalaApache Kafka / Producer ~
https://dk521123.hatenablog.com/entry/2023/04/27/235703
ScalaApache Kafka / Consumer ~
https://dk521123.hatenablog.com/entry/2023/04/28/014737
Terraform ~ AWS EC2 ~
https://dk521123.hatenablog.com/entry/2023/05/21/003048
curl コマンド
https://dk521123.hatenablog.com/entry/2017/12/05/233100