如何从lambda发布到物联网流>>

问题描述 投票:1回答:1

当我尝试调用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": "*"
    },

UPDATE:

我试图暂时给lambda函数管理员访问权限,但这甚至没有用。

当我尝试调用iotData发布函数时,我的lambda函数一直保持超时。下面的代码。它总是超时而不会出错。此功能从sam本地命令行运行。我...

aws-lambda aws-sdk aws-iot
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.