Move Your Spring Boot API to AWS Lambda and Reduce Costs

August 4, 2019

You can use your API 12 Million times per year and pay AWS $0

AWS Lambda

🧞 An AWS Lambda function is flexible:

  • As of Nov. 2018, you can write in any language — even the 1959 language Cobol!
  • You can trigger the code to run from a database action, an API query, a metric in CloudWatch, a 3rd party monitoring app, an IoT device…
  • Even though a Lambda’s max timeout is 15 minutes, it can trigger another Lambda. So you can even do DNA sequencing with Lambda.
  • You’re not locked into AWS. A Lambda just has the core logic — no server configurations. Google Cloud, Azure, and IBM have cloud functions too.

💸 An AWS Lambda function can be cheap :

  • You can run your code 1 Million times per month for free, forever. That’s enough for many businesses, especially startups.
  • You only pay for the milliseconds it’s in use for, instead of paying 24/7 costs for a server, not to mention IT staff.
  • AWS has been around since 2006 and has never raised costs. Prices keep falling dramatically.

🏃 An AWS Lambda function makes you agile:

  • Even if it doesn’t reduce your costs, because you have billions of users, the speed of development is worth it. Think of all the developer hours saved.
  • Imagine how much faster you can try out ideas with customers.

🍀 And yes, a Lambda can run Spring Boot.

Get a Sample Spring Boot API Running in 5 Minutes

  1. Prerequisites.
  2. Clone the code from GitHub.
  3. Build it with Maven.
  4. Deploy it to AWS.
  5. 🐶 Test it by going to the API’s URL and getting back a JSON of dog names.

Prerequisites

🐑 Clone the Code 🐑

This was written by AWS Labs, and then I converted the Spring Boot Pet Store example from SAM to the Serverless Framework. I wrote about how that reduced deployment complexity by 97.4%.

  
git clone https://github.com/serverless-guru/aws-serverless-java-container.git
  

🚜 Build It With Maven

  
cd samples/springboot/pet-store
mvn package
  

🚀 Deploy the API

  
sls deploy
  

Watch out for the output in your command-line. You should see something like this:

  
endpoints:
  ANY - https://rbzh9fj8q1.execute-api.us-west-2.amazonaws.com/dev/
  

⚗️ Test It

Paste that endpoint URL into your browser and add pets at the end, so .com/dev/pets. If it’s a success, then congratulations. If not, then leave a comment.

Anatomy of a Spring Boot Lambda

Just as with any Lambda, you can take advantage of CloudWatch Logs to print debugging messages. 🐛

The entry point to any Lambda accepts arguments, including the event that triggered it (the inputStream). This one is in the StreamLambdaHandler.java:

  
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context)
  throws IOException {
    handler.proxyStream(inputStream, outputStream, context);
}
  

What kind of Java things have you done in Lambda?

Serverless Handbook
Access free book

The dream team

At Serverless Guru, we're a collective of proactive solution finders. We prioritize genuineness, forward-thinking vision, and above all, we commit to diligently serving our members each and every day.

See open positions

Looking for skilled architects & developers?

Join businesses around the globe that trust our services. Let's start your serverless journey. Get in touch today!
Ryan Jones - Founder
Ryan Jones
Founder
Speak to a Guru
arrow
Edu Marcos - CTO
Edu Marcos
Chief Technology Officer
Speak to a Guru
arrow
Mason Toberny
Mason Toberny
Head of Enterprise Accounts
Speak to a Guru
arrow

Join the Community

Gather, share, and learn about AWS and serverless with enthusiasts worldwide in our open and free community.