Serverless Lambda functions are the easiest and most customizable form of analytics. Lambda analytics allow you to process data immediately, with powerful support for Python, Node.js, GO, C#, and Java. The easiest way to configure a Lambda function is through the AWS console as follows:
- From the AWS console, we have to search for Lambda and click on the Create function button
- Select the Author from scratch section and, for the name field, enter my_iiot_lambda_threshold. From the role template, we can define our role name as my_iiot_lambda_threshold_role:
Creating a Lambda function
After clicking on the Create function button, AWS will show a page similar to the following screenshot:
Triggering the Lambda function
- We can now create a custom IoT rule, named my_iiot_lambda_threshold_rule, that will be projected into the IoT Core act. As mentioned previously, the source is as follows:
SELECT * FROM 'signals/#'
Triggering the Lambda function
- Click on the Save button.
- Finally, we can write our function. Click on the my_iiot_lambda_threshold box and configure the entry for index.js as follows:
Coding the Lambda function
- Enter the following code into the index.js field:
exports.handler = (event, context, callback) => {
console.log(event);
if (event.temperature>20) console.log("Alert")
callback(null, 'Hello from Lambda');
};
- Click on the Save button.
- Now, it's time to test our function. From the command console, enter the following command:
$ node iiot-book-device.js
Let's take a look at the results in CloudWatch:
CloudWatch's log of the Lambda function