A developer is writing an AWS Lambda function in Node.js to calculate order discounts. The developer declares a helper array, `appliedDiscounts`, outside of the handler function to store the discount codes applied during execution. During testing, the developer notices that subsequent invocations of the function return incorrect discounts because they contain discount codes from previous invocations. Which of the following actions should the developer take to resolve this issue?
- AConfigure Provisioned Concurrency on the Lambda function to guarantee that each invocation runs in a newly created execution environment.
- BSet the `context.callbackWaitsForEmptyEventLoop` property to `false` to force the recycling of the execution environment after each invocation.
- Move the initialization of the helper array inside the handler function.Cevap
- DIncrease the visibility timeout of the incoming event source to automatically flush the global execution state between invocations.
Cevap
Move the initialization of the helper array inside the handler function.
Moving the initialization of the helper array inside the handler function ensures that the array is declared and reset to empty on every execution. Since AWS Lambda frequently reuses execution contexts to optimize performance, variables declared globally (outside the handler) persist across invocations. For mutable data structures like arrays, this persistence leads to state leakage and incorrect calculations on subsequent invocations.
Adım Adım Çözüm
Anahtar Kavram
AWS Lambda Execution Context Reuse and Variable Scoping
Tahmini Süre:1m 0s