Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Spring Cloud Function in action - how to levera...

Spring Cloud Function in action - how to leverage Serverless Java?

Discover how Spring Cloud Function enables writing cloud-native Java applications that seamlessly run across multiple platforms - from traditional servers to modern serverless environments like AWS Lambda. Through practical examples, we’ll explore how to write platform-agnostic business logic, handle common serverless challenges, and achieve true function portability. You’ll learn essential patterns for function composition, handling streaming & message processing, testing strategies, and deployment best practices. Walk away with the knowledge to build resilient serverless applications while leveraging your existing Spring expertise.

Avatar for Dennis Kieselhorst

Dennis Kieselhorst

May 23, 2025
Tweet

More Decks by Dennis Kieselhorst

Other Decks in Programming

Transcript

  1. © 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
  2. © 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
  3. © 2025, Amazon Web Services, Inc. or its affiliates. All

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

    rights reserved. Amazon Confidential and Trademark. 16 How to process events asynchronously? 16
  11. © 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)
  12. © 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.)
  13. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 20 What about multiple functions? 20
  14. © 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
  15. © 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
  16. © 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<String, String> upperCase() { return String::toUpperCase; } @Bean public Function<String, String> lowerCase() { return String::toLowerCase; } }
  17. © 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<String, String> upperCase() { return String::toUpperCase; } @Bean public Function<String, String> lowerCase() { return String::toLowerCase; }
  18. © 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<String, String> upperCase() { return String::toUpperCase; } @Bean public Function<String, String> lowerCase() { return String::toLowerCase; } }
  19. © 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
  20. © 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
  21. © 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
  22. © 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
  23. © 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
  24. © 2025, Amazon Web Services, Inc. or its affiliates. All

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

    rights reserved. Amazon Confidential and Trademark. 33 What about performance? 33
  26. © 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
  27. © 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
  28. © 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
  29. © 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
  30. © 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
  31. © 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
  32. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 42 42 What about RestControllers?
  33. © 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
  34. © 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
  35. © 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 <dependency> <groupId>com.amazonaws.serverless</groupId> <artifactId>aws-serverless-java-container-springboot3</artifactId> <version>2.1.3</version> </dependency> com.amazonaws.serverless.proxy.spring.SpringDelegatingLambdaContainerHandler
  36. © 2025, Amazon Web Services, Inc. or its affiliates. All

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

    rights reserved. Amazon Confidential and Trademark. 50 Updated: Spring Cloud Function AWS Lambda docs
  38. © 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
  39. © 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]