How to view size of Lambda /tmp storage size?

Viewed 2813

How is it possbile to see the used sizes of the storage that is used by a AWS Lamdba function?

For example, like this it is possibe to check if a file exists:

import os
os.path.isfile('/tmp/' + filename)

However, I need to know when when the 512 MB limit is reached.

1 Answers

Since the Lambda Function is just running on a container in a linux environment, we can use the OS to tell us generally how much space we have left on the temporary filesystem.

from subprocess import check_output
out = str(check_output(["df", '-k']))
print(out)
result = re.search(r"(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\%\s+\/tmp", out)
size, used, available, used_pct = (int(g) for g in result.groups())  

print(size, used, available, used_pct)  # 538424 440 526148 1

Here's what's happening here. First We use the subprocess.check_output function to run the command: df -k which displays a string about information on the disk space usage in KB. Here's a sample of that output on a lambda function:

Filesystem     1K-blocks    Used Available Use% Mounted on\n/dev/root        6127168 4978732   1132052  82% /\n/dev/vdb         1965904   45296   1904224   3% /dev\n/dev/loop0        538424     440    526148   1% /tmp\n

You can see that at the end of the string is there the /tmp partition is located. So we take the output and search it with the regex pattern: (\d+)\s+(\d+)\s+(\d+)\s+(\d+)\%\s+\/tmp which separates out the relevant fields into 4 groups which we can set to their own variables that represent the size: total partition size (in KB), used: The amount of space currently being used (in KB), available: The amount of space still available on the partition (in KB), and used_pct: The percentage of the partition space that is being used.

This is a pretty simple way of getting at the space available, I would not use this if precise measurements are needed or for guessing if a new file can be written to the disk. But if you're just logging it on execution for debugging, it should be perfectly fine.

Related