MQTT in AWS Lambda function for Alexa Javascript

Please help, I need to use mqtt protocol in lambda function to send some data to a broker. I use simple code to test it :

mqtt = require('mqtt');
var client  = mqtt.connect('mqtt://test.mosquitto.org');

client.on('connect', function () {
  client.subscribe('presence');
  client.publish('presence', 'Hello mqtt');
});

client.on('message', function (topic, message) {
  // message is Buffer 
  console.log(message.toString());
  client.end();
});

But I get an error "Cannot find module 'mqtt'", how can I include this module in the lambda function??? How can I use mqtt in my lambda anyways?? Somebody???

Answers:

Answer

First you will do in the directory of your project:

npm install mqtt --save 

after you will zip this folder (inside the folder, the files and subdirectories) and upload to your lambda function.

Every time you must create a handler function, so you will create a function like this:

exports.handler  = function (event, context, callback) {
... your code...

}

in your lambda function at the AWS panel you will appoint to the file and the function you are using in Handler text field.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.