skip to Main Content

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?

2

Answers


  1. 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.

    Login or Signup to reply.
  2. I would highly suggest to remove the lambda and connect directly IoT Rule Action to DynamoDB (this would reduce the cost of your solution). The lambda here doesn’t provide any valuable logic (unless you have hidden something). Also, as mentioned by Lee, make sure every message sent to DDB is unique (partition and sort key). Partition key could be serial number and sort key the timestamp.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search