Search code examples
node.jsamazon-web-servicesaws-lambdaamazon-kinesisamazon-kinesis-firehose

AWS Kinesis Firehose not responding to Lambda


Here's the Lambda code:

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
});

}

And the policy is :

{
"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"
        ]
    }
]

}

I've cut literally everything else out. And the response I'm seeing in cloudwatch is:

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

So, execution reaches the firehose lines, and goes straight past them, without doing anything...


Solution

  • 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.