Can I store a temp file on AWS Lambda Function?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
AWS Lambda is a serverless computing service provided by Amazon Web Services that allows developers to run code without provisioning or managing servers, with billing based solely on compute time consumed. One common inquiry amongst developers using AWS Lambda is whether they can store temporary files during the execution of a Lambda function. This article delves into the technical aspects of storing temporary files on AWS Lambda, the use cases, limitations, and best practices for managing these files.
Understanding AWS Lambda's Execution Environment
Each Lambda function runs inside its own isolated environment, which includes the allocated memory, network settings, and specific runtime. Lambda handles the provisioning, scaling, and maintenance of the execution environment, freeing developers from infrastructure concerns.
Access to `/tmp` Directory
- AWS Lambda provides a temporary file storage system accessible via the `/tmp` directory.
- This directory exists within the execution environment and offers up to 512 MB storage capacity.
- Files stored in `/tmp` are persisted only during the lifetime of a single invocation and are cleaned up automatically when an invocation completes.
Example: Storing and Reading a Temp File
Below is a simple example showcasing how to write to and read from a temporary file within an AWS Lambda function written in Python:
- File Processing: Transforming or processing files (e.g., resizing images) temporarily before saving the result to a persistent storage service like S3.
- Data Caching: Storing data that needs to be quickly accessed multiple times during a single invocation.
- Batch Job State Management: Keeping intermediate state information or partial results for batch processing invoked multiple times with different data packets.
- Size Restriction: Limited to 512 MB, so not suitable for large files.
- Life Span: Temporary data is ephemeral, accessible only within the lifecycle of a single invocation.
- Cold Start Overheads: Initializing data to the `/tmp` directory can contribute to latency during cold starts.
- Minimize Usage: Use temporary storage sparingly and only when necessary due to space and lifecycle constraints.
- Clean Up: Explicitly delete temporary files after use to avoid hitting the storage limit within a long-running invocation.
- Security Considerations: Do not store sensitive data in the `/tmp` directory without adequate encryption, as it shares the server's broader environment.
Related reading
- Can I test AWS Glue code locally?
- Can I update an existing Amazon S3 object?
- Can I use AWS Certificate Manager certificates for API Gateway with Custom Domain Name
- Can I use boto3 anonymously?
- Can I use local SSDs on GKE for fast, temporary storage within a pod?
- Can I use PouchDB with Dynamo on the back instead of Couch?
- Can I write to an AWS MSK Kafka cluster from a Lambda function?
- Can Kubernetes be used like Docker Compose?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.