Search code examples
amazon-web-serviceslambdaamazon-dynamodb

How to sent data triggered lambda function?


I want to send changed data triggered lambda function from DynamoDb. Then ı want to save this changed data different DynamoDb tables.

Programming Language: NodeJs

DynomaDb
Stream enabled: Yes
View type: New and old images

Lambda Function

'use strict';

console.log('Loading function');

exports.handler = (event, context, callback) => {
    //console.log('Received event:', JSON.stringify(event, null, 2));
    event.Records.forEach((record) => {
        console.log(record.eventID);
        console.log(record.eventName);
        console.log('DynamoDB Record: %j', record.dynamodb);
    });
    callback(null, `Successfully processed ${event.Records.length} records.`);
};

it is not working.


Solution

  • DynamoDB streams and Lambda can be used for the above use case.

    1) Enable the DynamoDB streams on Dynamodb table to stream the data

    2) Create a Lambda function to consume the stream and write to another DynamoDB table. The Lambda function can be created in many programming languages (API). You can use AWS SDK to create the lambda function.

    Refer the below links for more details.

    Full documentation

    Enable Streams and Lambda - Cross region replication use case

    Stream View Type:-

    StreamViewType—specifies the information that will be written to the stream whenever data in the table is modified:

    KEYS_ONLY—only the key attributes of the modified item.

    NEW_IMAGE—the entire item, as it appears after it was modified.

    OLD_IMAGE—the entire item, as it appeared before it was modified.

    NEW_AND_OLD_IMAGES—both the new and the old images of the item.

    Event Name:-

    record.eventName should have MODIFY when the data is updated in the DynamoDB table.

    record.dynamodb should have the values based on the Stream view type. If you have selected NEW_AND_OLD_IMAGES, then it should have both old and new values.

    eventName — (String) The type of data modification that was performed on the DynamoDB table:

    INSERT - a new item was added to the table.

    MODIFY - one or more of an existing item's attributes were modified.

    REMOVE - the item was deleted from the table