'AWS Lambda storage issue

I got the following error when I ran python code on AWS lambda.

"errorMessage": "[Errno 17] File exists: '/tmp/testdir/'"

It occurs on the line os.makedirs('/tmp/testdir/').

Before I didn't have this kind of error. Does this mean Lambda function preserves tmp directory?

Should I clean tmp directory everytime?



Solution 1:[1]

Yes, the content of the Lambda diskspace at /tmp may be available to subsequent Lambda invocations (these are so-called 'warm start' invocations).

See Understanding Container Reuse.

You can clean up the /tmp folder before existing your Lambda function or you could use the following code to safely create the folder, ignoring the fact that it may already exist:

os.makedirs('/tmp/testdir/', exist_ok=True)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1