简体   繁体   中英

AWS Lambda - PutItem Dynamo DB Callback not called

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');
                }
            }); 

        });
};

I am trying to listen to Kafka and consume messages (works).

But then the callback of putItem() is never called so the statement : console.log("HELLO WORLD!!!!");

never shows. What could be the problem?

Your requests to DynamoDB are timing out because you are running your Lambda function in a VPC, but you are not providing either a NAT Gateway or a VPC Endpoint to allow the Lambda function to access DynamoDB which exists outside your VPC.

I recommend configuring a VPC Endpoint to DynamoDB.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM