A developer is troubleshooting an AWS Lambda function written in Node.js that processes contact form submissions. The function is configured to process one message at a time. The developer notices that when multiple forms are submitted in quick succession, the logs contain duplicate and combined messages from different users. The developer finds that a global array used to accumulate message parts is declared outside the Lambda handler function. Which of the following explains the cause of this issue and the correct resolution?
- The Lambda execution context is reused across sequential invocations, causing the global array to retain data from previous executions. The developer should declare and initialize the array inside the handler function.Answer
- BThe Lambda function is configured to run inside a public subnet in a VPC without a NAT Gateway, causing routing loops that duplicate the events. The developer should configure the function to run inside private subnets with a NAT Gateway.
- CThe AWS SDK client is reusing outdated session credentials stored in the global execution context. The developer should disable connection reuse by setting the AWS_SDK_LOAD_CONFIG environment variable to false.
- DAWS X-Ray active tracing is enabled, but the function fails to propagate the tracing context to the global scope, causing downstream requests to overlap. The developer should import and configure the X-Ray SDK at the beginning of the code.
Answer
The Lambda execution context is reused across sequential invocations, causing the global array to retain data from previous executions. The developer should declare and initialize the array inside the handler function.
The execution context is reused for sequential invocations, meaning any global variables defined outside the handler will persist. To prevent data from leaking or duplicating between runs, variables containing request-specific data must be defined inside the handler.
Step-by-Step Solution
Key Concept
Lambda execution context reuse and global state retention