I Cant delete multiple items using DynamoDB BatchWrite including a primary key and sort key

Viewed 306

So I'm trying to delete two records for the same user. However, when I run the Lambda function below it really does not console log either a success or failure and the records are not deleted in DynamoDB. Any advice on how I could modify the below to get the expected result of having both records deleted would be appreciated. Thanks

  • Included several console logs for troubleshooting
  • both PK and SK are strings in DynamoDB
  • Both delete request are for the same user just two different entries in the same table
'use strict'
const AWS = require('aws-sdk');

AWS.config.update({ region: "us-east-1" });

exports.handler = async (event, context) => {
    var ddb = new AWS.DynamoDB.DocumentClient();
    
    
    var records = [{
        DeleteRequest : {
            Key : {
                'PK' : 'username',
                'SK' : 'C297329360'
            }
        },
        DeleteRequest : {
            Key : {
                'PK' : 'username',
                'SK' : 'R297329360'
            }
        }
    }];
    
    var params = {
        RequestItems : {
            'userTable' : records
        }
    };
    console.log(JSON.stringify(records));
    console.log(records);
    console.log(JSON.stringify(params));
    console.log(params);
    
    ddb.batchWrite(params, function(err, data) {
        if (err) {
            console.log('Batch delete unsuccessful ...');
            console.log(err, err.stack); // an error occurred
        } else {
            console.log('Batch delete successful ...');
            console.log(data); // successful response
        }
    
    });
}

Test Event Name
DeleteReportById

Response
null

Function Logs
START RequestId: 6c40c1f7-5cdb-4cef-95a7-306d540aa9f9 Version: $LATEST
2022-02-11T13:49:56.007Z    6c40c1f7-5cdb-4cef-95a7-306d540aa9f9    INFO    [{"DeleteRequest":{"Key":{"PK":"username","SK":"R297329360"}}}]

2022-02-11T13:49:56.046Z    6c40c1f7-5cdb-4cef-95a7-306d540aa9f9    INFO    [ { DeleteRequest: { Key: [Object] } } ]

2022-02-11T13:49:56.047Z    6c40c1f7-5cdb-4cef-95a7-306d540aa9f9    INFO    {"RequestItems":{"FitTool_Prod":[{"DeleteRequest":{"Key":{"PK":"username","SK":"R297329360"}}}]}}

2022-02-11T13:49:56.065Z    6c40c1f7-5cdb-4cef-95a7-306d540aa9f9    INFO    { RequestItems: { userTable: [ [Object] ] } }

END RequestId: 6c40c1f7-5cdb-4cef-95a7-306d540aa9f9
REPORT RequestId: 6c40c1f7-5cdb-4cef-95a7-306d540aa9f9  Duration: 589.81 ms Billed Duration: 590 ms Memory Size: 128 MB Max Memory Used: 77 MB  Init Duration: 474.35 ms

Request ID
6c40c1f7-5cdb-4cef-95a7-306d540aa9f9
1 Answers

You are not waiting for the batchWrite call to complete, the lambda terminates before actually sending the request to DynamoDB. Your handler is async, so you can do :

await ddb.batchWrite(...).promise()
Related