dwyl / learn-aws-lambda

✨ Learn how to use AWS Lambda to easily create infinitely scalable web services
1.05k stars 202 forks source link

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

Open nbibin001 opened 7 years ago

nbibin001 commented 7 years ago

Hi I was trying to develop a lambda function which will connect to the AWS thing and 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 managed to create the thing, certificate and connect to it. But the lambda function seems to not send the MQTT message. I am attaching my sample code here in the index.js. I kept all my certificates and keys in a folder and zipped and uploaded. Since I am testing this lambda function now, I am not using the event to trigger the connection and message publishing, I just did it like whenever the function is called , it will connect and publish the message. The function seems to be running without error, but I dont see any message coming to my Pi when this runs, (I have a python code running at the Pi side which is subscribed to the same topics, and it is connected to AWS as well). Below is the lambda code I am using, could anyone please help me if I am missing something here ?

var awsIot = require('aws-iot-device-sdk'); var deviceName = "Bibin_Pi_3"; // Updated by Bibin from AWS IOT Thing name

var thingShadows = awsIot.thingShadow({ keyPath: './private.pem.key', certPath: './certificate.pem.crt', caPath: './rootCA.pem', clientId: deviceName, region: "eu-east-1", });

var ctx = null;

exports.handler = function (event, context) { ctx = context;

thingShadows.on('connect', function() {
        thingShadows.register(deviceName);
        console.log("Connected...");
        console.log("Registering...");

    });

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

thingShadows.publish('qrio/test/pi/voice', 'BedroomLightOn', function(){
        var cardTitle = "Bedroom Lamp on";
        var repromptText = "";
        var sessionAttributes = {};
        var shouldEndSession = false;
        var speechOutput = "Your bedroom light is on. ";
        repromptText = "Your bedroom light is on. ";
        var shouldEndSession = false;

    });
    console.log('value1 =', 'Bibin');
    ctx.succeed();

};

JAPDD commented 7 years ago

Hi i just took the reference of internet and wants to send the MQTT message to AWS IOT through a lambda function . I did in following manner

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' });

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()); });

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)" ] }

But with the same code if i would to send from my local PC to AWS IOT ..it works fine ...can you plz guide me .

nelsonic commented 6 years ago

@nbibin001 have you seen: https://github.com/dwyl/aws-lambda-iot-chat ?

soundarsurya commented 6 years ago

when i do test my lambda function..... i get this error "errorMessage": "Invalid connect options supplied.", "errorType": "Error", "stackTrace": [ "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)"

what it mean? where do i fix this..?? and how help me to fix this i have almost complete