Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In AWS Lambda the constructor is invoked for every invocation #27775

Open
tarsij opened this issue Sep 7, 2022 · 3 comments
Open

In AWS Lambda the constructor is invoked for every invocation #27775

tarsij opened this issue Sep 7, 2022 · 3 comments
Labels
area/amazon-lambda kind/bug Something isn't working

Comments

@tarsij
Copy link

tarsij commented Sep 7, 2022

Describe the bug

When creating a native quarkus aws lambda, the constructor is invoked for every handler invocation.
That is a problem as the subsequent handler calls won't benefit of the pre-initialised instance.

In the standard lambda implementations the constructor is used to do pre-initialisations like loading some resources or opening connections. The subsequent calls for a warm instance would then just invoke the handler.

Expected behavior

The constructor should be invoked only on cold start so heavy resources/connections can be pre-initialised and be reused across multiple handler invocations

Actual behavior

the constructor is invoked for every handler invocation so the lambda cannot benefit of the pre-initialisation done in the constructor

How to Reproduce?

  1. create a new quarkus lambda project
  2. add a logging statement into the handler's constructor.
  3. test the handler in aws => in the cloudwatch logs you will see that the constructor is invoked for every lambda call (even for the warm ones)

Output of uname -a or ver

Linux 5.15.0-46-generic #49~20.04.1-Ubuntu SMP Thu Aug 4 19:15:44 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

Output of java -version

openjdk version "11.0.16" 2022-07-19

GraalVM version (if different from Java)

GraalVM 22.2.0 Java 11 CE (quay.io/quarkus/ubi-quarkus-native-image:22.2-java11)

Quarkus version or git rev

2.12.0.Final

Build tool (ie. output of mvnw --version or gradlew --version)

Apache Maven 3.6.3

Additional information

No response

@tarsij tarsij added the kind/bug Something isn't working label Sep 7, 2022
@quarkus-bot
Copy link

quarkus-bot bot commented Sep 7, 2022

/cc @matejvasek, @patriot1burke

@patriot1burke
Copy link
Contributor

Put @ApplicationScoped on your lambda class

@tarsij
Copy link
Author

tarsij commented Sep 7, 2022

Hi @patriot1burke,

Thanks a mill for your answer.
In this case there's another small bug.
On the first run the Constructor is invoked twice.
I've left a logging in the constructor to test the behaviour. I see the log appearing twice (both in my tests and on aws as well).

For subsequent calls it is not invoked anymore, so that is indeed sorted.

One more note.
If this is the expected solution, then it would be nice to add this to the quarkus-amazon-lambda-archetype and to the documentation (e.g. here https://quarkus.io/guides/amazon-lambda) as this is the default expected behaviour for lambdas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/amazon-lambda kind/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants