[英]Publishing message from AWS Lambda to AWS IoT
I am trying to publish message from AWS Lamba using Nodejs to AWS IoT .我正在尝试使用 Nodejs 从 AWS Lamba 向 AWS IoT 发布消息。 I have zipped the project and uploaded on to the AWS IoT below is the code snippet
我已经压缩了项目并上传到 AWS IoT 下面是代码片段
var awsIot = require('aws-iot-device-sdk');
var device = awsIot.device({
keyPath: 'keyfilepath',
certPath: 'pem file path',
caPath: 'root-CA.crt',
clientId: 'iotTest7526532135',
host: 'host id'
});
device
.on('connect', function() {
console.log('connect');
device.subscribe('topic_3');
device.publish('topic_1', JSON.stringify({ message_id:23,Message:'HelloWorld'}));
});
device
.on('message', function(topic, payload) {
console.log('message', topic, payload.toString());
});
I am getting below error我得到以下错误
"errorMessage": "Cannot find module 'aws-iot-device-sdk'", "errorMessage": "找不到模块 'aws-iot-device-sdk'",
I know that iot sdk is missing, I am not sure how to install it on AWS Lambda.我知道缺少 iot sdk,我不确定如何在 AWS Lambda 上安装它。
Any suggestions will be really helpful任何建议都会非常有帮助
I would highly recommend not using the aws-iot-device-sdk to interact with AWS Iot from a Lambda function.我强烈建议不要使用 aws-iot-device-sdk 从 Lambda 函数与 AWS Iot 交互。
You need to understand there are 2 javascript APIs that you can use to access AWS IoT您需要了解有 2 个 javascript API 可用于访问 AWS IoT
There are multiple reasons why you should opt for the HTTP based SDK :您应该选择基于 HTTP 的 SDK 的原因有很多:
Your code can become as simple as this (notice that there are no credentials or extra node modules required) :您的代码可以变得如此简单(请注意,不需要凭据或额外的节点模块):
var AWS = require('aws-sdk');
var iotdata = new AWS.IotData({endpoint:"yourendpoint.iot.eu-central-1.amazonaws.com"});
exports.handler = function(event, context, callback) {
console.log("found iotdata",iotdata);
var params = {
topic: 'topic/test',
payload: 'blah',
qos: 0
};
iotdata.publish(params, function(err, data){
if(err){
console.log("Error occured : ",err);
}
else{
console.log("success.....");
}
});
callback();
};
When you zip your project, you also zip ./node_modules
folder.压缩项目时,也会压缩
./node_modules
文件夹。 So as long as aws-iot-device-sdk
is there (along with all the dependencies), your Lambda will be fine.因此,只要
aws-iot-device-sdk
存在(以及所有依赖项),您的 Lambda 就可以了。
So all you need is:所以你只需要:
npm install aws-iot-device-sdk
zip ...
You'll need to make sure you're uploading your package.json
file as well, which should have a dependency requirement for aws-iot-device-sdk
您还需要确保您正在上传
package.json
文件,该文件应该对aws-iot-device-sdk
有依赖性要求
you can add the package to your package.json by running您可以通过运行将包添加到 package.json
npm -i --save aws-iot-device-sdk
from your project directory.从你的项目目录。
Add something like the below in your package.json file.在 package.json 文件中添加如下内容。 then run
npm install
.然后运行
npm install
。 This will create a node_modules folder.这将创建一个 node_modules 文件夹。 Now zip it and upload it once again.
现在将其压缩并再次上传。
"aws-iot-device-sdk": "^2.1.0"
If you are just going to publish the data to IoT topic, you are better of using http protocol instead of MQTT connection.如果您只是要将数据发布到 IoT 主题,则最好使用 http 协议而不是 MQTT 连接。
And using HTTP connection doesnt require aws-iot-device-sdk package.并且使用 HTTP 连接不需要 aws-iot-device-sdk 包。 AWS default SDK has iotdata.
AWS 默认 SDK 具有 iotdata。 And iotdata will provide the http connection to the device.
iotdata 将提供到设备的 http 连接。
iotHTTPConnection= AWS.IotData({endpoint: Your_END_POINT}); iotHTTPConnection= AWS.IotData({endpoint: Your_END_POINT});
iotHTTPConnection.publish(params) will publish the data using http without iot specific sdk. iotHTTPConnection.publish(params) 将使用 http 发布数据,而无需特定于 iot 的 sdk。
https://docs.aws.amazon.com/iot/latest/apireference/API_iotdata_Publish.html https://docs.aws.amazon.com/iot/latest/apireference/API_iotdata_Publish.html
There is no http subscribe though.虽然没有 http 订阅。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.