将消息从AWS Lambda发布到AWS IoT [英] Publishing message from AWS Lambda to AWS IoT

查看:85
本文介绍了将消息从AWS Lambda发布到AWS IoT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Nodejs将AWS Lamba的消息发布到AWS IoT.我已将项目压缩并上传到AWS IoT下面是代码段

I am trying to publish message from AWS Lamba using Nodejs to AWS IoT . I have zipped the project and uploaded on to the AWS IoT below is the code snippet

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

我遇到错误
"errorMessage":无法找到模块'aws-iot-device-sdk'",

I am getting below error
"errorMessage": "Cannot find module 'aws-iot-device-sdk'",

我知道缺少物联网sdk,我不确定如何在AWS Lambda上安装它.

I know that iot sdk is missing, I am not sure how to install it on AWS Lambda.

任何建议都会很有帮助

推荐答案

我强烈建议不要使用aws-iot-device-sdk从Lambda函数与AWS Iot进行交互.

I would highly recommend not using the aws-iot-device-sdk to interact with AWS Iot from a Lambda function.

您需要了解有2种JavaScript API可用于访问AWS IoT

You need to understand there are 2 javascript APIs that you can use to access AWS IoT

  • The AWS IOT Device SDKs for javascript, using MQTT as a protocol and x509 certificates for authentication. These are typically used in devices running outside of your AWS cloud.
  • The AWS SDK for javascript, using HTTP as a protocol, and IAM roles (among other things) for authentication. These SDKs are typically run inside your AWS cloud (such as a Lambda)

选择基于HTTP的SDK的原因有多种:

There are multiple reasons why you should opt for the HTTP based SDK :

  • aws-iot-device-sdk专门针对需要远程连接的,在AWS外的生活"设备(物联网网关/现场设备).
  • 设备SDK使用MQTT和x509证书与AWS IoT进行交互.无需在lambda中配置x509证券.在您的AWS账户上运行的Lambda可以通过IAM角色轻松访问AWS IoT,因此,如果为lambda函数配置了正确的角色,则可以使用标准的AWS开发工具包.
  • 像MQTT这样的协议(或任何发布/订阅协议)与无服务器lambda体系结构不太匹配.在lambda函数中,您正在订阅一个主题,而在lambda函数短暂的情况下,您通常不会这样做.
  • 现成的lambda提供了适用于NodeJS的AWS开发工具包.不需要或打包其他节点模块)

您的代码可以变得如此简单(请注意,不需要凭据或额外的节点模块):

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

这篇关于将消息从AWS Lambda发布到AWS IoT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆