代码之家  ›  专栏  ›  技术社区  ›  Jerome Ansia

AWS lambda-未调用PutItem Dynamo DB回调

  •  0
  • Jerome Ansia  · 技术社区  · 6 年前
    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');
                    }
                }); 
    
            });
    };
    

    我试着听卡夫卡和消费信息(作品)。

    但是,从未调用putItem()的回调,因此语句: console.log(“你好,世界!!!!“!”

    从不展示。 有什么问题?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Mark B    6 年前

    您对dynamodb的请求正超时,因为您正在vpc中运行lambda函数,但您没有提供nat网关或 VPC Endpoint 允许lambda函数访问vpc外部存在的dynamodb。

    我建议将vpc端点配置为dynamodb。