exports.handler = function(event, context) {
var AWS = require('aws-sdk');
var dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
var kafka = require('kafka-node');
var Consumer = kafka.Consumer,
// The client specifies the ip of the Kafka producer and uses
// the zookeeper port 2181
client = new kafka.KafkaClient({kafkaHost: '172.16.35.115:9092,172.16.35.217:9092,172.16.37.14:9092'});
// The consumer object specifies the client and topic(s) it subscribes to
consumer = new Consumer( client, [ { topic: 'BillKazTopic', partition: 0, fromOffset: 'latest'} ], { autoCommit: true });
consumer.on('message', function (message) {
console.log("hellow");
console.log(message);
// Add to Dynamo
var tableName = "dev-AM-Appointment";
console.log(JSON.stringify(event, null, ' '));
dynamodb.putItem({
"TableName": tableName,
"Item" : {
"appointment_id": {S: 'message=' + message.value}
}
}, function(err, data) {
console.log("HELLO WORLD!!!!");
if (err) {
console.log('Error putting item into dynamodb failed: '+err);
context.succeed('error');
}
else {
console.log('great success: '+JSON.stringify(data, null, ' '));
context.succeed('Done');
}
});
});
};
我正在尝试听Kafka并使用消息(作品)。
但是从不调用putItem()的回调,因此该语句: console.log(“ HELLO WORLD !!!!”);
从不显示。 可能是什么问题?
答案 0 :(得分:3)
您对DynamoDB的请求正在超时,因为您正在VPC中运行Lambda函数,但没有提供NAT网关或VPC Endpoint来允许Lambda函数访问VPC外部的DynamoDB。
我建议将VPC端点配置为DynamoDB。