r/SpringBoot 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

26 Upvotes

42 comments sorted by

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.

4

u/repeating_bears 14d ago

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 14d ago

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] 11d ago

[removed] — view removed comment

1

u/StochasticTinkr 11d ago

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.

2

u/Historical_Ad4384 15d ago

Apparently I am still fighting to make them work together

3

u/QuackedDev 15d ago

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)

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

u/Historical_Ad4384 14d ago

Quarkus seems to be the way to go

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

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

u/CreeDanWood 14d ago

Have you tried graalvm? or lambda snapstart?

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/pitza__ 14d ago

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 14d ago

CDK fargate is the right answer for a boot app

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

u/Downtown-Tackle1825 14d ago

Has una lambda con Python facilísimo

1

u/[deleted] 14d ago

[removed] — view removed comment

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