0

I am trying to set up a stream from one dynamo stream so it can put data into another dynamo table, but the data does not seem to be put into the dynamo target table

The code executes fine, and cloudwatch does not show any errors. It has enough permissions to access the dynamo table it is trying to write to.

var AWS = require("aws-sdk");
const dynamoDb = new AWS.DynamoDB.DocumentClient();

exports.handler = async (event, context) => {

event.Records.forEach((record) => {
    if (record.eventName != 'INSERT') {
        console.log('Ignored ' + record.eventName + ' stream event.');
        return;
    }

    var input = record.dynamodb.NewImage;
    var id = input.pageId.S;
    console.log(input);
    console.log(id);
        const params = {
            TableName: "target-table",
            Key: {
                "targetId": id,
            },
            Item: {"targetId": id},
        };
    dynamoDb.put(params).promise();


    console.log("put put the put put")
});
}

I am expecting data I enter in one table to be replicated into the target, but nothing seems to happen.

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470

2 Answers2

0
const dynamoDb = new AWS.DynamoDB.DocumentClient();
and
dynamoDb.put(params...

needs to be

const dynamodb = new AWS.DynamoDB();
and
dynamodb.putItem(params

Full code used in my lambda:

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

exports.handler = async (event, context) => {
    const dynamodb = new AWS.DynamoDB();
    event.Records.forEach((record) => {
        if (record.eventName != 'INSERT') {
            console.log('Ignored ' + record.eventName + ' stream event.');
            return;
        }

        var input = record.dynamodb.NewImage;
        var id = input.pageId.S;
        console.log(input);
        console.log(id);
            const params = {
                TableName: "target-table",
                Item: {"targetId": {S: "emailaddress"}},
            };
         dynamodb.putItem(params, function(err, data) {
   if (err) console.log(err, err.stack); // an error occurred
   else     console.log(data);           // successful response

 });


        console.log("put put the putput")
    });
    }
0

you need to use await keyword since your handler function is async function

    //(...)
    await dynamoDb.put(params).promise();
    // (...) return at outside of forEach loop
    return {statusCode: 200, body : 'insertion is done'}
elbik
  • 1,749
  • 2
  • 16
  • 21
milkcoke
  • 1
  • 3