Craig A Gomez
Craig A Gomez

Reputation: 139

AWS Lambda update DynamoDB item

I have a DynamoDB table called Wishlist, and an existing DynamoDB Item which I'm calling "monitor".

I am trying to write a Lambda function that updates the "monitor" item as follows:

Here is my code:

console.log('Loading function');
var doc = require('dynamodb-doc');
var db = new doc.DynamoDB();

exports.handler = function(event, context) 
{
     var username  = event.username;
     var email = event.username+"@gmail.com";
     console.log(username + "," + email);

     var tableName = "WishList";
     var item = {
           "username":username,
           "email":  email,     
     };

     var params = {
         TableName:tableName, 
         Item: item
     };

     console.log(params);
     db.putItem(params, function(err,data){
         if (err) console.log(err);
         else console.log(data);
     });
};

How do I read the existing "monitor" item so that I can update it with putItem?

Upvotes: 0

Views: 9522

Answers (1)

jarmod
jarmod

Reputation: 78573

If I understand your question, you need to:

  • get the existing item by its key using getItem
  • modify the returned item
  • put the modified item using putItem

Alternatively, you can simply use updateItem which will edit an existing item's attributes, or add a new item to the table if it does not already exist.

You can see sample code here.

Upvotes: 2

Related Questions