Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Does an AWS Lambda keep field values throughout its invocation?

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"?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What would happen if i was static?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading