AWS Kinesis Firehose tidak menanggapi Lambda
Nov 05 2020
Inilah kode 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
});
}
Dan kebijakannya adalah:
{
"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"
]
}
]
}
Saya benar-benar memotong semua yang lain. Dan tanggapan yang saya lihat di cloudwatch adalah:
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
Jadi, eksekusi mencapai garis tembak, dan langsung melewati mereka, tanpa melakukan apa pun ...
Jawaban
1 Marcin Nov 05 2020 at 12:29
Since you are using async handler I think the issue is that your function completes before firehose code has a chance to run.
One way to rectify this is through the use of Promise as shown in AWS docs. For example:
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;
};
The above changes are exemplary only, thus probably some adjustments will be still needed.
Kiat Pemilik Anjing yang Bermanfaat: Mengapa Penting untuk Membiarkan Anjing Anda Mengendus di Jalan
Jana Duggar: Semua yang Dia Katakan Tentang Cinta dan Jendela 5 Tahunnya untuk Menemukan 'Yang Satu'