Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js AWS动力消防软管对Lambda无响应_Node.js_Amazon Web Services_Aws Lambda_Amazon Kinesis_Amazon Kinesis Firehose - Fatal编程技术网

Node.js AWS动力消防软管对Lambda无响应

Node.js AWS动力消防软管对Lambda无响应,node.js,amazon-web-services,aws-lambda,amazon-kinesis,amazon-kinesis-firehose,Node.js,Amazon Web Services,Aws Lambda,Amazon Kinesis,Amazon Kinesis Firehose,以下是Lambda代码: const AWS = require('aws-sdk'); var firehose = new AWS.Firehose({ region: 'ap-southeast-2' }); exports.handler = async (event, context) => { var params = { DeliveryStreamName: 'TestStream', Record: { Data: 'test dat

以下是Lambda代码:

const AWS = require('aws-sdk');
var firehose = new AWS.Firehose({ region: 'ap-southeast-2' });

exports.handler = async (event, context) => {

var params = {
    DeliveryStreamName: 'TestStream', 
    Record: {
        Data: 'test data'
    }
};
console.log('params', params);

firehose.putRecord(params, function (err, data) {
    if (err) console.log(err, err.stack); // an error occurred
    else console.log('Firehose Successful',  data);           // successful response
});

}
政策是:

{
"Version": "2012-10-17",
"Statement": [
    {
        "Effect": "Allow",
        "Action": [
            "firehose:DeleteDeliveryStream",
            "firehose:PutRecord",
            "firehose:PutRecordBatch",
            "firehose:UpdateDestination"
        ],
        "Resource": [
            "arn:aws:firehose:ap-southeast-2:xxxxxxxxxxxxx:deliverystream/TestStream"
        ]
    }
]
}

我把其他的都删掉了。我在cloudwatch中看到的反应是:

2020-11-05T18:08:17.564+13:00   START RequestId: 3bed96b1-54af-4b08-bc06-be3732bba9ea Version: $LATEST

2020-11-05T18:08:17.568+13:00   2020-11-05T05:08:17.567Z 3bed96b1-54af-4b08-bc06-be3732bba9ea INFO params { DeliveryStreamName: 'TestStream', Record: { Data: <Buffer 74 65 73 74 20 64 61 74 61> } }

2020-11-05T18:08:17.621+13:00   END RequestId: 3bed96b1-54af-4b08-bc06-be3732bba9ea

2020-11-05T18:08:17.621+13:00   REPORT RequestId: 3bed96b1-54af-4b08-bc06-be3732bba9ea Duration: 57.38 ms Billed Duration: 100 ms Memory Size: 960 MB Max Memory Used: 85 MB Init Duration: 399.22 ms
2020-11-05T18:08:17.564+13:00启动请求ID:3bed96b1-54af-4b08-bc06-be3732bba9ea版本:$LATEST
2020-11-05T18:08:17.568+13:00 2020-11-05T05:08:17.567Z 3bed96b1-54af-4b08-bc06-be3732bba9ea信息参数{DeliveryStreamName:'TestStream',记录:{Data:}
2020-11-05T18:08:17.621+13:00结束请求ID:3bed96b1-54af-4b08-bc06-be3732bba9ea
2020-11-05T18:08:17.621+13:00报告请求ID:3bed96b1-54af-4b08-bc06-be3732bba9ea持续时间:57.38毫秒计费持续时间:100毫秒内存大小:960 MB最大使用内存:85 MB初始持续时间:399.22毫秒
因此,执行到达firehose行,并直接经过它们,而不做任何操作…

因为您正在使用,我认为问题在于您的函数在firehose代码有机会运行之前完成

纠正这种情况的一种方法是使用
Promise
,如中所示。例如:

const AWS = require('aws-sdk');
var firehose = new AWS.Firehose({ region: 'ap-southeast-2' });

exports.handler = async (event, context, callback) => {

    const promise = new Promise(function(resolve, reject) {

     var params = {
       DeliveryStreamName: 'TestStream', 
       Record: {
          Data: 'test data'
       }
     };
     console.log('params', params);

    firehose.putRecord(params, function (err, data) {
       if (err) console.log(err, err.stack); // an error occurred
       else console.log('Firehose Successful',  data);           //         successful response
     });
  })

  return promise;   
};

以上更改仅为示例性更改,因此可能仍需要进行一些调整。

太好了。这很有效。如何利用这个?也就是说,如果我将它放入一个函数中,并调用它两次,如
wait sendToFirehose()它只运行一次,然后停止?这听起来不对。@monkey对这个新问题不太确定。你可以为此提出新的问题。不过,如果我的回答有帮助的话,我会很感激你接受我的回答。我创建了一个新问题来捕捉我的上述评论: