Get DynamoDB items from AWS Lambda function

Recently I was using DynamoDB with AWS Lambda functions and in this post I would like to share with you few recipes for querying DynamoDB tables from AWS Lambda Node.js function.

In this short post I’ll show you how to get single item and all the items from the database. Former uses query function and the latter will use scan function.

Get item by primary key (query)

let params = {
        TableName: "weatherData",
        KeyConditionExpression: "id = :id",
        ExpressionAttributeValues: {
            ":id": Number(stationId)
        }
        documentClient.query(params, function(err, data) {
            if (err) {
                // no data
            } else {
                processData(data.Items);
            }
        });

Get all items from table (scan)

var params = {
    TableName: "weatherData",
};
documentClient.scan(params, function(err, data) {
    if (err) {
        // no data
    } else {
        for (let item in data.Items) {
            processData(data.Items[item]);
        }
    }
});

 

Leave a Reply

Your e-mail address will not be published. Required fields are marked *