Skip to content

MateoRodriguez0/spring-cloud-serverless

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Cloud Serverless

This project implements Spring Cloud Function to create serverless applications with Spring Boot for AWS Lambda + API Gateway.

Routing Function

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: functionRouter

If 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 WORLD

DefaultRouterCallBack

This 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'

defaultMessageRoutingHandler

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

Installation and Build

Requirements

  • JDK 21
  • Maven 3.6+

Local Execution

You can run the application locally:

  1. Clone the repo: git clone https://github.com/MateoRodriguez0/spring-cloud-serverless.git

  2. Uncomment the dependencies spring-cloud-function-web and spring-boot-starter-web so a web server is enabled.

  3. Run with Spring Boot: mvn spring-boot:run

Deployment

AWS Lambda

  1. To deploy this project first build it with the following command:
mvn clean package
  1. The previous command will build the application and produce 2 JARs, as configured in the pom.xml.

  2. Upload to Lambda the JAR named spring-serverless-0.0.1-SNAPSHOT-aws.jar.

  3. In the Lambda function, set the runtime to Java 21.

  4. Configure the handler as org.springframework.cloud.function.adapter.aws.FunctionInvoker::handleRequest.

  5. 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.

Integration with API Gateway

To integrate with API Gateway, follow these steps:

  1. 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.

  2. Create a resource with any name you prefer.

  3. Add an ANY method and select Lambda Proxy Integration. Select the Lambda function created earlier. Then click Create Method.

  4. 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.

Authors

References

About

Spring Cloud function with lambda and API gateway

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages