This project implements Spring Cloud Function to create serverless applications with Spring Boot for AWS Lambda + API Gateway.
Because AWS Lambda only allows exposing a single function per Lambda entry point, Spring Cloud Function provides a way to route multiple functions through a single Lambda function using a Routing Function. To route requests you must include a header in the request.
With Spring Cloud Function there is a Routing function registered in the default catalog named functionRouter. The application has been configured to use it as the default function via:
spring:
cloud:
function:
definition: functionRouterIf you make a request like the one below, the request will be routed to the uppercase function.
curl -X post "http://localhost:8080" \
-H "Content-Type: text/plain; charset=utf-8" \
-H "spring.cloud.function.definition: uppercase" \
--data-binary $'hello world'You should receive:
HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
HELLO WORLDThis is another routing function that takes precedence over the default Routing Function in the catalog.
@Bean
public MessageRoutingCallback DefaultRouterCallBack() {
return new MessageRoutingCallback() {
@Override
public String routingResult(Message<?> message) {
return (String) message.getHeaders().get("function");
}
};
}To indicate a function you can do it like this:
curl -X post "http://localhost:8080" \
-H "Content-Type: text/plain; charset=utf-8" \
-H "function: uppercase" \
--data-binary $'hello world'The defaultMessageRoutingHandler function acts as a catch-all.
@Bean
public Function<Message<?>, Message<String>> defaultMessageRoutingHandler( ) {
return message -> MessageBuilder.withPayload("funcion no encontrada").build();
}When the functionRouter routes to a function that is not present in the functionCatalog, it will receive a response like:
HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
funcion no encontrada
- JDK 21
- Maven 3.6+
You can run the application locally:
-
Clone the repo: git clone https://github.com/MateoRodriguez0/spring-cloud-serverless.git
-
Uncomment the dependencies
spring-cloud-function-webandspring-boot-starter-webso a web server is enabled. -
Run with Spring Boot:
mvn spring-boot:run
- To deploy this project first build it with the following command:
mvn clean package-
The previous command will build the application and produce 2 JARs, as configured in the pom.xml.
-
Upload to Lambda the JAR named
spring-serverless-0.0.1-SNAPSHOT-aws.jar. -
In the Lambda function, set the runtime to Java 21.
-
Configure the handler as
org.springframework.cloud.function.adapter.aws.FunctionInvoker::handleRequest. -
If you prefer to expose a default function, override the environment variable
SPRING_CLOUD_FUNCTION_DEFINITION.
Note
This repository includes a ready-to-use SAM template at the project root named template.yml. If you prefer to run or deploy the application using AWS SAM, you can use that template directly.
To integrate with API Gateway, follow these steps:
-
Create a REST API; ensure its API endpoint type is at least Regional so it is accessible outside the VPC. It must be in the same region as the Lambda function.
-
Create a resource with any name you prefer.
-
Add an
ANYmethod and select Lambda Proxy Integration. Select the Lambda function created earlier. Then click Create Method. -
Once the function is integrated, choose Deploy API and create a new stage. Finally, you will get a public invocation URL.
Important
Keep in mind that Lambda functions, and serverless applications in general, are intended to be small. Having many functions inside a single application can cause cold-start performance issues. This project was created for educational purposes and to demonstrate Spring Cloud Function concepts.