quarkus-amazon-lambda @Inject still doesn't work in JVM mode

Created on 28 May 2020  路  20Comments  路  Source: quarkusio/quarkus

Describe the bug
What is happening on #3354 is still happening: after deploying the JVM project, I get NPE in CloudWatch because the injected stuff is null.

Expected behavior
I'd like to have the services injected properly, not null...

Actual behavior
I follow all the indications in the guide https://quarkus.io/guides/amazon-lambda and the @ Inject members are null.

To Reproduce
Steps to reproduce the behavior:
I changed the inputs because my existing lambda reacts to a CloudWatch event, didn't want to fiddle with working AWS settings.

  1. generate a new project with the quarkus-amazon-lambda-archetype
  2. add to the ProcessingService a new function
  public String test() {
    return "done";
  }
  1. modify the TestLambda to implements RequestHandler<Object, Object>
  2. modify its handleRequest to
  @Override
  public Object handleRequest(Object input, Context context) {
    return service.test();
  }
  1. deploy to AWS Lambda and lo, NPE

Environment (please complete the following information):

  • Output of uname -a or ver: Windows 10
  • Output of java -version: 11
  • GraalVM version (if different from Java):
  • Quarkus version or git rev: 1.4.2.Final
  • Build tool (ie. output of mvnw --version or gradlew --version): Maven 3.6.3

Additional context
I must also say that I don't really understand what their mentioned "correct way" is in #3354, as it's not detailed, so the problem might be in front of the keyboard.

kinquestion

All 20 comments

PS: I can't use native mode because I'm on Windows 10...

Created also a test project with all defaults as generated by the archetype. Generate the runner jar (not native), upload it as lambda, test in AWS console with a sample json and watch it crash.
https://github.com/sorin-costea/quarkus-inject

cc @oztimpower

Will take a look. Give me a few days.

@sorin-costea The sample project is working for me in all scenarios, via the Unit Test, via SAM Local and AWS.

If however I omit to pass the payload, or the JSON does not have the "name" property, then yes a NPE would be thrown from the ProcessingService:11 as opposed to an Inject failure. Injection was working.

I'll try your manual code as mentioned in the initial report, however I would appreciate if you could share your stack track from SAM Local or from AWS. How did you run and test this?

FYI #3354 is unrelated and the code has changed significantly since then.

Ok, I've modified the code as described above with your ProcessingService::test() function, and that also works with SAM Local and AWS.

Please share any stack traces, because the provided project and the manual explanation do not show a reproducer.

  1. I added an extra log statement to print the injected service, please pull the change
  2. mvn install (using Java 11 in either 8 or 11 compatibility)
  3. upload the jar to aws lambda
  4. lambda configuration:
  5. handler org.sorincos.TestLambda::handleRequest
  6. runtime Java 8 or 11 depending what I used above
  7. created test event in Lambda
{
  "name": "jim",
  "greeting": "hi"
}
  1. test execution failed with:
{
  "errorMessage": "java.lang.NullPointerException",
  "errorType": "java.lang.NullPointerException",
  "stackTrace": [
    "org.sorincos.TestLambda.handleRequest(TestLambda.java:25)",
    "java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)",
    "java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)",
    "java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)",
    "java.base/java.lang.reflect.Method.invoke(Unknown Source)"
  ]
}

and log output (service is null):

START RequestId: e11881f7-9cc6-4d27-a371-f53394c45a10 Version: $LATEST
May 29, 2020 2:47:14 PM org.sorincos.TestLambda handleRequest
INFO: service is: null
May 29, 2020 2:47:14 PM org.sorincos.TestLambda handleRequest
WARNING: WARN
May 29, 2020 2:47:14 PM org.sorincos.TestLambda handleRequest
SEVERE: SEV
java.lang.NullPointerException: java.lang.NullPointerException
java.lang.NullPointerException
    at org.sorincos.TestLambda.handleRequest(TestLambda.java:25)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.base/java.lang.reflect.Method.invoke(Unknown Source)

END RequestId: e11881f7-9cc6-4d27-a371-f53394c45a10
REPORT RequestId: e11881f7-9cc6-4d27-a371-f53394c45a10  Duration: 535.28 ms Billed Duration: 600 ms Memory Size: 512 MB Max Memory Used: 96 MB  Init Duration: 379.95 ms

Still works fine for me.

Local:

sam local invoke --template target/sam.jvm.yaml --event payload.json

__  ____  __  _____   ___  __ ____  ______ 
 --/ __ \/ / / / _ | / _ \/ //_/ / / / __/ 
 -/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \   
--\___\_\____/_/ |_/_/|_/_/|_|\____/___/   
2020-05-30 04:01:23,178 DEBUG [org.jbo.logging] (main) Logging Provider: org.jboss.logging.JBossLogManagerProvider
2020-05-30 04:01:24,800 DEBUG [org.jbo.threads] (main) JBoss Threads version 3.1.1.Final
START RequestId: 655fbfd0-9bc5-1433-bc2e-9e3d7686872e Version: $LATEST
2020-05-30 04:01:25,092 FINE  [org.sor.TestLambda] (main) FINE
2020-05-30 04:01:25,093 INFO  [org.sor.TestLambda] (main) service is: org.sorincos.ProcessingService@24c4ddae
2020-05-30 04:01:25,094 WARNING [org.sor.TestLambda] (main) WARN
2020-05-30 04:01:25,094 SEVERE [org.sor.TestLambda] (main) SEV
END RequestId: 655fbfd0-9bc5-1433-bc2e-9e3d7686872e
REPORT RequestId: 655fbfd0-9bc5-1433-bc2e-9e3d7686872e  Init Duration: 2913.67 ms       Duration: 52.21 ms      Billed Duration: 100 ms Memory Size: 256 MB     Max Memory Used: 87 MB      

{"result":"hello Bill","requestId":"655fbfd0-9bc5-1433-bc2e-9e3d7686872e"}

On AWS:

sh target/manage.sh invoke

Invoking function
++ aws lambda invoke response.txt --function-name Qtest --payload file://payload.json --log-type Tail --query LogResult --output text
++ base64 -d

START RequestId: acc56a45-7c7a-4834-86a2-1e8c75745f7b Version: $LATEST
2020-05-30 04:04:15,922 FINE  [org.sor.TestLambda] (main) FINE
2020-05-30 04:04:15,922 INFO  [org.sor.TestLambda] (main) service is: org.sorincos.ProcessingService@1f81aa00
2020-05-30 04:04:15,923 WARNING [org.sor.TestLambda] (main) WARN
2020-05-30 04:04:15,923 SEVERE [org.sor.TestLambda] (main) SEV
END RequestId: acc56a45-7c7a-4834-86a2-1e8c75745f7b
REPORT RequestId: acc56a45-7c7a-4834-86a2-1e8c75745f7b  Duration: 155.17 ms     Billed Duration: 200 ms Memory Size: 256 MB     Max Memory Used: 113 MB Init Duration: 1620.50 ms   

I note you listed the handler explicitly, is there a reason for this? If you look at the templates I am invoking, the handler is fixed as Quarkus; your handler is determined at build time via the Named annotation, and corresponding property in the application.properties.

I suspect this is your issue. If you use the SAM Local, which can run on Windows, you will see the definition, and also the templated target/manage.sh.

This is documented in the Quarkus Amazon Lambda guide

You have listed handler as:
handler: org.sorincos.TestLambda::handleRequest

when it is fixed as in the Quarkus templates:

io.quarkus.amazon.lambda.runtime.QuarkusStreamHandler::handleRequest

@geoand Please close, discovered the issue as mentioned above.

@oztimpower I think you also have the rights to close issues, no?

Unfortunately, no.

Oh, okay

@oztimpower it might be obvious to you but I see no reference in the documentation that I should enter that handler in AWS, even if I was overriding it. So instead of closing the issue blindly can you please tell me what I should enter in the lambda handler? io.quarkus.amazon.lambda.runtime.QuarkusStreamHandler::handleRequest throws ClassNotFoundException, same with io.quarkus.amazon.lambda.runtime.QuarkusHandler::handleRequest and same with test.

@geoand it's still not working.

I'll let you and @oztimpower figure it out. If you both think it should be reopened, I'll gladly reopen it

Hi @sorin-costea, look in the target/sam.jvm.yaml and the target/manage.sh for guidance.

I'm happy to get on a video call and walk through with you.

Can I suggest that you download SAM for Windows, and try the command I have above?

Let's take this offline, you can reach me on [email protected]

Using the SAM Template you can deploy, via sam deploy if needs be.

The handler is defined below, and the code to upload is function.zip

  AWSTemplateFormatVersion: '2010-09-09'
  Transform: AWS::Serverless-2016-10-31
  Description: AWS Serverless Quarkus - qtest-1.0-SNAPSHOT
  Globals:
    Api:
      EndpointConfiguration: REGIONAL
      BinaryMediaTypes:
        - "*/*"

  Resources:
    Qtest:
      Type: AWS::Serverless::Function
      Properties:
        Handler: io.quarkus.amazon.lambda.runtime.QuarkusStreamHandler::handleRequest
        Runtime: java11
        CodeUri: function.zip
        MemorySize: 256
        Timeout: 15
        Policies: AWSLambdaBasicExecutionRole

For posterity: I was mistakenly uploading the runner.jar instead of the correct function.zip. That was all the difference.

Was this page helpful?
0 / 5 - 0 ratings