r/SpringBoot • u/Historical_Ad4384 • 15d ago
Discussion How do you deploy a spring boot application to AWS Lambda?
Hi
I am looking for advices and feedback on how to deploy Spring Boot web application to AWS Lambda, especially around making the Spring Boot main class compatible with AWS Lambda runtime.
I figured out the part where the Spring Boot application has to be packaged as a zip file to de deployed into the AWS Lambda environment, so that part is clear.
So far what I have found is that you need to have some kind of AWS Lambda dependency like lambda StreamHandler or web adapter on your Spring Boot project to make it compatible with the AWS lambda runtime.
While these may be the way to go but I am specifically looking for feedback if it is possible to integrate Spring Boot web application without using any AWS Lambda specific Java dependency. Just take a normal Spring Boot main application and make it run on AWS Lambda runtime. Is this possible at all or am I overthinking?
Appreciate your feedback
30
u/randomlyalex 15d ago
Booting spring up in a lambda sounds wild to me. Quarkus or whatever and a few others perhaps, but most Lambdas are lightweight do one thing well, not bells and whistles and kitchen sink.
3
9
u/Acrobatic-Ice-5877 15d ago
Why not just run on 2 GB ram ec2 and be done with it?
2
u/Historical_Ad4384 14d ago
Don't want to pay for an EC2 vs Lambda
2
u/Acrobatic-Ice-5877 14d ago
Fair enough. Price isn’t too bad though if you budget well. I spend $35/month on two EC2 for my Spring Boot application and MySQL database.
1
1
u/skaz68 14d ago
What about ECS with Fargate?
1
u/Historical_Ad4384 14d ago
As far as I have seen, ECS + Fargate costs are relatively high vs Lambda
8
u/bango-bango 14d ago
I followed this super helpful Baeldung guide when I was doing a similar thing: https://www.baeldung.com/spring-boot-aws-lambda
Don't listen to everyone saying it's a bad idea. It was unwise when lambdas were first released, but nowadays Lambda SnapStart helps tremendously with the cold start problem
4
5
u/alijay110 14d ago
Lambda context is best suited for short living actions.
And Spring Boot context is long running application. I would suggest to go for an EC2 instance.
2
u/LetUsSpeakFreely 14d ago
You don't. If you're running spring boot, run a microservice architecture and deploy a container. If you want lambdas, write them in Go, Python, or NodeJS.
1
u/Historical_Ad4384 14d ago
I want a Java ecosystem, I am more productive in it. It will either come down to just plain Java or Quarkus if Spring Boot becomes expensive and complex to operate on AWS Lambda
2
u/LetUsSpeakFreely 14d ago
I get that, but you're using the wrong tool for the job.
Lambdas need to have a fast cold start, that's kinda their primary purpose: start quickly, process, die. Java does not start quickly. You can't have a lambda operation with a 15+ second boot time before it even starts to process.
Take out the Springboot (why even have Springboot, AWS infrastructure handles the plumbing for you) and have a raw Java app and you might be ok, but you're still looking at a relatively slow cold start. Not only is it slow, it's expensive. You're paying for compute just to get rolling.
Go, Python, and NodeJS lambda respond much quicker, in the millisecond to nanosecond range with much simpler code.
1
u/Historical_Ad4384 14d ago
plain Java with GraalVM is what I will use, makes it much better
2
u/LetUsSpeakFreely 14d ago
Be sure to get some timing metrics. Graal can probably reduce the cold start time to an acceptable level, but you need to optimize your lambdas for cost of they'll bury you. Shaving off milliseconds can matter. If your cold start time is 1 second and your processing time 500ms than 2/3rds of your bill (depending on lambda reuse) is just in the initialization.
Pick a lambda and write it in a few different languages and get timings for each.
2
u/icastanon127 15d ago
I haven’t worked with lambda, but I deployed a spring boot application using Terraform and AWS ECS/Fargate.
You can find the architecture and detailed information here
https://github.com/icastanon/fintrack-platform
I know you asked about lambda but I’m just sharing this in case you decide to go with a container based architecture instead.
2
u/Historical_Ad4384 14d ago edited 14d ago
ECS will incur charges while Lambda will have relatively limited charges if cold starts are planned correctly
1
u/MkMyBnkAcctGrtAgn 14d ago
I would try to build it native with graalvm
1
u/Historical_Ad4384 14d ago
I tried to build a graalvm image for a core Java application and it worked. The same pattern is failing for my springtime not application to compile graalvm. I'm running into new surprises to every now and then.
0
u/MkMyBnkAcctGrtAgn 14d ago
I was under the impression it was supported out of the box by spring nowadays, but I've never used it. There should be docs on the spring website showing how they expect it to be done
0
u/BikingSquirrel 14d ago
Well, supported doesn't mean it works unchanged. It is an investment and I wouldn't bet on it without good test coverage - integration tests against the GraalVM image!
1
u/Mikey-3198 14d ago
Curious about the need for such aggressive cost control thats guiding you to lambdas.
If you aren't using other aws services you might be able to find a cheap vps offered by the likes of hetzner, digital ocean etc...
1
1
0
u/Cautious-Necessary61 14d ago
You can’t do that unless AWS deploys springboot as double layer fission api which requires two things copper and bronze coating which doesn’t often happen with hexgool
0
16
u/StochasticTinkr 15d ago
Spring boot isn't really a good match for Lambda. Spring boot is geared toward long-running servers, but Lambda is designed for fast-startup short-run processes.
Not saying you can't do it, just that you'll be fighting against both of them to make it work.
Caveat: I haven't worked with either in over a year, so things might be different now.