Slide 1

Slide 1 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Serverless Java with Spring Maximilian Schellhorn Senior Solutions Architect AWS Dennis Kieselhorst Principal Solutions Architect AWS

Slide 2

Slide 2 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 2

Slide 3

Slide 3 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon EKS, Amazon ECS container orchestration 2 Run on compute instances You have to manage compute capacity Customer account Instance Server-based containers 1 Pod Networking Application Load Balancer NAT gateway Pod definition Pod Pod Pod Pod Pod Pod

Slide 4

Slide 4 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Run on compute instances You have to manage compute capacity Customer account Instance Server-based containers (continued) Cluster auto scaling, Karpenter Instance 2 Networking Application Load Balancer NAT gateway Pod definition Pod Pod Pod Pod Amazon EKS, Amazon ECS container orchestration 1 Platform engineering Auth State Abstraction Event bus Policy Automation API OPA ArgoCD Crossplane

Slide 5

Slide 5 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Networking You don’t have to manage capacity Customer account Serverless containers AWS Fargate 2 Pod Application Load Balancer NAT gateway Pod definition Amazon EKS, Amazon ECS container orchestration 1

Slide 6

Slide 6 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. From full control to fully managed Container Base Image Runtime App Networking Cloud Container Base Image Runtime App Networking Compute Cloud Auto Scaling Container Base Image Runtime App Cloud

Slide 7

Slide 7 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved.

Slide 8

Slide 8 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Anatomy of a Serverless function Handler function • Function written in Java, JS, Python etc. • Input params with req. information Configuration and Deployment • Runtime Version: Java 11, Java 17, Java 21 • Memory setting, CPU Architecture (x86, ARM), Timeout up to 15 minutes • Package as an uber jar and upload via UI, CLI or infrastructure-as-code 10240 MB 128 MB

Slide 9

Slide 9 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Example: AWS Lambda invocation AWS Lambda Service GetRequestId Function API request to endpoints Lambda Invoke-API Invoke UnicornRequestIdHandler::handleRequest

Slide 10

Slide 10 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda Event integration Messages from a queue or stream Reads ProcessMessage Function Invoke AWS Lambda Service (Event Source Mapping) SQSHandler::handleRequest

Slide 11

Slide 11 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Functions are invoked by events 12 AWS Lambda Function POST /v1/pets HTTP/2 Host: x.execute-api.eu-west-1.. User-Agent: curl/7.64.1 Accept: */* Content-Type: application/json Content-Length: 39 Body: {“data:“ : “test“} { "body": “{“data“: “test“}“ , "resource": "/{proxy+}", "path": "/path/to/resource", "httpMethod": "POST", "isBase64Encoded": true, "headers": { "Accept-Encoding": "gzip", "Accept-Language": "en-US,en;q=0.8" }, }, "requestContext": { "accountId": "123456789012", "requestId": "c6af9ac6-7b61-..", ... } } • AWS Lambda is invoked via events (API Event, Kafka Event, SQS Event ..) • Events follow a certain structure • This is different from the raw format (HTTP, Kafka Record etc.) API Gateway

Slide 12

Slide 12 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Functions are invoked by events • AWS Lambda is invoked via events (API Event, Kafka Event, SQS Event ..) • Events follow a certain structure • This is different from the raw format (HTTP, Kafka Record etc.) 13 { "eventSource":"aws:kafka",", "bootstrapServers":"b-2.demo-cluster", "records":{ "mytopic-0":[ { "topic":"mytopic", "partition":0, "offset":15, "timestamp":1545084650987, "key":"abcDEFghiJKLmnostuVW", "value":“my-value", "headers":[] }] } } AWS Lambda Service (Event Source Mapping) AWS Lambda Function

Slide 13

Slide 13 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Spring Cloud Function § Promotes the use of Java functions to implement business requirements § Additional routing and mapping functionality § Specific adapters (spring-cloud-function-adapter-aws) 14

Slide 14

Slide 14 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Spring Cloud Function advantages 15 org.springframework.cloud.function. adapter.aws.FunctionInvoker Context agnostic with Message<> Type Mapping to your POJO Plain Java Functions Leverage full Spring ecosystem (DI, Spring Data, Testing) Platform specific entrypoint for routing and abstraction

Slide 15

Slide 15 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Spring AWS adapter FunctionInvoker 16 { "body": "{"name": "MyPet"}" , "resource": "/{proxy+}", "path": "/path/to/resource", "httpMethod": "POST", "isBase64Encoded": true, "headers": { "Accept-Encoding": "gzip", "Accept-Language": "en-US,en;q=0.8" }, }, "requestContext": { "accountId": "123456789012", "requestId": "c6af9ac6-7b61-..", ... } } API Gateway org.springframework.cloud.function. adapter.aws.FunctionInvoker

Slide 16

Slide 16 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Spring Cloud Function Routing 17 FunctionInvoker needs routing instructions for multiple functional beans Static configuration via application.properties / environment variables Dynamic configuration via Message headers & MessageRoutingCallback

Slide 17

Slide 17 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Static routing for multiple Lambda functions 18 Use cases: Different permission or scaling requirements per function PostPetFunction DeletePetFunction

Slide 18

Slide 18 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. PostPet Function DeletePet Function POST DELETE org.springframework.cloud.function. adapter.aws.FunctionInvoker /unicorns Static routing for multiple Lambda functions Amazon API Gateway SPRING_CLOUD_FUNCTION_DEFINITION: handlePetPostFunction SPRING_CLOUD_FUNCTION_DEFINITION: handlePetDeleteFunction

Slide 19

Slide 19 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Function granularity Will I end up with hundreds of functions? API Gateway GET /unicorns POST PUT DELETE HEAD OPTIONS /orders GET POST PUT DELETE HEAD

Slide 20

Slide 20 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. API Gateway GET /unicorns POST PUT DELETE HEAD OPTIONS /orders GET POST PUT DELETE HEAD API Gateway /orders /unicorns Many functions vs. few functions /orders/{id}/images

Slide 21

Slide 21 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Dynamic routing for a single Lambda function 22 Use cases: Same permission set (CRUD), less cold-starts, simple deployment PetFunction

Slide 22

Slide 22 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Dynamic routing for a single Lambda function 23 PetFunction Amazon API Gateway org.springframework.cloud.function. adapter.aws.FunctionInvoker

Slide 23

Slide 23 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Dynamic routing for a single Lambda function 24 PetFunction Amazon API Gateway org.springframework.cloud.function. adapter.aws.FunctionInvoker HTTP Header : spring.cloud.function.definition: handlePetPostFunction

Slide 24

Slide 24 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Dynamic routing for a single Lambda function 25 handlePetPostFunction PetFunction Amazon API Gateway org.springframework.cloud.function. adapter.aws.FunctionInvoker HTTP Header : spring.cloud.function.definition: handlePetPostFunction

Slide 25

Slide 25 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Dynamic routing for a single Lambda function 26 PetFunction Amazon API Gateway org.springframework.cloud.function. adapter.aws.FunctionInvoker HTTP Header : spring.cloud.function.definition: handlePetDeleteFunction handlePetDeleteFunction

Slide 26

Slide 26 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Dynamic routing with custom logic 27 Implement your own dynamic routing logic

Slide 27

Slide 27 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Recap on Spring Cloud Function • Standard way for asynchronous functions triggered by Kafka, RabbitMQ, Amazon SQS etc. • Good for small to medium sized synchronous APIs (CRUD /pet) • Additional benefit for synchronous functions: No need for embedded HTTP servers (Tomcat or Netty) • Portability and testing experience 28 1 Lambda function ≠ 1 Spring Cloud Function

Slide 28

Slide 28 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 31 What about RestControllers?

Slide 29

Slide 29 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. HTTP adapter 32 Framework Application code Adapter logic Web app function Invocation event mapped to framework request Function result mapped from framework response Web application wrapped in adapter logic. Amazon API Gateway

Slide 30

Slide 30 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Example with AWS Serverless Java Container • Transforms events so that frameworks can handle them as if it was an HTTP Request • Routing via RestController, POJO serialization, HTTP status codes • Add the library to your code and provide a configuration class 33

Slide 31

Slide 31 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 34 AWS Serverless Java Container 2.1.2 Support for Spring Framework 6.x, Spring Boot 3.x and JAX-RS/ Jersey 3.x including native image support (GraalVM)

Slide 32

Slide 32 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Traditional Servlet Container vs. SJC 35 Embedded Apache Tomcat HTTP Connector Servlet Container Expose port Spring Boot (Web) HttpServlet POST /orders -> doPost GET / orders -> doGet Serverless Java Container Spring Boot (Web) HttpServlet POST /orders -> doPost GET / orders -> doGet API Gateway JSON Your Java Code Your Java Code

Slide 33

Slide 33 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Serverless Java Container & Spring Cloud Function 36 Spring Framework Application code (@SpringBootApplication, @Controller) AWS Serverless Java Container Web app function Invocation event mapped to framework request Function result mapped from framework response Amazon API Gateway Spring Cloud Function Spring Boot

Slide 34

Slide 34 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Steps to implement 1. Add dependency to pom.xml (or Gradle buildfile accordingly) 2. Configure the handler class 3. Package and deploy 37 com.amazonaws.serverless aws-serverless-java-container-springboot3 2.1.2 com.amazonaws.serverless.proxy.spring.SpringDelegatingLambdaContainerHandler

Slide 35

Slide 35 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Advantages of HTTP Adapter • Use the familiar @Controller programming style • Easily port your existing applications to AWS Lambda • Sample implementations in Spring Boot via Serverless Java Container 38 com.amazonaws.serverless.proxy.spring.SpringDelegatingLambdaContainerHandler

Slide 36

Slide 36 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Summary: Two main ways of handling events in Java • Deserialize JSON payload to a Java object, no HTTP request/ response classes involved • Consume and process the content in event specific code • Standard way of implementing for Non-HTTP usecases • More difficult to replatform existing applications and reuse frameworks • Adapter transforms events to HTTP requests/ responses (e.g. to Jakarta Servlet API) • Allows to keep existing code and reuse existing frameworks as is • Some overhead due to additional transformation 39 Handling via functions Using an HTTP adapter

Slide 37

Slide 37 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Performance 40

Slide 38

Slide 38 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda request handling 41 Execution Environment ready? Download Code Start Runtime Initialize Function Code Code execution Request Code execution No Yes Create Execution Environment Execution Environment Cold start Warm start

Slide 39

Slide 39 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Example: AWS Lambda scaling • Initialize execution environment on first invoke • An execution environment handles a single request at a time • If an execution environment is busy with a request – another environment is started – simpler programming model • Scales to zero if there is no request – Managed auto scaling 42 1 Initialization Execution Execution 2 Execution 3 Initialization 4 Execution Env #1 Env #2

Slide 40

Slide 40 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 0 2 4 6 8 10 12 14 0 1 2 3 4 5 6 7 8 9 Tiered Compilation Compile Time frameworks No framework No optimization Lightw. deps Function handler GraalVM Effort to modernize Cold-start (seconds) Provisioned Concurrency AWS Lambda performance optimizations for a typical Spring application SnapStart Default with Java 17 & 21 on AWS Lambda 43

Slide 41

Slide 41 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 44 AWS Lambda SnapStart Up to 10x faster start-up performance

Slide 42

Slide 42 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda SnapStart • Takes a snapshot of the memory and disk state • Snapshot encryption & caching for low-latency access • Creates new Lambda environments from cached snapshot • Fully managed microVM snapshot technology

Slide 43

Slide 43 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. How SnapStart works 46 Code execution Invoke Resume Resume Snapshot Post Snapshot Hook (optional) Pre Snapshot Hook (optional) Create Execution Environment Init during deployment Download Code Start Runtime Initialize Function Code Create Snapshot first request

Slide 44

Slide 44 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. SnapStart benefits • No additional cost • Minimal to no code changes • Customization via Runtime Hooks 47 Without SnapStart With SnapStart 6.725 - 80 % 1.15 Full Duration Results will vary based on your application

Slide 45

Slide 45 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. - beforeCheckpoint - priming - class loading - dependency injection - afterRestore - unique ID for execution environment - re-establish connections CRaC interface for post- and pre-hooks examples Spring Boot >3.2 implements useful hooks for you!

Slide 46

Slide 46 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Custom runtime/ GraalVM native image 49

Slide 47

Slide 47 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. GraalVM native images on AWS Lambda § Create a native binary and deploy as AWS Lambda custom runtime § Sub-second application startup and low memory footprint § Spring Boot provides built-in support 50 bootstrap 2023

Slide 48

Slide 48 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 0 2 4 6 8 10 12 14 0 1 2 3 4 5 6 7 8 9 Tiered Compilation Compile Time frameworks No framework No optimization Lightw. deps Function handler GraalVM Effort to modernize Cold-start (seconds) Provisioned Concurrency AWS Lambda performance optimizations for a typical Spring application SnapStart Default with Java 17 & 21 on AWS Lambda 51

Slide 49

Slide 49 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Deep Dive: Java on AWS Lambda Workshop 55 https://catalog.workshops.aws/java-on-aws-lambda

Slide 50

Slide 50 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Thank you! Maximilian Schellhorn @maschnetwork Dennis Kieselhorst @dekies.de 57 Please complete the session survey using the website https://www.jfokus.se/rate/2247