Imagine the following java code:
public class HandlerClass implements RequestHandler<DynamodbEvent, int> {
int i = 0;
public RequestHandler() {}
public int handleRequest(DynamodbEvent dynamodbEvent, Context context) {
i = i + 1;
System.out.println(i);
return i
}
}
Now the lambda is triggered from an DynamoDB event A, which would lead to an invocation of the lambda, which in turn would create an instance of the RequestHandler class and a call to handleRequest(). Shortly after event A was handled, but before the timeout of the lambda, event B fires.
Does event B reuse the same instance of the RequestHandler class and thus would print "2" or does event B receive its own instance and prints "1"?
What would happen if i was static?
No, the request handler is not shared across invocations, but objects initiated outside of the request handler is.
The answer is 1
For that reason it's always best practices to create your reusable components outside of the request handler to save you having to initiate them on each invocation.