Search code examples
node.jstypescriptaws-lambda

How to access /tmp folder in Lambda with in Node?


I'm trying to save a processed image momentally in /tmp folder but it's not working for me. Assuming it's under the root folder I'm trying to get it this way:

let tempraryImageDirectory: string;
if (process.env.DEV && process.env.DEV === 'Yes') {
  tempraryImageDirectory = path.join(__dirname, `../../tmp/`);
} else {
  tempraryImageDirectory = path.join(__dirname, `./tmp/`);
}

The else choice here is to test locally. I don't want to create a /tmp folder in the root directory. Locally everything is very fine. But in Lambda at the moment any operation happens on the directory CloudWatch never shows any of my logs written after that and my function fails for unknown reason. Any idea if I'm addressing the /tmp folder correctly?


Solution

  • The directory is just /tmp, it is not relative to the working directory:

    let tempraryImageDirectory: string;
    if (process.env.DEV && process.env.DEV === 'Yes') {
      tempraryImageDirectory = path.join(__dirname, `../../tmp/`);
    } else {
      tempraryImageDirectory = '/tmp/';
    }
    

    You may also wish to rename your variable to include the o in temporary if you did not leave it out on purpose.