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)});
已提问 2 年前23323 查看次数
1 回答
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
专家
Uri
已回答 2 年前
profile pictureAWS
专家
已审核 10 个月前
  • @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.

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则