在lambda函数中得到Aws的东西影子

时间:2016-06-02 05:11:58

标签: node.js amazon-web-services aws-lambda aws-iot

我试图在lambda函数中获取aws iot资源的影子但是给定代码在成功而不是数据时给出null值。请让我知道问题出在哪里,我应该做些什么改变才能让它正常工作。提前致谢。

var AWS=require('aws-sdk');
var iotdata = new AWS.IotData({endpoint: 'XXXXXXXXX.iot.us-east-1.amazonaws.com'});
var params = {
thingName: 'thing_name' /* required */
};

exports.handler=function(event,context){
payload1=new Buffer(event.payload);
console.log(payload1); 
iotdata.getThingShadow(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response

  context.succeed(data);

});

};

1 个答案:

答案 0 :(得分:0)

几乎没有需要更改的地方。这是新代码:

getIotShadow: function (thingName) {
    config.IOT_BROKER_ENDPOINT = "xxxxxxxxx.iot.us-east-1.amazonaws.com"; // also called the REST API endpoint
    config.IOT_BROKER_REGION = "us-east-1"; // eu-west-1 corresponds to the Ireland Region.  Use us-east-1 for the N. Virginia region
    config.IOT_THING_NAME = thingName;
    AWS.config.region = config.IOT_BROKER_REGION;
    var iotData = new AWS.IotData({
        endpoint: config.IOT_BROKER_ENDPOINT
    });

    var paramsGet = {
        "thingName": config.IOT_THING_NAME /* required */
    };

    iotData.getThingShadow(paramsGet, function (err, data) {
        if (err) {
            console.log("Error : " + err, err.stack);
        } else {
            console.log(JSON.stringify(data));
        }
    });
}