Spring Boot 3 application on AWS Lambda - Part 8 Introduction to Spring Cloud Function
Tags: java, springboot, aws, serverless
Published: 2024-06-24 15:11:59

During the parts [2](https://dev.to/aws-builders/spring-boot-3-application-on-aws-lambda-part-2-introduction-to-aws-serverless-java-container-144), [3](https://dev.to/aws-builders/spring-boot-3-application-on-aws-lambda-part-3-develop-application-with-aws-serverless-java-container-2901) and [4](https://dev.to/aws-builders/spring-boot-3-application-on-aws-lambda-part-4-measuring-cold-and-warm-starts-with-aws-serverless-java-container-mb0) we introduced the concept behind the AWS Serverless Java Container and especially its variant for the Spring Boot (3) application, learned how to develop, deploy, run and optimize Spring Boot 3 Application on AWS Lambda using AWS Serverless Java Container. In the parts [5](https://dev.to/aws-builders/spring-boot-3-application-on-aws-lambda-part-5-introduction-to-aws-lambda-web-adapter-m21), [6](https://dev.to/aws-builders/spring-boot-3-application-on-aws-lambda-part-6-develop-application-with-aws-lambda-web-adapter-h88) and [7](https://dev.to/aws-builders/spring-boot-3-application-on-aws-lambda-part-7-measuring-cold-and-warm-starts-with-aws-lambda-web-adapter-310h) we did the same but for AWS Lambda Web Adapter. In this part of the series we'll introduce another alternative, the framework called [Spring Cloud Function](https://spring.io/projects/spring-cloud-function), concepts behind it and especially its integration with AWS Lambda.

## Spring Cloud Function

Spring Cloud Function is a project with the following high-level goals:

- Promote the implementation of business logic via functions.
- Decouple the development lifecycle of business logic from any specific runtime target so that the same code can run as a web endpoint, a stream processor, or a task. One of these specific runtime targets can be AWS Lambda which will be focus of this article.
- Support a uniform programming model across serverless providers, as well as the ability to run standalone (locally or in a PaaS).
- Enable Spring Boot features (auto-configuration, dependency injection, metrics) on serverless providers.
- It abstracts away all of the transport details and infrastructure, allowing the developer to keep all the familiar tools and processes, and focus firmly on business logic.

A simple function application (in context or Spring) is an application that contains beans of type Supplier, [Java 8 Function interface](https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html) or Consumer. 

## Spring Cloud Function on AWS Lambda

With [Spring Cloud Function on AWS Lambda](https://docs.spring.io/spring-cloud-function/reference/adapters/aws-intro.html), [AWS adapter takes a Spring Cloud Function](https://github.com/spring-cloud/spring-cloud-function/tree/main/spring-cloud-function-adapters/spring-cloud-function-adapter-aws) app and converts it to a form that can run in AWS Lambda. So, with AWS it means that a simple function bean should somehow be recognized and executed in AWS Lambda environment.  Which very well fits into the AWS Lambda model with Amazon API Gateway in front which similar to the Java 8 function receives the (HTTP) request, executes some business logic and then sends the (HTTP) response to the caller.

```
@SpringBootApplication
public class FunctionConfiguration {

      public static void main(String[] args) {
            SpringApplication.run(FunctionConfiguration.class, args);
      }

      @Bean
      public Function<String, String> uppercase() {
            return value -> value.toUpperCase();
      }
}
```
The diagram below describes the request flow in the typical web application on AWS. AWS Request Adapter converts the JSON coming from Lambda function to the [HttpServletRequest](https://jakarta.ee/specifications/platform/9/apidocs/jakarta/servlet/http/httpservletrequest) which then invokes the [Spring Dispatcher Servlet](https://docs.spring.io/spring-framework/reference/web/webmvc/mvc-servlet.html) which then interacts with our Spring Boot application on API level without starting web server (i.e. Tomcat). Then response flows back and AWS Response Adapter converts [HttpServletResponse](https://jakarta.ee/specifications/platform/9/apidocs/jakarta/servlet/http/httpservletresponse) to JSON which Lambda function sends back to API Gateway.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kxsds9wmfunwtas0s79a.png)

## Conclusion

In this part of the series, we introduced Spring Cloud Function and especially its integration with AWS Lambda and concepts behind it. In next part we'll learn how to develop and deploy application using this framework.