Question

Difficulty: MediumServerless Development with AWS Lambda

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., 1,2,31, 2, 3), while other concurrent logs show a value of 11 at the same timestamp.

Which of the following statements explains this behavior?

  1. A
    AWS Lambda automatically resets all variables declared outside the handler function after each synchronous execution to ensure execution isolation, but preserves them for asynchronous invocations.
  2. 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.Answer
  3. C
    The 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.
  4. D
    The 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.

Answer

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.

Step-by-Step Solution

1
Analyze how variables declared outside the Lambda handler function are scoped and maintained.
Variables declared outside the handler are initialized when the Lambda execution environment is first created (cold start) and persist across subsequent invocations that reuse the same environment (warm starts).
This establishes the basic execution context lifecycle behavior of AWS Lambda.
2
Evaluate the behavior of the system under concurrent load.
To process concurrent requests, AWS Lambda spins up multiple independent execution environments. Each environment runs in isolation with its own memory space and copy of the global variable.
This explains why concurrent requests might see independent event counts starting from 11.
3
Differentiate between variable persistence within a single container and synchronization across containers.
AWS Lambda does not synchronize memory state or global variables across separate execution environments.
This identifies the root cause of the behavior: sequential requests on the same environment see incrementing counts, while parallel requests on new environments start at 11.

Key Concept

AWS Lambda Execution Context Reuse and Concurrency Dynamics
Rate this question