How to get rows count from Amazon DynamoDB using Lambda AWS

3.1k views Asked by At

My Lambda script is:

var AWS = require("aws-sdk");
var DOC = require("dynamodb-doc");
AWS.config.update({region: "us-west-1"});
var docClient = new DOC.DynamoDB();
var dynamodb = new AWS.DynamoDB();

exports.handler = function(event, context) {
    var params = {};
    params.TableName = "Emails";
    params.ConditionalOperator = "AND";
    params.ScanFilter = {
        Machinekey:{
            ComparisonOperator:"EQ",
            AttributeValueList: [{S: "okok"}]
        }
    };
    params.Select= "COUNT";

    //params.IndexName = "Machinekey-index";
    //params.KeyConditions = docClient.Condition("EmailId", "NOT_NULL");
    //params.QueryFilter = docClient.Condition("Machinekey", "EQ","ARUZE-010415_15112442NLHIYTLY-A23V3.9");
    dynamodb.scan(params, function(err, data) { 
    if (err) {
        console.log(err, err.stack);
    } else {
        //console.log(data);
        context.succeed(data.Count);
    }
});
};

Result of Execution logs:

[ValidationException: ConditionalOperator can only be used when Filter or Expected has two or more elements] message: 'ConditionalOperator can only be used when Filter or Expected has two or more elements', code: 'ValidationException', time: Wed Jun 10 2015 03:12:34 GMT+0000 (UTC), statusCode: 400, retryable: false,

Please, help me!

1

There are 1 answers

0
James On

You specify ConditionalOperator = "AND";, but you do not need this with your current query that contains only one condition (Machinekey == "okok"). You would only need the ConditionalOperator if you had two or more conditions.

Try commenting out the ConditionalOperator and trying it again:

var params = {};
params.TableName = "Emails";
//params.ConditionalOperator = "AND";
params.ScanFilter = {
    Machinekey:{
        ComparisonOperator:"EQ",
        AttributeValueList: [{S: "okok"}]
    }
};
params.Select= "COUNT";
...