A developer is implementing an AWS Lambda function in Node.js to process events. The developer wants to log the count of processed items for each individual invocation. The function code is structured as follows:
javascript
let totalItems = 0;
exports.handler = async (event) => {
totalItems += event.items.length;
console.log(`Processed ${totalItems} items.`);
return { statusCode: 200 };
};
During testing, the developer observes that when the function is invoked repeatedly in short succession, the logged count accumulates across requests rather than reflecting only the items processed in the current invocation.
Which of the following modifications should the developer make to ensure the function correctly logs only the items processed in the current invocation?
- AConfigure the Lambda function's reserved concurrency to 1 to force the execution context to tear down and recreate after each invocation.
- Declare the totalItems variable inside the handler function so that it is re-initialized on each invocation.Answer
- CAssociate the Lambda function with a private VPC subnet without a NAT Gateway to prevent execution contexts from sharing global variables.
- DIncrease the visibility timeout of the trigger Amazon SQS queue to exceed the function execution time, forcing the Lambda service to clear the execution context.