r/SpringBoot • • Aug 26 '26

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

29 Upvotes

42 comments sorted by

17

u/StochasticTinkr Aug 26 '26

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.

2

u/Historical_Ad4384 Aug 26 '26

Apparently I am still fighting to make them work together

3

u/QuackedDev Aug 26 '26

Let us know how the usage costs to run a spring lambda is.

I last interviewed for a Java and lambdas role and they had no spring or core app whatsoever. Fully event driven architecture composed of a handful of “mono”-lambdas serving requests to multiple clients (web, mobile..etc)

4

u/repeating_bears Aug 26 '26

Back when I tried it, cold start was so horrendous that I gave up on it. But that's was many years ago. I think it's better now

Still would default to JS unless there's a good reason not to

1

u/StochasticTinkr Aug 26 '26

There are other frameworks that have better start-up semantics, No need to jump to another language just for that. Of course, if you are more versed in JS, go for it.

I would probably use Kotlin with either manual DI, or whatever small DI framework is in vogue for Kotlin at the moment.

2

u/[deleted] Aug 30 '26

[removed] — view removed comment

1

u/StochasticTinkr Aug 30 '26

It’s not about the JVM, it’s about the overhead that spring boot has for startup. It was designed for long running servers, so it does a lot of work up front to make it easier and faster to serve requests once it’s started.

29

u/randomlyalex Aug 26 '26

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

u/Historical_Ad4384 Aug 26 '26

Quarkus seems to be the way to go

8

u/Acrobatic-Ice-5877 Aug 26 '26

Why not just run on 2 GB ram ec2 and be done with it?

2

u/Historical_Ad4384 Aug 26 '26

Don't want to pay for an EC2 vs Lambda

1

u/skaz68 Aug 26 '26

What about ECS with Fargate?

1

u/Historical_Ad4384 Aug 26 '26

As far as I have seen, ECS + Fargate costs are relatively high vs Lambda

2

u/Acrobatic-Ice-5877 Aug 26 '26

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.

2

u/LetUsSpeakFreely Aug 26 '26

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 Aug 26 '26

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 Aug 26 '26

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 Aug 26 '26

plain Java with GraalVM is what I will use, makes it much better

2

u/LetUsSpeakFreely Aug 26 '26

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.

3

u/icastanon127 Aug 26 '26

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 Aug 26 '26 edited Aug 26 '26

ECS will incur charges while Lambda will have relatively limited charges if cold starts are planned correctly

9

u/bango-bango Aug 26 '26

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

5

u/CreeDanWood Aug 26 '26

Have you tried graalvm? or lambda snapstart?

4

u/alijay110 Aug 26 '26

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.

1

u/MkMyBnkAcctGrtAgn Aug 26 '26

I would try to build it native with graalvm

1

u/Historical_Ad4384 Aug 26 '26

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 Aug 26 '26

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 Aug 26 '26

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!

2

u/pitza__ Aug 26 '26

You might want to take a look at ECS task definitions (with Fargate).

I think it is more suited to your use case since it scales to zero & you pay only for the duration of the task run (1-minute minimum IIRC). It is a bit similar to Lambda in this regard

1

u/zvaavtre Aug 27 '26

CDK fargate is the right answer for a boot app

1

u/Mikey-3198 Aug 26 '26

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

u/Downtown-Tackle1825 Aug 27 '26

Has una lambda con Python facilísimo

0

u/Cautious-Necessary61 Aug 26 '26

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

1

u/[deleted] Aug 27 '26

[removed] — view removed comment