Small tricks: Utilize Temporary Storage under Twilio Functions

September 04, 2019
Written by

Small Tricks: Utilize temporary storage under Twilio Functions

When working with Twilio Functions, you might need to create resources you'd like to store locally for one-off activities. For example, you might like to create a file with user-provided data and send it onto the next step in a flow based on your business needs.

In Functions, anything that you create on-the-fly gets stored in the temporary folder, which may not be obvious at first.

In this quick tutorial, I'll show you how to access and utilize the temporary storage under Functions for your purposes. Let's get started.

Temporary Storage in a Twilio Function

If you try to perform operations anywhere other than the temporary folder in the underlying OS filesystem you will see something like:

Error in Twilio Functions read-only file system

Below I show a Twilio Function writing to and retrieving from the relevant temp folder:

*  This Function shows you how to reach and utilise the temporary storage under the Function layer, mainly for single-invocation jobs
*  For example, on each invocation we can create a file based on user data and use it accordingly
*  IMPORTANT: Do NOT treat this storage as long term storage or for personal data that need to persist.
*  The contents get deleted whenever the associated container is brought down, so this function is useful for one time actions
var fs = require('fs');
var path = require('path');
var tmp_dir = require('os').tmpdir();

exports.handler = function(context, event, callback) {

   /*We create a text file and we put some data in it*/
   fs.writeFile(path.join(tmp_dir, 'test_file.txt'), 'Contents of created file in OS temp directory', function(err) {
       if (err) callback(err);

       /*We read the contents of the temporary directory to check that the file was created. For multiple files you can create a loop*/
       fs.readdir(tmp_dir, function(err, files) {
           if (err) callback(err);

           callback(null, "File created in temporary directory: " + files.join(", "));

That should be enough to get you started reading and writing to a temporary file in Functions. You should be able to adapt from there for your business needs.

Also: Find it on Twilio Labs GitHub and Serverless (Twilio CLI)

You can check out this Function template on the Twilio Labs GitHub page here. There you'll find even more useful function templates to get you started with Twilio Functions

If you wish to use it from the Twilio CLI, you can accomplish that by running the following command inside a project that was created with the Serverless Toolkit:

twilio serverless:init example --template=temp-storage

Evangelos Resvanis is a Solutions Engineer at Twilio. He is currently working on his favorite topics, Autopilot and Twilio Functions, exploring and expanding the possibilities offered by these tools. He can be reached at eresvanis [at] and on Twitter at @eresvanis.