aws / serverless-java-container

A Java wrapper to run Spring, Spring Boot, Jersey, and other apps inside AWS Lambda.
https://aws.amazon.com/serverless/
Apache License 2.0
1.5k stars 561 forks source link

[RFC] Support for native-image compilation #335

Closed sapessi closed 10 months ago

sapessi commented 4 years ago

Scenario

As the Spring framework adds support for GraalVM's native image tool, we should explore making it easy to compile SpringBoot applications and execute them as a custom runtime in AWS Lambda

This issue serves as a tracker for this project to make design decisions and collect feedback on the implementation

urmichm commented 1 year ago

Totally support this issue! @sapessi if you are interested I might help to create a project for GraalVM and Springboot with MVC controllers.

deki commented 1 year ago

Please check https://catalog.workshops.aws/java-on-aws-lambda/en-US/01-migration/03-alternatives/graalvm for an example. Will get that added to the docs.

fsomme2s commented 1 year ago

Now that we have Spring Boot 3 Support (see #487, which I tested and it works really nice so far! Awesome work!), maybe it's time to revive this issue?

I can use Spring Boot Native to create Custom Images; I can use the serverless-java-container lib to "bridge" Api Gateway Events into Spring ... But I can't get my head around how to combine these two.

Is this already possible? Or would it need some kind of special Custom Runtime "Event Loop" thing, similar to this ? (That would call SpringBootLambdaContainerHandler.proxy(...) when it polls a new event)

lenis0012 commented 1 year ago

@fsomme2s One critical difference between native image support for spring via "Spring Cloud Function" and aws-serverless-java-container is the runtime.

You can use spring boot 3 to compile to a native image, create a custom runtime and use that to make a simple function with the code already provided by Spring Cloud Function. But this will not correctly proxy API Gateway requests.

The reason why aws-serverless-container does not work out of the box is that it assumes to be running in a java runtime. When this happens, the invocation is handled by the AWS Platform which executes a java method somewhere. This does not work with a custom runtime where you have to listen for invocations by calling out to an API.

The SAM template in the spring boot 3 example uses the following for runtime invocation:

Handler: com.amazonaws.serverless.sample.springboot3.StreamLambdaHandler::handleRequest

From my understanding, this is the part that has to be changed.

What does work is the custom runtime integration that is provided by Spring Cloud Function's AWS adapter. (spring-cloud-starter-function-adapter-aws). Similar logic would have to be integrated into this project to make it work with native compilation. And a different SAM template is required that specifies a custom runtime. I might be missing something but from my understanding that's what has to happen.

I don't think it would be too difficult to create a runtime inializer inspired by CustomRuntimeInitializer and CustomRuntimeEventLoop.

However, it seems that it is quite possible Spring Cloud Function already supports this since very recently. I haven't confirmed but there is https://github.com/spring-cloud/spring-cloud-function/blob/main/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/main/java/org/springframework/cloud/function/adapter/aws/web/WebProxyInvoker.java

babaksd commented 11 months ago

@deki I'm having trouble integrating aws-serverless-java-container-springboot3 with Graalvm and a custom lambda runtime/handler. Has anyone successfully managed to do this? Is the anyway to make it work?

deki commented 11 months ago

Hey @babaksd, we are aware. @olegz is currently working on that and we'll include it in the next release (current plan is to have that end of this month).

babaksd commented 10 months ago

@deki That's great, Thank you for the amazing work!

deki commented 10 months ago

739 has been merged and will be part of the next release. We've also added a sample: https://github.com/aws/serverless-java-container/tree/main/samples/springboot3/pet-store-native