node.js - Properly handling context.succeed()/context.fail() with AWS S3 service calls in AWS Lambda -
i've searched through posts here (i.e. how structure sequential aws service calls within lambda given calls asynchronous?) , elsewhere, , can't seem find 1 little bit of information me past annoying issue. when have lambda function iterates through loop, , within loop makes call s3.putobject(), runs short-circuit issue when trying deal context.succeed()/context.fail() or older context.done(null, 'msg') way of closing lambda process.
i.e. iteration needs call s3.putobject() current object uploaded, still output cloudwatch or possibly sqs/sns file successfully uploaded. however, of attempts @ putting type of closure function meets random results of getting file names, other times getting of file names, etc.
what best way it? i've attempted use q , async honest, i'm still learning of stuff..
below rough example of i'm attempting do:
function output(s3object){ s3.putobject(s3object, function(err, data){ if (err) { console.log('there issue outputting object.', err); } else { // how close if have x number of incoming calls?? // context.done(null, 'success'); } // , later in code calls output function // , note: should output of file names invocation uploads! (var = 0; < myrecords.length; a++){ output(myrecords[a]); } but, said previously, attempts i've made far, mixed results.
successfully output object: mybucket/prefix/part_000000123432345.dat output object: mybucket/prefix/part_000000123432346.dat but test of function outputs:
successfully output object: mybucket/prefix/part_000000123432346.dat argh.
i'll give simple example using async can adapt:
var async = require('async'); var sleep = function(message, callback) { settimeout(function() { callback(null, "echo: " + message); }, math.floor(math.random() * 2000)); }; exports.handler = function(event, context) { async.map(['a', 'b', 'c', 'd', 'e'], sleep, context.done); }; here i've defined sleep() function takes message , callback, sleeps random amount of time between 0 , 2 seconds, echoes message callback.
we use async.map() invoke sleep() function asynchronously on 5 different messages. per the docs, callback function, in case context.done, called when all iterator functions have finished. run in lambda console , sure enough, get:
[ "echo: a", "echo: b", "echo: c", "echo: d", "echo: e" ] so code might simple as:
async.map(myrecords, s3.putobject, context.done); but have no way of testing that, leave part you.
Comments
Post a Comment