Slide 1

Slide 1 text

Maximilian Schellhorn maxschell Dennis Kieselhorst kieselhorst SPRING CLOUD FUNCTION IN ACTION How to leverage Serverless Java?

Slide 2

Slide 2 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 3 Agenda • Evolution of Compute à Serverless • Java Functional Interfaces • What is Spring Cloud Function? • Serverless functions along with examples on AWS Lambda • Asynchronous processing • Multiple functions and routing • Performance considerations and testing

Slide 3

Slide 3 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 4 Evolution of compute à Serverless Level of abstraction Focus on business logic Serverless Physical machines Virtual machines Containerization FaaS Serverless Containers • Continuous scaling • Fault tolerance built-in • Pay for value • Zero maintenance • Focus on business value

Slide 4

Slide 4 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 5 Java Functional Interfaces Supplier • Method: T get() • Use case: Lazy value generation, factory methods Consumer • Method: void accept(T t) • Use case: Processing or consuming data streams Function • Method: R apply(T t) • Use case: Data transformation, mapping operations Others BiFunction Predicate UnaryOperator BiConsumer Interfaces with a single abstract method, enabling use with lambda expressions and method references.

Slide 5

Slide 5 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 6 What is Spring Cloud Function? • Promotes the use of Java Functions to implement business requirements • Platform-independent function development - write once, run anywhere through platform-specific adapters • Standalone • AWS • Azure • GCP • Automatic function registration and routing • Type conversion and content negotiation • Function composition and chaining • Testing support for functional applications 6

Slide 6

Slide 6 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 10 Serverless Architecture 10 Event Changes in data state Requests to endpoints Changes in resource state Application code AWS Lambda function Framework

Slide 7

Slide 7 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 11 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 8

Slide 8 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 12 AWS Lambda invocation AWS Lambda Service GetRequestId Function { "some": "json" } { "result": "json" } API request to endpoints Lambda Invoke-API Invoke

Slide 9

Slide 9 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 13 Functions are invoked by events 13 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-..", ... } } API request to endpoints • 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.)

Slide 10

Slide 10 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 15 Spring AWS adapter FunctionInvoker 15 { "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 2. Routing 1. Type Conversion (Abstraction)

Slide 11

Slide 11 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 16 How to process events asynchronously? 16

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 18 Functions are invoked by events 18 { "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 AWS Lambda Function • 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.)

Slide 14

Slide 14 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 20 What about multiple functions? 20

Slide 15

Slide 15 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 21 Function granularity Will I end up with hundreds of functions? API Gateway /lowerCase /upperCase /reverse /hello /camelCase /random GET unicorn POST unicorn PUT unicorn DELETE unicorn HEAD unicorn

Slide 16

Slide 16 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 22 API Gateway CRUD /unicorns /stringutils Many functions vs. few functions API Gateway /lowerCase /upperCase /reverse /hello /camelCase /random GET unicorn POST unicorn PUT unicorn DELETE unicorn HEAD unicorn

Slide 17

Slide 17 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 24 Static routing for multiple Lambda functions 24 Use cases: Different permission or scaling requirements per function UpperCaseFunction LowerCaseFunction @SpringBootApplication public class SpringCloudFunctionDemoApplication { @Bean public Function upperCase() { return String::toUpperCase; } @Bean public Function lowerCase() { return String::toLowerCase; } }

Slide 18

Slide 18 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 25 UpperCase Function LowerCase Function /upperCase /lowerCase org.springframework.cloud.function. adapter.aws.FunctionInvoker Static routing for multiple Lambda functions Amazon API Gateway SPRING_CLOUD_FUNCTION_DEFINITION: upperCase SPRING_CLOUD_FUNCTION_DEFINITION: lowerCase @Bean public Function upperCase() { return String::toUpperCase; } @Bean public Function lowerCase() { return String::toLowerCase; }

Slide 19

Slide 19 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 26 Dynamic routing for a single Lambda function 26 Use cases: Same permission set (CRUD), less cold-starts, simple deployment StringUtils Function @SpringBootApplication public class SpringCloudFunctionDemoApplication { @Bean public Function upperCase() { return String::toUpperCase; } @Bean public Function lowerCase() { return String::toLowerCase; } }

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 28 Dynamic routing for a single Lambda function 28 StringUtils Amazon API Gateway org.springframework.cloud.function. adapter.aws.FunctionInvoker HTTP Header / Custom Logic upperCase

Slide 22

Slide 22 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 29 Dynamic routing for a single Lambda function 29 upperCase StringUtils Amazon API Gateway org.springframework.cloud.function. adapter.aws.FunctionInvoker HTTP Header / Custom Logic upperCase

Slide 23

Slide 23 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 30 Dynamic routing for a single Lambda function 30 StringUtils Amazon API Gateway org.springframework.cloud.function. adapter.aws.FunctionInvoker HTTP Header / Custom Logic reverse reverse

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 32 1 Lambda function ≠ 1 Spring Cloud Function

Slide 26

Slide 26 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 33 What about performance? 33

Slide 27

Slide 27 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 34 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 34

Slide 28

Slide 28 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 36 AWS Lambda request handling 36 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 29

Slide 29 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 37 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 30

Slide 30 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 38 How SnapStart works 38 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 31

Slide 31 text

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

Slide 32

Slide 32 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 40 Testing • Local HTTP Invoke with Spring Boot Web Package • Spring Integration Testing • AWS SAM local invoke • Testing in the cloud? GitHub: aws-samples/java-on-aws/ samples/spring-cloud-function-demo

Slide 33

Slide 33 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 42 42 What about RestControllers?

Slide 34

Slide 34 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 43 HTTP adapter 43 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 35

Slide 35 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 44 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 44

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 49 Recap: Spring I/O 2024

Slide 38

Slide 38 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 50 Updated: Spring Cloud Function AWS Lambda docs

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 52 Thank you! © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. Dennis Kieselhorst [email protected] Maximilian Schellhorn [email protected]