Accessing AWS Lambda from IoT devices

All API SDK for accessing AWS IoT, including AWS Greengrass, can be obtained on the software menu from AWS IoT Management Console. You can install it based on your device and runtime platforms.

In this section, I will show you how to invoke Lambda from AWS Greengrass Core. For testing, I used the Node.js application. You should install the aws-sdk library for Node.js. You also need to access the key ID and secret access key from the AWS IAM.

Please write the following complete program:

var AWS = require('aws-sdk');
AWS.config.update({region:'<region>'});

var accessKeyId = '<accessKeyId>'
var secretAccessKey = '<secretAccessKey>';
AWS.config.update({accessKeyId: accessKeyId, secretAccessKey: secretAccessKey});

var lambda = new AWS.Lambda();
var params = {
FunctionName: '<lambda-function-arn>',
Payload: '{"msg": "this is testing"}'
};
lambda.invoke(params, function(err, data) {
if (err) console.log(err, err.stack);
else console.log(data);
});

Now you can run the preceding program using the following command:

$ node lambda_invoker.js

On successful execution, you should see a message on the Terminal that shows the status and response message from Lambda. A sample of the program output can be seen in the following screenshot:

We have accessed AWS Lambda from an IoT device or computer. You can do more experiments to get experience with AWS Lambda.

Next, we will integrate AWS Lambda with other AWS services. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.174.0