title: How to wait for async actions inside AWS Lambda?
body: <p>I am trying to process uploaded file in S3. Since <strong>getObject</strong> is asyncronous main function ends before processing is done, and AWS kills lambda in 3-4 seconds.</p>
  
  <p>Even worse, processing method also has async operations in it - it makes http calls.</p>
  
  <p>On high level, my code looks like:</p>
  
  <pre><code>exports.handler = function(event, context) {
      // Get the object from the event and show its content type
      var bucket = event.Records[0].s3.bucket.name;
      var key = event.Records[0].s3.object.key;
      var params = {
          Bucket: bucket,
          Key: key
      };
      s3.getObject(params, function(err, data) {
          if (err) {
               ...
          } else {
              processFile(data.Body.toString(), 0);
              console.log("ok");
          }
      });
      //need to wait here till processFile is done
  };
  
  processFile = function(content, start) {
    ... build url to call
    http.get(url, function(res) {  
      console.log("Got response: " + res.statusCode + ");
      processFile(content, start + 1);
    });
  }
  </code></pre>
  
  <p>I find out that there is async in nodejs but it is not included by amazon; Both  require('async') or require('sleep) causes errors.</p>
  
  <p>Lambda timeout configured to 60 seconds, but it exits in 3-4 seconds.</p>