Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you write to the file system of an aws lambda instance?

I am unsuccessfully trying to write to the file system of an aws lambda instance. The docs say that a standard lambda instance has 512mb of space available at /tmp/. However the following code that runs on my local machine isn't working at all on the lambda instance:

  var fs = require('fs');   fs.writeFile("/tmp/test.txt", "testing", function(err) {       if(err) {           return console.log(err);       }       console.log("The file was saved!");   }); 

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.

It's possible that this is a related question. 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.

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");                 }             });         }     }); }; 
like image 683
Rymnel Avatar asked Jan 26 '16 04:01

Rymnel


People also ask

Can you write files in AWS Lambda?

Conclusion. EFS for Lambda allows you to share data across function invocations, read large reference data files, and write function output to a persistent and shared store. After configuring EFS, you provide the Lambda function with an access point ARN, allowing you to read and write to this file system.

Does Lambda have a file system?

To configure file system accessOpen the Functions page of the Lambda console. Choose a function. Choose Configuration and then choose File systems. Under File system, choose Add file system.

How do I add a file to a Lambda function?

If want to upload file through lambda, one way is to open your AWS API Gateway console. There you will find "Binary Media Types" section. Save your changes. Then Go to "Resources" -> "proxy method"(eg.


2 Answers

Modifying your code into the Lambda template worked for me. I think you need to assign a function to exports.handler and call the appropriate context.succeed() or context.fail() method. Otherwise, you just get generic errors.

var fs = require("fs");  exports.handler = function(event, context) {     fs.writeFile("/tmp/test.txt", "testing", function (err) {         if (err) {             context.fail("writeFile failed: " + err);         } else {             context.succeed("writeFile succeeded");         }     }); }; 
like image 160
James Avatar answered Oct 06 '22 00:10

James


So the answer lies in the context.fail() or context.succeed() functions. Being completely new to the world of aws and lambda I was ignorant to the fact that calling any of these methods stops execution of the lambda instance.

According to the docs:

The context.succeed() method signals successful execution and returns a string.

By eliminating these and only calling them after I had run all the code that I wanted, everything worked well.

like image 42
Rymnel Avatar answered Oct 06 '22 01:10

Rymnel