By using AWS re:Post, you agree to the Terms of Use
/How to save a file to a tmp/temp folder in lambda?/

How to save a file to a tmp/temp folder in lambda?

0

is it possible to save a file , where the lambda is running. i am using a javascript library to manipulate some text , same code below . I am using a thirdparty async function to get some data and as the returned array can be quite big, i think it is failing , when i try to upload it to s3. how can i save it to a file in /tmp/file.txt or where the lambda is running , so that i can debug to see at least the data is being returned and then is it possible to upload the file to and s3 and then delete it locally?

thirdparty.func()
.then(function(data){
    //data returned is an array
   data.forEach(function(val){
          //write val to a local file 
   })
})
.catch(e=>{console.log(e)});
1 Answers
0

You can write files to /tmp in your Lambda function. By default the size is limited to 512 MB, but you can increase it up to 10 GB. Note that the /tmp is inside the function's execution environment and you do not have access to it from outside the function. If you want to save a file that you can access it externally, you should either save it to S3 or mount an EFS volume and write the file there.

Inside your Lambda function you can do almost anything, so yes, you can copy files to S3 and you can delete them.

EXPERT
answered a month ago
  • @Uri - thanks. i'm having an issue with reading the file . is there a pre-built lambda layer for aws cli sdk for node js that i can use with my lambda and may be issue commands like aws s3 cp /.tmp.sample.txt s3://bucket in my nodejs lambda?

  • I am not familiar of any such extension. The following code should upload a local file to S3:

    const uploadFile = (fileName) => {
        const content = fs.readFileSync(fileName);
    
        const params = {
            Bucket: BUCKET_NAME,
            Key: OBJECT_NAME,
            Body: content
        };
    
        s3.upload(params, function(err, data) {
            if (err) {
                throw err;
            }
        });
    };

    I suggest that you await for it to complete.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions