Skip to content Skip to sidebar Skip to footer

Dynamodb Update Does Not Console.log Any Output

I have the following code. This code is supposed to receive an SQS message, read the body, then update a dynamo record with the information contained within that body. The update i

Solution 1:

There're a couple of ways to do this, but I'm not sure about your use cases: Are operations are critical? Do the failed items need to be handled? Are performance need to be boosted as the large dataset? etc...

// I'm not recommend to this implementationconst { DynamoDB } = require('aws-sdk');
const documentClient = newDynamoDB.DocumentClient();


exports.handler = async (event) => {
    for (const { messageId, body } of event.Records) {
        console.log('SQS message %s: %j', messageId, body);
        // Parse json is dangerous without knowing the structure, remember to handle// when error occuredconstJSONBody = JSON.parse(body)
        const id = JSONBody.id;
        const address = JSONBody.Address;

        const params = {
            TableName: 'myTable',
            Key: {
                ID: ':id',
            },
            UpdateExpression: 'set address = :address',
            ExpressionAttributeValues: {
                ':id': id,
                ':address': address,
                ':sortKey': "null"
            },
            ReturnValues: "UPDATED_NEW"
        };

        // Wait for each update operation to finished// IO time will be extendedawait documentClient.update(params)
            .promise()
            .then(res => {
                console.log(res)
            })
            .catch(err => {
                console.error(err);
            })
    }

    // In case there's a failed update operation, this message still be returned by lambda handlerreturn`Successfully processed ${event.Records.length} messages.`;
};
// My recommended wayconstAWS = require('aws-sdk');
const documentClient = newAWS.DynamoDB.DocumentClient();


exports.handler = async (event) => {
    // All the update operation is fired nearly concurrently// IO will be reducedreturnPromise.all(event.Records.map(({ messageId, body }) => {
        console.log('SQS message %s: %j', messageId, body);
        // Parse json is dangerous without knowing the structure, remember to handle// when error occuredconstJSONBody = JSON.parse(body)
        const id = JSONBody.id;
        const address = JSONBody.Address;

        const params = {
            TableName: 'myTable',
            Key: {
                ID: ':id',
            },
            UpdateExpression: 'set address = :address',
            ExpressionAttributeValues: {
                ':id': id,
                ':address': address,
                ':sortKey': "null"
            },
            ReturnValues: "UPDATED_NEW"
        };

        return documentClient.update(params)
            .promise()
            .then(res => {
                console.log(res)
            })
    }))
        // When lambda handler finised all the update, lambda handler return a string
        .then(() => {
            return`Successfully processed ${event.Records.length} messages.`
        })
        // In case any of the update operation failed, the next update operations is cancelled// Lambda handler return undefined
        .catch(error => {
            console.error(error);
            // return some error for lambda response.
        })
};

P/s: My two cents, before you do any kind of Lamba development with node.js runtime, you should understand the differences between callbacks, promises, await/async in javascript.

Solution 2:

Post a Comment for "Dynamodb Update Does Not Console.log Any Output"