title: My Lambda is timeout with my S3 upload
body: <p>I didn't understand why my Lambda doesn't work with my csv upload.</p>
  
  <pre class="lang-js prettyprint-override"><code>var AWS = require('aws-sdk');
  var mysql = require('mysql');
  var json2csv = require('json2csv');
  
  exports.handler = function (event, context) {
  
      AWS.config = {
          region: 'us-east-1',
          apiVersions : {
              s3: '2006-03-01'
          }
      };
  
      var s3 = new AWS.S3({signatureVersion: 'v4'});
      var bucket = 'search-dev'; 
  
      if(event.debug == true)
      {
          var connection = mysql.createConnection({
                              host     : event.database.host,
                              user     : event.database.user,
                              password : event.database.pwd,
                              database : event.database.name
                          });
      }
  
      connection.connect(function(err) {
          if (err) {
              console.error('error connecting: ' + err.stack);
              context.fail(err);
          }
      });
  
      mysqlData();
  
      connection.end();
  
      function mysqlData () {
          connection.query('SELECT id, keywords, result_count FROM keyword WHERE result_count != ""', function (err, results, fields) {
              if (err) {
                  console.error('error connecting: ' + err.stack);
                  context.fail(err);
              }
  
              console.log(results.length + " trouvés");
  
              var csvFields = [];
              for (var i in fields) {
                  csvFields.push(fields[i].name);
              }
  
              var csv = json2csv({ data: results, fields: csvFields });
  
              writeInFile(csv);
          });
      }
  
      function writeInFile(dataCsv)
      {
          filename = generateFilename();
          var params = {
              ACL: 'public-read',
              Bucket: bucket,
              Key: filename+'.csv',
              Body: dataCsv
          };
          s3.upload(params, function (err, data) {
              if (err) {
                  console.error(err, err.stack); // an error occurred
                  context.fail(err);
              }
  
              context.succeed({url: data.Location});
          });
  
      }
  
      function generateFilename() 
      {
          var date = new Date();
  
          var filename = date.getDate()+'-'+date.getMonth()+'-'+date.getFullYear()+'_'+date.getHours()+':'+date.getMinutes()+':'+date.getSeconds();
  
          return filename;
      }
  
  };
  </code></pre>
  
  <p>On other lambda function, there aren't a problem but here, it's all the time timeout with s3.upload my Lambda have S3FullAccess in IAM.</p>
  
  <p>This code works perfectly in local.</p>
  
  <p>And others problem is I created a new bucket but impossible to upload on it in local too <code>[NoSuchBucket: The specified bucket does not exist]</code> I don't understand because I created other bucket and they aren't a problem to upload on it.</p>
  
  <p>Thanks </p>