Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda /tmp folder is not shared between executions

I'm trying to temporary cache some data for AWS lambda function executions to omit HTTP request that this lambda does every time right now and optimise its' speed.

In "viewer request" I have the following logic (example is pseudo-code) -

exports.handler = async (event) => {
   // check /tmp folder for data
   const cachedData = await getDataFromCache();

   if (cachedData) {
     return cachedData;
   }

   const data = await getDataFromApi();
   
   // save data for 2 mins in /tmp for re-use in next executions
   await saveDataInCache(data);

   return data;
}

As you can see, I'm trying to save the data in /tmp folder for a few mins in order to re-use it in next executions and reduce the number of API requests.

During "viewer request" lambda execution, when I cache data inside /tmp folder, I immediately see that data is available in the logs -

async function saveDataInCache(data = {}) {
  // save data in tmp folder
  try {
    await fs.outputJson('/tmp/cache.json', {
      data,
      updatedAt: Date.now()
    });
  } catch (err) {
    console.log(`page data save cache error ${err}`);
  }

  // check that data is immediately available
  let fileData = {};
  try {
    fileData = await fs.readJson('/tmp/cache.json');
  } catch (err) {
    console.log(`page data read cache error ${err}`);
  }

  console.log(`check immediate value ${JSON.stringify(fileData)}`);
}

However every time when "viewer request" tries to read the data before saving, it always returns error - ENOENT: no such file or directory, open '/tmp/cache.json' -

async function getDataFromCache() {
  let fileData = {};

  try {
    fileData = await fs.readJson('/tmp/cache.json');
  } catch (err) {
    console.log(`page data read cache error ${err}`);
  }

  if (isEmpty(fileData)) {
    return undefined;
  }

  const { data = {}, updatedAt = 0 } = fileData;
  const maxCacheTime = updatedAt + DATA_TTL;
  const currentTime = Date.now();
  const isCacheExpired = currentTime > maxCacheTime;

  if (!isCacheExpired) {
    return data;
  }

  return undefined;
}

Does it mean that /tmp folder content is not shared between executions? Thanks!

like image 637
Kosmetika Avatar asked Feb 08 '26 00:02

Kosmetika


1 Answers

The /tmp directory is private to each Lambda instance/execution context, it's not shared among all Lambda instances. That means if your request is handled by a different execution context, the data won't be present there.

Unfortunately you can't really control how these execution contexts are created and deleted, that's something Lambda handles for you in the background.

like image 175
Maurice Avatar answered Feb 09 '26 14:02

Maurice



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!