An introduction to Spring Cloud Function

Spring Cloud Function is a project which helps developers bring the power of Spring Boot features (auto-configure, metrics, ..) on Serverless platforms, but not only.

More importantly, it abstracts all the details concerning the infrastructure, the transport and the provider.

The following function :

public Function<String, String> uppercase() {
    return String::toUpperCase;

can be exposed as an http endpoint, a message consumer or a task processor and be run locally or on any of the serverless platform providers like AWS or even as a Knative service which will take care of scaling down your function app to zero in case of inactivity.

High Level Concepts

In a nutshell, the Spring Cloud Function project will detect and wrap your beans of type Function, Consumer or Supplier and expose as http endpoint and/or message stream listeners/publishers with the most common message brokers such as RabbitMQ or Kafka.

Getting Started

The best way to get started is to initiate a project on . We will expose our function to the web layer, so you will need to select the following dependencies :

  • Function
  • Spring Web

Click on generate, download the zipfile and extract it.

The content will be a standard spring boot application and you can immediately start writing your function as a bean and start the spring boot application.

Amend the main Application class and add the following bean :

public Function<String, String> uppercase() {
    return String::toUpperCase;

Launch the application :

mvn spring-boot:run

In another terminal window, you can make an http request passing a string to be uppercased by the function :

echo 'hello' | http localhost:8080


$ echo 'hello' | http localhost:8080
HTTP/1.1 200
Content-Length: 6
Content-Type: application/json
Date: Sat, 16 Jan 2021 20:24:11 GMT
Keep-Alive: timeout=60
accept: application/json, */*;q=0.5
accept-encoding: gzip, deflate
connection: keep-alive, keep-alive
contenttype: application/json
host: localhost:8080
scf-func-name: uppercase
timestamp: 1610828651105
user-agent: HTTPie/2.2.0


Since we’re having only one Function, she is automatically mapped to the root path / , we’ll see in a further blog post how to route functions.

Let’s add another bean of type Consumer :

public Consumer<String> consume() {
    return str -> System.out.println("Received " + str);

It will automatically be mapped under the /consume endpoint. Let’s try it out :

echo 'hello' | http localhost:8080/consume
2021-01-16 21:31:10.845  INFO 48020 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-01-16 21:31:10.846  INFO 48020 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms


We’ve seen the basic concepts of the Spring Cloud Function project and how it enables developers to only focus on writing the business logic by implementing it as Functions.

In further blog posts, we’ll see how to route, compose and deploy functions.

You can find the source code used in this post here.