Question

Difficulty: MediumDebugging Lambda Execution and Configuration Issues

An analytics application utilizes an AWS Lambda function to generate daily reports. The function downloads several source files to the local ephemeral storage (`/tmp`), merges them, and uploads the final report to Amazon S3. The function is configured with 512 MB512\text{ MB} of ephemeral storage. While initial test runs succeed, the function intermittently fails during peak hours with a `No space left on device` error, even though the source files for any single invocation never exceed 100 MB100\text{ MB} in total. What is the root cause of this failure, and how should the developer resolve it?

  1. The execution context is being reused across invocations, causing files from previous runs to persist in the `/tmp` directory. The developer should modify the code to delete temporary files from `/tmp` before the function execution completes.Answer
  2. B
    The developer assumed that the ephemeral storage is automatically wiped by Lambda between warm starts. The developer should increase the Lambda function's execution timeout limit to allow the platform's automatic background garbage collector to clean up the directory.
  3. C
    The Lambda function is deployed in a private VPC subnet without a NAT Gateway or VPC endpoint to access the Amazon S3 service. The developer should configure a NAT Gateway in the public subnet and update the route tables to allow the function to upload the files and free up storage.
  4. D
    The Lambda function's execution role trust policy is missing the `lambda.amazonaws.com` service principal, preventing the function from writing log streams to CloudWatch and causing local write buffers to overflow. The developer should modify the trust policy to allow the service principal.

Answer

The execution context is being reused across invocations, causing files from previous runs to persist in the `/tmp` directory. The developer should modify the code to delete temporary files from `/tmp` before the function execution completes.
The correct answer is the option explaining that the execution context is reused across invocations, which preserves files in the `/tmp` directory. To resolve the issue, the developer must explicitly delete the files before the invocation ends. AWS Lambda reuses execution environments to avoid cold start overhead. When an environment is reused, the state of the `/tmp` directory is maintained. If a function writes files to `/tmp` and does not clean them up, subsequent invocations in the same environment will find those files still present, leading to cumulative disk usage and eventual disk exhaustion.

Step-by-Step Solution

1
Analyze the error message and context.
The `No space left on device` error indicates that the local disk storage under `/tmp` is fully exhausted, despite the current invocation's files being well below the 512 MB512\text{ MB} limit.
This discrepancy suggests that storage space is leaking across separate execution events, pointing to a persistent state issue in the Lambda runtime environment.
2
Evaluate the behavior of Lambda ephemeral storage during execution context reuse.
AWS Lambda reuses execution environments for subsequent invocations (warm starts) to improve response times. Files stored in `/tmp` are preserved during this reuse.
Since the function download logic writes files but does not delete them, these files accumulate over successive invocations in a reused execution context until the disk space is depleted.
3
Determine the appropriate correction method.
Implement cleanup logic in the Lambda handler code to delete the files from `/tmp` right before the handler returns or finishes processing.
Explicitly deleting files ensures that each invocation starts or ends with a clean `/tmp` directory, preventing cumulative disk usage across reused execution environments.

Key Concept

AWS Lambda reuses its execution context (including the `/tmp` directory) across sequential invocations (warm starts). Developers must explicitly clean up any ephemeral storage files to prevent disk exhaustion over time.
Rate this question