title: How do you write to the file system of an aws lambda instance?
body: <p>I am unsuccessfully trying to write to the file system of an aws lambda instance.  <a href="http://docs.aws.amazon.com/lambda/latest/dg/limits.html" rel="noreferrer">The docs say</a> that a standard lambda instance has 512mb of space available at <code>/tmp/</code>. However the following code that runs on my local machine isn't working at all on the lambda instance:</p>
  
  <pre><code>  var fs = require('fs');
    fs.writeFile("/tmp/test.txt", "testing", function(err) {
        if(err) {
            return console.log(err);
        }
        console.log("The file was saved!");
    });
  </code></pre>
  
  <p>The code in the anonymous callback function is never getting called on the lambda instance.  Anyone had any success doing this?  Thanks so much for your help.</p>
  
  <p>It's possible that this is a related <a href="https://stackoverflow.com/questions/30307208/fs-writefile-callback-never-gets-called-same-for-writablestream-write-etc">question</a>. Is it possible that there is some kind of conflict going on between the s3 code and what I'm trying to do with the fs callback function?  The code below is what's currently being run.</p>
  
  <pre><code>console.log('Loading function');
  
  var aws = require('aws-sdk');
  var s3 = new aws.S3({ apiVersion: '2006-03-01' });
  var fs = require('fs');
  
  exports.handler = function(event, context) {
      //console.log('Received event:', JSON.stringify(event, null, 2));
  
      // Get the object from the event and show its content type
      var bucket = event.Records[0].s3.bucket.name;
      var key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
      var params = {
          Bucket: bucket,
          Key: key
      };
      s3.getObject(params, function(err, data) {
          if (err) {
              console.log(err);
              var message = "Error getting object " + key + " from bucket " + bucket +
              ". Make sure they exist and your bucket is in the same region as this function.";
              console.log(message);
              context.fail(message);
          } else {
  
              //console.log("DATA: " + data.Body.toString());
              fs.writeFile("/tmp/test.csv", "testing", function (err) {
  
                  if(err) {
                      context.failed("writeToTmp Failed " + err);
                  } else {
                      context.succeed("writeFile succeeded");
                  }
              });
          }
      });
  };
  </code></pre>