aws / aws-iot-device-sdk-js

SDK for connecting to AWS IoT from a device using JavaScript/Node.js
Apache License 2.0
964 stars 384 forks source link

Lambda Function to Connect to AWS IoT and Publish MQTT Messages #138

Closed JAPDD closed 7 years ago

JAPDD commented 7 years ago

Hi i want to publish an MQTT message, I want to receive these published messages on a raspberry Pi which is connected to the thing as well, and subscribed to the same topic.

I am able to publish MQTT message from my device to AWS IOT with the same code .But while doing through lambda function i got the error.

Step 1. index.js

var awsIot = require('aws-iot-device-sdk');

var device = awsIot.device({ keyPath: 'XXXX.private.key', certPath: 'XXXX.cert.pem', caPath: 'root-CA.crt', clientId: 'XXXX', host: 'XXXXXspv.iot.ap-northeast-1.amazonaws.com' });

exports.handler = function (event, context) { ctx = context; device .on('connect', function () { console.log('connect'); device.subscribe('dd_receive_topic_1'); device.publish('dd_send_topic_2', JSON.stringify({ test_data: 'HELLO '})); console.log('Message send Success.....'); });

device .on('message', function (topic, payload) { console.log('message', topic, payload.toString()); });

exportsctx.succeed();

};

Step 2 Along with the index.js , async,gm and aws-iot-device-sdk.---->created a zip file

Step 3: Load the zip file on lambda and test run..

I got the following error { "errorMessage": "Invalid "keyPath" option supplied.", "errorType": "Error", "stackTrace": [ "new DeviceClient (/var/task/index/node_modules/aws-iot-device-sdk/device/index.js:416:7)", "Object.DeviceClient [as device] (/var/task/index/node_modules/aws-iot-device-sdk/device/index.js:201:14)", "Object. (/var/task/index/index.js:3:21)", "Module._compile (module.js:570:32)", "Object.Module._extensions..js (module.js:579:10)", "Module.load (module.js:487:32)", "tryModuleLoad (module.js:446:12)", "Function.Module._load (module.js:438:3)", "Module.require (module.js:497:17)" ] } Am i doing something wrong .Please do guide me .

fengsongAWS commented 7 years ago

It seems you have invalid key path. Are you using the relative path in your device option configuration?

Another thing to note is that mqtt is long live connection so it is not a recommended way to use it in lambda function. You can still do pub but the connection will lose once lambda function dies.

Thanks for you interest in AWS IoT Device SDK.

JAPDD commented 7 years ago

Hi all,

I want to control my camera from my application .I want to use AWS IOT for that.

I want to do that in following way:

  1. Create an Application on EC2

  2. Call the Rest API from the Application to API gateway.

  3. API gate way will invoke Lambda function and MQTT message will send to AWS IOT thing.

  4. AWS IOT thing should be connected to my Raspberry PI and Camera .

Can you suggest me which will be a good way to execute this .

On Tue, Aug 8, 2017 at 2:12 AM, fengyi notifications@github.com wrote:

It seems you have invalid key path. Are you using the relative path in your device option configuration?

Another thing to note is that mqtt is long live connection so it is not a recommended way to use it in lambda function. You can still do pub but the connection will lose once lambda function dies.

Thanks for you interest in AWS IoT Device SDK.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/aws/aws-iot-device-sdk-js/issues/138#issuecomment-320723265, or mute the thread https://github.com/notifications/unsubscribe-auth/AdUhnIqkEFGwXt7Sg79BH5Q4vn9iEXPdks5sV0VjgaJpZM4OvDtp .

fengsongAWS commented 7 years ago

Based on your description, it seems to be a typical use case of AWS IoT. You can definitely do 1-3 as described. I am assuming you are running iot device SDK on Raspberry Pi. So, you can subscribe a topic and receive messages published from your lambda function.

In this case, you do not need to use iot device sdk in lambda. Instead, you could use AWS SDK which is a HTTP based SDK as you do not care about long live connections.

Hope this helps

groteworld commented 7 years ago

EDIT: @fengsongAWS beat me to it 🍻


@JAPDD, in your lambda you may find it easier to use aws-sdk and it's IotData class. If in a lambda, it could use the aws-sdk and aws credentials built into the container. This avoids the need for x509 certs that aws-iot-device-sdk requires.

While it could be used in the lambda, this is much better suited for the raspberry pi step

JAPDD commented 7 years ago

@Blake Thanks for your reply.Can you provide me a small example.How can i use aws-sdk device using Iotdata class.

Thank you

On Wed, Aug 9, 2017 at 3:19 AM, Blake Grotewold notifications@github.com wrote:

@JAPDD https://github.com/japdd, in your lambda you may find it easier to use aws-sdk https://github.com/aws/aws-sdk-js and it's IotData http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/IotData.html class. If in a lambda, it could use the aws-sdk and aws credentials built into the container. This avoids the need for x509 certs that aws-iot-device-sdk requires.

While it could be used in the lambda, this is much better suited for the raspberry pi step

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/aws/aws-iot-device-sdk-js/issues/138#issuecomment-321039537, or mute the thread https://github.com/notifications/unsubscribe-auth/AdUhnG1QjHF6HhvEou8TulhQ5PGins8uks5sWKa9gaJpZM4OvDtp .

groteworld commented 7 years ago

Here is a simple example of this. This won't cover your full use case above. but showcases a minimal usage of IotData instead of the device sdk. Where this device sdk is arguably better suited for an actual thing that, while out in the wilderness, needs a better security.

https://gist.github.com/groteworld/af0577ea5c59e98843ef47325407da71

fengsongAWS commented 7 years ago

Hi,

For AWS SDK in node.js. You can refer to following link. https://aws.amazon.com/sdk-for-node-js/