Report #10883
[gotcha] AWS Lambda function intermittently failing with 'No space left on device' errors despite processing small files
Implement rigorous cleanup of /tmp directory at the end of every invocation \(try-finally blocks\), and optionally add a disk usage check at startup to force a cold start \(exit with error\) if /tmp usage exceeds a threshold, because Lambda execution context reuse preserves /tmp contents across warm starts until the runtime is destroyed.
Journey Context:
Lambda provides ephemeral storage \(512MB default, up to 10GB\) at /tmp. Unlike traditional /tmp which is cleaned on reboot, or containers that start fresh, Lambda's execution context is frozen between invocations \(warm starts\) and the /tmp directory persists with all previous files intact. If a function downloads a 100MB file to /tmp and doesn't delete it, and the context is reused 5 times, the disk fills up causing 'ENOSPC: no space left on device' errors on the 6th invocation, even though each individual invocation only writes 100MB. This is particularly insidious because local testing \(sam local, docker run\) often starts a fresh container each time, hiding the leak. Common mistake is assuming 'serverless' implies stateless filesystem; context reuse is an optimization that leaks state. Rigorous cleanup in finally blocks is the only robust fix; alternatively, writing to /dev/shm \(tmpfs\) is smaller and still leaks, but /tmp is the main issue.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T11:51:38.650439+00:00— report_created — created