Copying an Item from one DynamoDB Table to another with a Lambda Trigger

0

Hi all,

I'm sure this will be really basic for a lot of you but I'm new to both Javascript and AWS in general and I appear to have hit a ridge on the learning curve.

This appears to be successfully making a copy of a modified item ( delete = 1 ) to another table but I'm getting an error that I'd prefer not to have but I have no idea what's causing it.

Here's the code:

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

exports.handler = (event, context, callback) => 
{
    event.Records.forEach((record) =>
    {
        if (record.eventName == 'MODIFY')
        {
            var input = record.dynamodb.NewImage;
            var _delete = input.Delete.N;

            if ( _delete == 1 )
            {
                const putParams = {
                    TableName: 'TableToCopyTo',
                    Item: AWS.DynamoDB.Converter.unmarshall(input)
                };
                  
                DC.put(putParams, (result, error) => {
                    if (error) {
                        console.error(error);
                        callback(error);                    
                    }
                    else
                    {
                        console.log("result = " + result);
                        callback(null, `Successfully processed!!`);
                    }
                });                      
            }
        }
    });
};

And here's the CloudWatch output:

2020-06-18T14:48:36.445Z	2bbfec75-dc2e-406e-a1e5-36e3d18cddd3	ERROR	Invoke Error 	
{
    "errorType": "Error",
    "errorMessage": "[object Object]",
    "stack": [
        "Error: [object Object]",
        "    at _homogeneousError (/var/runtime/CallbackContext.js:13:12)",
        "    at postError (/var/runtime/CallbackContext.js:30:54)",
        "    at callback (/var/runtime/CallbackContext.js:42:7)",
        "    at /var/runtime/CallbackContext.js:105:16",
        "    at Response.<anonymous> (/var/task/index.js:28:25)",
        "    at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:364:18)",
        "    at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:106:20)",
        "    at Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:78:10)",
        "    at Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:683:14)",
        "    at Request.transition (/var/runtime/node_modules/aws-sdk/lib/request.js:22:10)"
    ]
}

I wonder if someone might be able to advise?

Many thanks in advance.

Rob.

Rob66
asked 4 years ago1056 views
2 Answers
0

I don't know the precise nature of the issue, but refactoring it to use asyc seemed to solve the issue.

var AWS = require("aws-sdk");
var DC = new AWS.DynamoDB.DocumentClient();
console.log('Loading function');

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

    for (const record of event.Records) {
        console.log(record.eventID);

        if (record.eventName == 'MODIFY')
        {
            var input = record.dynamodb.NewImage;
            var _delete = input.Delete.N;

            if ( _delete == 1 )
            {
                const putParams = {
                    TableName: 'TableToCopyTo',
                    Item: AWS.DynamoDB.Converter.unmarshall(input)
                };
                  
                try {
                    const data = await DC.put(putParams).promise();
                    console.log(data);
                }
                catch (err)
                {
                    console.log(err);
                }
            }
        }
    }
};
Rob66
answered 4 years ago
0

I managed to fix this myself.

Rob66
answered 4 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions