amazon-web-servicesaws-lambdaamazon-dynamodbaws-iot-coreaws-roles

AWS IoT , Rules, Lambda Function, DynamoDB (storing streaming AWS iot core data to dynamoDB)


I have refered this meduim article :https://medium.com/dev-jam/tutorial-part-2-aws-iot-rules-lambda-function-dynamodb-40a7d4ea35b9

full picture of the implementation Above is the full picture of the implementation.

lambda function that I used for receiving data from iot core to dynamoDB is given below

console.log('Loading function');
const AWS = require('aws-sdk');
const dynamo = new AWS.DynamoDB.DocumentClient();

const collection ="IoTCatalog"

// Handler lamda function
exports.handler = function(event, context) {
console.log('Received event:', JSON.stringify(event, null, 2));
   const params = {
    TableName: collection,
    Item:{
        "serialNumber": event.serialNumber,
        "timestamp": event.dateTime,
        "activated": event.activated,
        "clientId": event.clientId,
        "device": event.device,
        "type": event.type,
        "payload": event.payload
        }
    };

    console.log("Saving Telemetry Data");
    
    dynamo.put(params, function(err, data) {
        if (err) {
            console.error("Unable to add device. Error JSON:", JSON.stringify(err, null, 2));
            context.fail();
        } else {
            console.log(data)
            console.log("Data saved:", JSON.stringify(params, null, 2));
            context.succeed();
            return {"message": "Item created in DB"}
        }
    });
}

I am receiving data in iot core for every 3 seconds. And I have deployed this lambda function, but when I run the NodeJS program which functions to transmit data to iot core after every 3 seconds, and so I should be receiving data every 3 seconds in DynamoDB table, but I am receiving only 1 time.

What should I do to receive data in DynamoDB as I am receiving data in IOT core after every 3 seconds?


Solution

  • DynamoDB items are unique based on their primary key (partition and sort key combined). If you are writing multiple items with the same key, then you are simply overwriting the item each time.

    You need to ensure you key is unique for each and every insert. For example, you can use a nanosecond timestamp as the sort key.