Move Your Spring Boot API to AWS Lambda and Reduce Costs by 100%

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

Photo by Sarah Dorweiler on Unsplash

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?

What did we miss?

When you leave your answer make sure to either comment below or tweet your answer to @serverlessgurux on Twitter.

https://serverlessguru.com

Yann Stoneman

Full Stack Cloud Developer — Serverless Guru

LinkedIn — @ystoneman

Twitter — @yannstoneman

Thanks for reading 😃

If you would like to learn more about Serverless Guru, please follow us on Medium, Twitter, Instagram, Facebook, or LinkedIn!

Serverless Guru

Serverless Guru is a cloud consulting and training company specializing in Serverless development and cloud optimization.

Yann Stoneman

Written by

Full Stack Cloud Developer @Serverless Guru. 4x AWS Certified, incl. Developer, SysOps & Solutions Architect Associate

Serverless Guru

Serverless Guru is a cloud consulting and training company specializing in Serverless development and cloud optimization.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade