【问题标题】:How to publish to iot stream from lambda如何从 lambda 发布到物联网流
【发布时间】:2018-08-05 07:28:40
【问题描述】:

当我尝试调用 iotData 发布函数时,我的 lambda 函数一直超时。代码如下。它总是超时而没有错误。此功能可从 sam 本地命令行运行。我想这是 lambda 的权限错误。奇怪的是,我已经为这个 lambda 函数授予了 IoT、Kinesis 和 SNS 的权限,但没有任何效果。

'use strict'; 
console.log('Loading function'); 

require('dotenv').config();
const {Pool} = require('pg');
const pool = new Pool();
const AWS = require('aws-sdk');
console.log("finished loading");


/**  * Provide an event that contains the following keys:  
 * *  *   - resource: API Gateway resource for event  
 * *   - path: path of the HTTPS request to the microservices API call  
 * *   - httpMethod: HTTP method of the HTTPS request from microservices API call  
 * *   - headers: HTTP headers for the HTTPS request from microservices API call  
 * *   - queryStringParameters: query parameters of the HTTPS request from microservices API call  
 * *   - pathParameters: path parameters of the HTTPS request from microservices     API call  
* *   - stageVariables: API Gateway stage variables, if applicable  
 * *   - body: body of the HTTPS request from the microservices API call  
 * */ 

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

console.log("starting");
let _response = ""; 
context.callbackWaitsForEmptyEventLoop = false;

if(event.httpMethod==="POST" && event.resource==="/pings"){
    var body = JSON.parse(event.body);
    console.log("here2");
    pool.query("SELECT name from pings where test = decode($1,'hex');",[body.bid], (err,res)=>{
        if(err){
            console.error(err.stack);
            _response = buildOutput(500, {
                message:"error in pg"
            });         
            callback(_response, null);
        }
        console.log("here3");
        var iotdata = new AWS.IotData({endpoint:'XXXXXXX.iot.us-east-1.amazonaws.com'});
        const publishParams = {
            topic: body.topic,
            payload: Buffer.from(JSON.stringify({
                message: "Welcome to "+res.rows[0].name+" house"
            }), 'utf8'),
            qos: 0
        }
        console.log("here4");
        iotdata.publish(publishParams, function(err, data) {
            if(err){
                console.error(err.stack);
                _response = buildOutput(500, {
                    message:"error in pg"
                });      
                callback(_response,null);
            }
            _response = buildOutput(200, {message: "success"});             
            callback(null, _response);
        });
    });
} else {
    _response = buildOutput(500, {
        message:"path not found"
    });      
    callback(_response,null);
}
};
/* Utility function to build HTTP response for the microservices output */ 
function buildOutput(statusCode, data) {  
let _response = {         
    statusCode: statusCode,
    headers: {
        "Access-Control-Allow-Origin": "*"
    },
    body: JSON.stringify(data)
}; 

return _response; 
}

政策

    {
        "Sid": "",
        "Effect": "Allow",
        "Action": [
            "iot:*"
        ],
        "Resource": "*"
    },

更新: 我试图暂时授予 lambda 函数管理员访问权限,但这甚至不起作用。

【问题讨论】:

  • 你解决了吗?我也在尝试从 lambda 函数向主题发布消息。 @fritz

标签: aws-lambda aws-sdk aws-iot


【解决方案1】:

这是您可以直接从 lambda 函数在 MQTT 主题上发帖的方式。 使用 Node.js 10.x 编写的代码

var AWS = require('aws-sdk');
const IOT_ENDPOINT = "yourIoTentPoint.iot.region.amazonaws.com"

var iotdata = new AWS.IotData({endpoint:IOT_ENDPOINT});

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

var params = {
    topic: 'my/topic',
    payload: 'This is my menssage from Lambda function.',
    qos: 1
    };

iotdata.publish(params, function(err, data){
    if(err){
        callback(err, null);
    }
    else{
        callback(null, {"published_message": params.payload, "topic": params.topic});
    }
  });
};

此外,您还可以通过 IoT Core>AWS 控制台测试查看订阅主题 my/topic 发送的消息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-01
    • 1970-01-01
    相关资源
    最近更新 更多