简体   繁体   English

将消息从 AWS Lambda 发布到 AWS IoT

[英]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

  • The AWS IOT Device SDKs for javascript , using MQTT as a protocol and x509 certificates for authentication.适用于 javascriptAWS IOT 设备开发工具包,使用 MQTT 作为协议和 x509 证书进行身份验证。 These are typically used in devices running outside of your AWS cloud.这些通常用于在 AWS 云之外运行的设备。
  • The AWS SDK for javascript , using HTTP as a protocol, and IAM roles (among other things) for authentication.适用于 javascriptAWS 开发工具包,使用 HTTP 作为协议,以及用于身份验证的 IAM 角色(除其他外)。 These SDKs are typically run inside your AWS cloud (such as a Lambda)这些 SDK 通常在您的 AWS 云(例如 Lambda)中运行

There are multiple reasons why you should opt for the HTTP based SDK :您应该选择基于 HTTP 的 SDK 的原因有很多:

  • The aws-iot-device-sdk is specifically targeted toward devices "living" outside of Aws (IoT gateways / devices in the field) that need to remotely connect. aws-iot-device-sdk 专门针对需要远程连接的“生活”在 Aws 之外的设备(物联网网关/现场设备)。
  • The Device SDK uses MQTT and x509 certificates to interact with AWS IoT.设备开发工具包使用 MQTT 和 x509 证书与 AWS IoT 交互。 There is no need to configure x509 securities in your lambda.无需在 lambda 中配置 x509 证券。 A Lambda running on your AWS account can easily access AWS IoT through an IAM role, so if your lambda function is configured with the correct role, you can use the standard aws sdks.在您的 AWS 账户上运行的 Lambda 可以通过 IAM 角色轻松访问 AWS IoT,因此如果您的 lambda 函数配置了正确的角色,您可以使用标准的 aws sdks。
  • A protocol like MQTT (or any pub/sub protocol) doesn't match well with a serverless lambda architecture.像 MQTT(或任何发布/订阅协议)这样的协议与无服务器 lambda 架构不太匹配。 In your lambda function you are subscribing to a topic, something that you don't typically do in a short-lived lambda function.在您的 lambda 函数中,您订阅了一个主题,这是您在短期 lambda 函数中通常不会做的事情。
  • The AWS SDK for NodeJS is provided to your lambda out of the box.There's no need to require or package additional node modules)适用于 NodeJS 的 AWS 开发工具包开箱即用提供给您的 lambda。无需要求或打包其他节点模块)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM