Souvik paul
Souvik paul

Reputation: 543

How to put items into DynamoDB table using lambda(node.js)?

I have deployed API Gateway, Lambda and DynamoDB using SAM. The code for the lambda function is given below-

var AWS = require('aws-sdk');


exports.handler = async (event) => {
    try {
        
        console.log(event);         //To check if there is any problem with the API call or not, (CloudWatch result = No issues)
        console.log(event.body);    // To confirm if the 'event' is a JSON object or not. (CloudWatch result = No issues)
        var obj = JSON.parse(event.body);
        
        console.log(obj.id);
        console.log(obj.name);
        
        var ID = obj.id;
        var NAME = obj.name;
        
        var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
        
        var params = {
            TableName: 'tabmine',
            Item: {
                 id : {S: ID},
                 name : {S: NAME}
            }
        };
        
        ddb.putItem(params, function(err,data){
            if(err){
                console.log("Error: ", err);
            }
            else
            {
                console.log("Item entered successfully:", data);
            }
        });
        
        var response = {
                'statusCode': 200,
                'body': JSON.stringify({
                    message: 'Data entered successfully'
            })
        };
    } catch (err) {
        console.log(err);
        return err;
    }

    return response;
};

Using Curl, I ran the following command to enter the datas mentioned in the request body.

 curl -X POST -d '{"id":"1","name":"sam"}' https://0000000000.execute-api.ap-south-1.amazonaws.com/Prod/writedb

It ran successfully. But when I checked the DynamoDB table, no data was present.

Then I checked the CloudWatch logs, I found no errors. But as per my lambda code, if a new item is successfully entered in the DynamoDB table, then console.log("Item entered successfully:", data); should get executed and I should get the output in the CloudWatch. But I have not got any output.

From the above scenario, I came to a conclusion that in the lambda function code, following statements are not getting executed.

 ddb.putItem(params, function(err,data){
            if(err){
                console.log("Error: ", err);
            }
            else
            {
                console.log("Item entered successfully:", data);
            }
        });

I am not able to figure out, why this portion of code is not getting executed. Please help.

Upvotes: 2

Views: 5340

Answers (1)

Seth Geoghegan
Seth Geoghegan

Reputation: 5747

Nothing is printing because the asynchronous lambda code completes before the call to putItem does it's job.

You should convert the putItem call to promise and await for it's response. It would look something like this

try{
    const data = await ddb.putItem(params).promise();
    console.log("Item entered successfully:", data);
} catch(err){
    console.log("Error: ", err);
}

Upvotes: 8

Related Questions