问题
I'm trying to create and deploy AWS Lambda function in Java using Micronaut, GraalVM, and Docker. I'm following this tutorial to create AWS lambda with GraalVM runtime.
I've created java project using micronaut
command $ mn create-app my-app --features aws-api-gateway-graal
. And then without any change in code or config files, try to build a Docker image with GraalVM native image. In the step of docker build when graalvm native image is building I get these warnings:
Warning: Aborting stand-alone image build. No instances are allowed in the image heap for a class that is initialized or reinitialized at image runtime com.amazonaws.serverless.proxy.model.ContainerConfig. Try marking this class for build-time initialization with --initialize-at-build-time=com.amazonaws.serverless.proxy.model.ContainerConfig
Detailed message: Trace: field io.micronaut.function.aws.proxy.AbstractLambdaContainerHandler.config
Warning: Use -H:+ReportExceptionStackTraces to print stacktrace of underlying exception*
Warning: Image 'server' is a fallback image that requires a JDK for execution (use --no-fallback to suppress fallback image generation).
After build I export docker image with bootstrap file to function.zip file as it's written in tutorial. Then I upload function.zip file to my created AWS lambda function and when I try to test function I get this error:
{ "errorType": "Runtime.ExitError", "errorMessage": "RequestId: 888854d7-0e0e-42b4-a138-9a003c3455e1 Error: Runtime exited with error: exit status 1" }
START RequestId: 888854d7-0e0e-42b4-a138-9a003c3455e1 Version: $LATEST
Error: No bin/java and no environment variable JAVA_HOME
END RequestId: 888854d7-0e0e-42b4-a138-9a003c3455e1
REPORT RequestId: 888854d7-0e0e-42b4-a138-9a003c3455e1 Duration: 415.09 ms Billed Duration: 500 ms Memory Size: 128 MB Max Memory Used: 16 MB
RequestId: 888854d7-0e0e-42b4-a138-9a003c3455e1 Error: Runtime exited with error: exit status 1
Runtime.ExitError
I don't know what is wrong. I haven't found any solution for this problem yet.
回答1:
This classes invokes static method defaultConfig
of the ContainerConfig
class.
io.micronaut.function.aws.proxy.AbstractLambdaContainerHandler
com.amazonaws.serverless.proxy.internal.LambdaContainerHandler
To resolve the issue we need to to initialize that classes at run-time instead of compile time.
I'm totally new to GraalVM and for now can't explain what the reason of that.
My native-image
configurations.
native-image
--delay-class-initialization-to-runtime=io.micronaut.function.aws.proxy.AbstractLambdaContainerHandler,com.amazonaws.serverless.proxy.internal.LambdaContainerHandler
--no-fallback
--no-server
-cp build/libs/yourJarName.jar
Resources: https://medium.com/graalvm/understanding-class-initialization-in-graalvm-native-image-generation-d765b7e4d6ed
来源:https://stackoverflow.com/questions/56735749/problem-with-creating-aws-lambda-function-using-micronaut-and-graalvm