0

I have an AWS MKS cluster in which I could create a topic, produce a message and consume a message using installed Kafka in my ec2 server. But when I am trying to use the Kafka producer in my nodejs app, I get the error while npm start

{"level":"ERROR","timestamp":"2023-07-29T06:18:34.532Z","logger":"kafkajs","message":"[Connection] Response SaslHandshake(key: 17, version: 1)","broker":"b-1.ashixxxxxx4.c3.kafka.ap-southeast-1.amazonaws.com:9094","clientId":"cab-allocation","error":"Request is not valid given the current SASL state","correlationId":1,"size":10}
{"level":"ERROR","timestamp":"2023-07-29T06:18:34.533Z","logger":"kafkajs","message":"[BrokerPool] Failed to connect to seed broker, trying another broker from the list: Request is not valid given the current SASL state","retryCount":0,"retryTime":273}
KafkaJSProtocolError: Request is not valid given the current SASL state

my kafkaproducer.js code is:

const { Kafka, AwsSasl } = require('kafkajs');// Define the Kafka client configuration
const BROKER_1 = process.env.KAFKA_BROKER_1 as string
const BROKER_2 = process.env.KAFKA_BROKER_2 as string
const AWS_REGION = process.env.AWS_DEFAULT_REGION as string
const ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID as string
const SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY as string

const kafka = new Kafka({
  clientId: 'cab-allocation',
  brokers: ["b-1.axxxxxxx3.kafka.ap-southeast-1.amazonaws.com:9094,b-2.asxxxxx74.c3.kafka.ap-southeast-1.amazonaws.com:9094"], // Replace with your AWS MSK broker endpoints
  ssl: true,
  sasl: {
    mechanism: 'aws',
    authenticationProvider: AwsSasl,
   aws: {
     region: "ap-southeast-1",
     //authorizationIdentity:"Geolah",       
     secretAccessKey: "q7SI3JyeOxxxxxyYpyMhY0ciAou6TDXWdyR6h",
     accessKeyId: "AKIAxxxxS25GD37NEJ",
   },
 },
});

const producer = kafka.producer();
producer.connect();

export async function sendKafkaMessage(topic: string, message: any) {
  const result = await producer.send({
    topic,
    messages: [
      { value: message }
    ]
  });
  console.log(result);
  return result;
}

producer.on('producer.connect', () => {
  console.log('Kafka producer connected');
});

(I have used correct aws credentials and MSK brokers)

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
ashique
  • 935
  • 2
  • 8
  • 26
  • Unrelated, but you should use your process.env values, not hard-code them in strings... Have you configured the VPC to allow remote connections? – OneCricketeer Jul 29 '23 at 12:24
  • yes i ve configured all vpc – ashique Aug 02 '23 at 07:46
  • Not sure, then. I'd suggest contacting MSK support since only they'll really know what network/security permissions your clients need to connect. You can also debug by following the docs https://docs.aws.amazon.com/msk/latest/developerguide/client-access.html – OneCricketeer Aug 02 '23 at 13:30

1 Answers1

1

The "request is not valid" since you need to use port 9098/9198 for IAM not 9094

https://docs.aws.amazon.com/msk/latest/developerguide/port-info.html

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245