Temporary storage during AWS Lambda runtime – Python

During the runtime of your lambda function, you may want to save some temporary files, to be accessed later during the function execution. Luckily, AWS provides 512 MB of file system (‘/tmp’ folder) that can be used by us for this purpose. It is equivalent to your local hard disk, providing fast throughput. You can perform operations like makedirs, listdir, etc. on this folder using the ‘os’ package, just like you’d perform on your local hard disk.

The /tmp storage is intended for use within the single execution of your function. If you have a scheduled lambda running every hour, you CAN’T expect to store files in the /tmp folder in one execution and access them in the next. You may want to explore other options (like S3 storage), for such an application.

A sample snippet is given below on how to use the /tmp folder for various operations, including making a directory, saving a file, listing files in a directory, and fetching files. Refer to the comments in the snippet for details.

import pandas as pd
import os

def handler(event='', context=''):

    #Change directory to /tmp folder
    os.chdir('/tmp')    #This is important
    #Make a directory
    if not os.path.exists(os.path.join('mydir')):
    #Save a CSV
    df = pd.read_csv(CSV_URL)
    filename = 'myfile.csv'
    save_path = os.path.join(os.getcwd(), 'mydir', filename)
    #Save HTML
    filename = 'test.html'
    save_path = os.path.join(os.getcwd(), 'mydir', filename)
    f = open(save_path, 'w')
    html_content = """<!DOCTYPE HTML><html><head>
      <title>Demo HTML</title>
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <h3>Demo HTML</h3>
      <form action="/get">
        Name: <input type="text" name="name">
        Address: <input type="text" name="address">
        <input type="submit" value="Submit">

    #Perform other operations
    #List files
    for file in os.listdir(os.path.join(os.getcwd(), 'mydir')):
        file_path = os.path.join(os.getcwd(), 'mydir', file)
        #Read CSV
            df_fetched = pd.read_csv(file_path)
            #Perform other operations on the fetched file

ound this post helpful? Then check out further posts related to AWS here. Also, if you are planning to become a certified AWS Solutions Architect, I’d recommend that you check out this course on Udemy. I took this course and found the lectures to be lucid, to-the-point, and fun. I hope they will help you as well.

Leave a comment

Your email address will not be published. Required fields are marked *