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

1

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)});
gefragt vor 2 Jahren23324 Aufrufe
1 Antwort
1

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.

profile pictureAWS
EXPERTE
Uri
beantwortet vor 2 Jahren
profile pictureAWS
EXPERTE
überprüft vor 10 Monaten
  • @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.

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen