A developer is testing a Node.js-based AWS Lambda function that processes incoming telemetry data. To analyze cold starts and container lifetime, the developer declares a variable outside the handler function to count the number of times the container has processed an event:
javascript
let localEventCount = 0;
exports.handler = async (event) => {
localEventCount++;
console.log("Events processed: " + localEventCount);
};
During a load test with high concurrent traffic, the developer notices that some log entries show sequential numbers (e.g., ), while other concurrent logs show a value of at the same timestamp.
Which of the following statements explains this behavior?
- AAWS Lambda automatically resets all variables declared outside the handler function after each synchronous execution to ensure execution isolation, but preserves them for asynchronous invocations.
- AWS Lambda spins up separate, concurrent execution environments to handle concurrent requests. The global variable is only persistent within a specific execution environment, meaning concurrent invocations will increment independent counters.Cevap
- CThe Lambda execution context is automatically terminated and initialized from scratch for every request unless Provisioned Concurrency is enabled, which synchronizes a single global variable state across all parallel containers.
- DThe developer must assign an IAM execution role with the sts:AssumeRole permission to allow the Lambda service to read and write variables in the global scope of the execution environment.
Cevap
AWS Lambda spins up separate, concurrent execution environments to handle concurrent requests. The global variable is only persistent within a specific execution environment, meaning concurrent invocations will increment independent counters.
The correct answer is correct because AWS Lambda initializes multiple independent execution environments (containers) to handle concurrent events. Since variables declared outside the handler function are local to the execution environment, they are only shared during sequential invocations on the same container. Under concurrent load, different containers execute concurrently, each maintaining its own independent instance of the global variable.
Adım Adım Çözüm
Anahtar Kavram
AWS Lambda Execution Context Reuse and Concurrency Dynamics