Serverless Spring Application Development

Nov 9, 2024

Serverless Spring Lecture Notes

Introduction

  • Speaker: Dan Vega
  • Topic: Serverless Spring
  • Overview of the new guide on Tanzu Developer Center titled "Serverless Spring".
  • Research and development of serverless applications in Java.

Key Concepts in Serverless Development

  • Building real-world applications requires:
    • Logging
    • Configuration
    • Dependency Injection
    • Database connectivity
    • Handling environment variables
  • Spring framework aids in building these applications.

Spring Cloud Function

  • Provides a consistent programming model for serverless applications.
  • Allows deployment to various cloud providers (e.g., AWS, Azure, Google Cloud).
  • Focus of the lecture: to build a serverless application with Spring and deploy it to AWS Lambda.

Use Cases for Serverless Applications

  • Full applications
  • Static sites + backend as a service
  • REST APIs
  • Authentication
  • Multimedia transformation
  • Email notifications
  • Data transformation
  • Cron jobs

Prerequisites

  • Use Java 11 for compatibility with AWS Lambda.
  • Basic understanding of Java and Spring Boot.
  • Final code available on GitHub.

Example Application: Newsletter Service

  • The application being built: A newsletter service to manage subscribers.
  • Requirements:
    • Adding subscribers
    • Listing subscribers

Application Setup

  1. Visit start.spring.io to create a new Spring Boot application.
  2. Metadata to fill:
    • Name: Newsletter
    • Dependencies: Spring Web, Spring Cloud Function
  3. Generate and open the project in an IDE.

Building the Newsletter API

  • Create a Subscriber model with:
    • ID (integer)
    • Email (string)
  • Create a SubscriberService to manage an in-memory list of subscribers:
    • findAll: return all subscribers.
    • create: add a new subscriber.
  • Create a Subscribers function class to handle:
    • Listing subscribers (using a supplier)
    • Adding subscribers (using a consumer)

Running the Application

  • Test the application locally:
    • Use CURL commands to create and list subscribers.
    • Endpoints:
      • POST to /create for new subscribers.
      • GET from /findAll to list subscribers.
    • Spring Cloud Function maps endpoints to function names.

Deploying to AWS Lambda

  1. Update pom.xml to include dependencies for AWS Lambda.
  2. Use the Maven Shade Plugin to create an AWS deployable jar.
  3. Create a new function in AWS Lambda:
    • Runtime: Java 11
    • Upload the created AWS jar.
    • Configure function URL to invoke your functions.

Testing the Deployed Function

  • Use Postman to test:
    • Send a POST request to create a new subscriber with the appropriate header.
    • Send a request to find all subscribers.
  • Ensure the correct function is invoked using header values.

Conclusion

  • Successfully deployed a serverless application to AWS Lambda.
  • Suggested future enhancements like integrating Spring Data JPA for database connectivity.
  • Mention of Spring Boot 3's AOT (Ahead of Time Compilation) for improved performance in serverless applications.

Call to Action

  • Encouragement to explore the guide for further learning.
  • Request for feedback on additional tutorials.