mirror of
https://github.com/serverless/serverless.git
synced 2025-12-08 19:46:03 +00:00
43 lines
1.0 KiB
JavaScript
43 lines
1.0 KiB
JavaScript
'use strict'
|
|
|
|
// NOTE: `kafkajs` is bundled into the deployment package
|
|
// eslint-disable-next-line import/no-unresolved
|
|
const { Kafka } = require('kafkajs')
|
|
|
|
function consumer(event, context, callback) {
|
|
const functionName = 'consumer'
|
|
const { records } = event
|
|
const messages = Object.values(records)[0].map((record) =>
|
|
Buffer.from(record.value, 'base64').toString(),
|
|
)
|
|
// eslint-disable-next-line no-console
|
|
console.log(functionName, JSON.stringify(messages))
|
|
return callback(null, event)
|
|
}
|
|
|
|
async function producer() {
|
|
const kafkaBrokers = process.env.BROKER_URLS.split(',')
|
|
const kafkaTopic = process.env.TOPIC_NAME
|
|
|
|
const kafka = new Kafka({
|
|
clientId: 'myapp',
|
|
brokers: kafkaBrokers,
|
|
ssl: true,
|
|
})
|
|
|
|
const kafkaProducer = kafka.producer()
|
|
await kafkaProducer.connect()
|
|
await kafkaProducer.send({
|
|
topic: kafkaTopic,
|
|
messages: [{ value: 'Hello from MSK Integration test!' }],
|
|
})
|
|
|
|
await kafkaProducer.disconnect()
|
|
|
|
return {
|
|
statusCode: 200,
|
|
}
|
|
}
|
|
|
|
module.exports = { producer, consumer }
|