The process is completed before the request error completes when testing a function in AWS Lambda

After completing the AWS Lambda sketching tutorial, I decided to try and tweak the code to check if the file was a jpg or csv file, and just moved it to a new bucket. The only thing I removed from my code is the comments and the function inside async.waterfall that will resize the images. However, whenever I test or run this new code, I get a "process completed before the request is completed" and the function does not transfer files correctly. Here is the code:

var async = require('async'); var AWS = require('aws-sdk'); var gm = require('gm') .subClass({ imageMagick: true }); // Enable ImageMagick integration. var util = require('util'); var s3 = new AWS.S3(); exports.handler = function(event, context) { console.log("Reading options from event:\n", util.inspect(event, {depth: 5})); var srcBucket = event.Records[0].s3.bucket.name; var srcKey = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " ")); var dstBucket = 'datacollectionbucket'; var dstKey = srcKey; if (srcBucket == dstBucket) { console.error("Destination bucket must not match source bucket."); return; } var typeMatch = srcKey.match(/\.([^.]*)$/); if (!typeMatch) { console.error('unable to infer file type for key ' + srcKey); return; } var imageType = typeMatch[1]; if (imageType != "jpg" && imageType != "csv") { console.log('skipping unrecognized file ' + srcKey); return; } async.waterfall([ function download(next) { s3.getObject({ Bucket: srcBucket, Key: srcKey }, next); function upload(contentType, data, next) { s3.putObject({ Bucket: dstBucket, Key: dstKey, Body: data, ContentType: contentType }, next); } ], function (err) { if (err) { console.error( 'Unable to resize ' + srcBucket + '/' + srcKey + ' and upload to ' + dstBucket + '/' + dstKey + ' due to an error: ' + err ); } else { console.log( 'Successfully classified ' + srcBucket + '/' + srcKey + ' and uploaded to ' + dstBucket + '/' + dstKey ); } context.done(); } ); }; 

Thanks guys,

+4
source share
1 answer

This means that you do not call context.done () before returning from your function or that your work does not end in lambda timeout.

For the first opportunity, you have several blocks of code in your example:

 if (srcBucket == dstBucket) { console.error("Destination bucket must not match source bucket."); return; } 

All this should be like this:

 if (srcBucket == dstBucket) { var errorText = "Destination bucket must not match source bucket." console.error(errorText); context.done(errorText); return; } 

If you pass any arguments to context.done (), then they assume that the first one is an error. (If you have no errors, just call context.done (), as you already do at the end of your waterfall).

So it is likely that one of these error cases is hit, but you are not getting the error back from lambda.

Another way that can happen is that S3 calls to getObject and putObject take longer than the assigned value of the lambda function. You can try increasing it to 60 seconds in the "config" tab of the parameters of the lambda function to find out if you will begin to receive answers.

0
source

All Articles