0

I'm having issues writing a file to the tmp folder in lambda instance.

const fs = require('fs-extra');
const os = require('os');
const cwd = process.cwd();

const buffer = Buffer.from(arrayBuffer, 'base64');
const newFileName = 'myFile.png';
const temporaryImageDirectory = os.tmpdir();

// filePath works locally, but doesn't work deployed to lambda. 
// No errors in cloudwatch
const filePath = `/tmp/${newFileName}`; // works locally

// filePath1 works locally, but doesn't work deployed to lambda.
// No errors in cloudwatch
const filePath1 = `${cwd}/${newFileName}`; // works locally

// filePath2 works locally, but doesn't work deployed to lambda.
// Cloudwatch Error: EROFS: read-only file system, open '/var/task/myFile.png'
const filePath2 = `${temporaryImageDirectory}/${newFileName}`; // works locally

console.log('FILE PATH: ', filePath);
console.log('FILE PATH1: ', filePath1);
console.log('FILE PATH2: ', filePath2);

fs.writeFile(filePath, buffer, 'utf-8')
    .then(() => {
        webhook.sendFile(filePath);
    })
    .catch((e) => {
        console.log('Error', e);
    });

console.log FILE PATHs in cloudwatch

enter image description here

filePath1 full error

Error [Error: EROFS: read-only file system, open '/var/task/myFile.png'] {
  errno: -30,
  code: 'EROFS',
  syscall: 'open',
  path: '/var/task/myFile.png'
}

How can I get the file to get stored temporarily successfully so that I can send it to it's destination webhook? I have no issues sending the file locally via serverless offline, but obviously in AWS is a little different. filePath, filePath1, and filePath2 all don't work in AWS, but work locally. What am I missing? Any help is appreciated!

user6680
  • 79
  • 6
  • 34
  • 78
  • If you're writing to `\`/tmp/${newFileName}\``, why are you not using that same path with `.sendFile()`? – jfriend00 Feb 18 '23 at 17:58
  • FYI, if you're just looking for a safe tmp directory, you can use [`os.tmpdir()`](https://nodejs.org/api/os.html#ostmpdir) to get that directory and then append your filename to that. – jfriend00 Feb 18 '23 at 18:05
  • Also don't know why you're using `await` and `.then()` together. Use one or the other. – jfriend00 Feb 18 '23 at 18:42
  • Those are fair points. I've updated my code per your suggestions and updated my post as well with more details. I tried using os.tmpdir() as well, but didn't have much luck with it working in AWS although it did work locally with sls offline. See original post for updates – user6680 Feb 18 '23 at 19:35
  • See [temp directory on AWS lambda](https://stackoverflow.com/questions/50946378/can-i-store-a-temp-file-on-aws-lambda-function). – jfriend00 Feb 19 '23 at 00:58
  • I'm not looking for a persistent storage solution. I just need to send the image file to a webhook then I don't care about the image file anymore. It can be deleted when invocation is done or lambda cools down. – user6680 Feb 19 '23 at 05:33
  • Per that post, `\`/tmp/${newFileName}\`` should work on AWS lambda. That's what it says. – jfriend00 Feb 19 '23 at 06:14
  • I managed it to see file in tmp directory. The webhook is sending it to a discord channel, but it doesn't seem to render correctly. Looks like this currently: https://postimg.cc/MfSQB3kN Renders fine when sent locally, but not from AWS. I'll accept an answer if you wanna post it. This seems like a different issue. Thanks. – user6680 Feb 19 '23 at 17:46

0 Answers0