Soru

Zorluk: OrtaDebugging Lambda Execution and Configuration Issues

A developer is troubleshooting an AWS Lambda function written in Python that processes transaction records. The function intermittently fails with a memory limit exceeded error after running successfully for several hours under continuous traffic. The developer reviews the code and notes that a helper class initializes an in-memory cache list in the global scope, outside the handler function, to store transaction IDs. Which of the following is the most likely cause of this issue and the correct resolution?

  1. The Lambda execution environment is being reused across multiple invocations, causing the global transaction ID cache list to grow indefinitely. To resolve this, the developer should initialize the cache list inside the handler function so it is cleared for each request.Cevap
  2. B
    The Lambda function's timeout is set too high, preventing the AWS runtime from automatically recycling the execution context and freeing up memory. To resolve this, the developer should decrease the timeout value to force context recycling.
  3. C
    The Lambda function is running in a private VPC subnet without an attached NAT Gateway, causing database connection attempts to hang and consume memory buffers. To resolve this, the developer should configure a NAT Gateway in the VPC's public subnet.
  4. D
    The function is hardcoding AWS IAM access keys within the global SDK initialization block, causing a memory leak during credential verification. To resolve this, the developer should configure the function to use the execution role's temporary credentials by removing the hardcoded keys.

Cevap

The Lambda execution environment is being reused across multiple invocations, causing the global transaction ID cache list to grow indefinitely. To resolve this, the developer should initialize the cache list inside the handler function so it is cleared for each request.
The correct option is correct because AWS Lambda reuses execution environments (warm starts) to improve latency. Objects declared in the global scope (outside the handler function) persist across these invocations. Since the transaction ID cache list is global and items are continuously added to it without being cleared, the memory footprint increases over time, eventually exceeding the configured memory limit. Initializing the list inside the handler ensures it is scoped to a single invocation and garbage collected afterward.

Adım Adım Çözüm

1
Analyze the symptom where the memory limit is exceeded only after running successfully for several hours under continuous traffic.
This indicates a progressive memory leak that accumulates across multiple invocations rather than a failure on the first execution.
AWS Lambda optimizes performance by keeping execution environments warm and reusing them for subsequent requests.
2
Inspect the code structure to locate the global variable declaration.
The in-memory cache list is declared in the global scope (outside the handler function).
State stored in global variables persists across invocations in reused execution environments.
3
Determine the resolution to prevent the list from growing across warm starts.
Declare the cache list inside the handler function or explicitly clear it at the beginning of each handler execution.
This guarantees that the list starts empty for every request, preventing memory accumulation.

Anahtar Kavram

Lambda Execution Context Reuse and Global State
Tahmini Süre:1m 30s
Bu soruyu puanla