Spring Cloud Function is a project within the Spring ecosystem that allows developers to build serverless applications using Spring Boot and deploy them to various cloud platforms, including AWS Lambda.
AWS Lambda is a serverless compute service provided by Amazon Web Services. It allows to run code without provisioning or managing servers. With Spring Cloud Function, can write functions using Spring’s programming model and deploy them to AWS Lambda.
Spring Cloud Function uses core functional interfaces provided by Java. We can create functions as Supplier (produces a result without any input), Consumer (consumes an input and produces no result), and Function (transforms an input to an output).
Lets go through the steps required to create the Serverless function. To get started create a Spring boot project with the below dependencies
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-function-context</artifactId> </dependency> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-lambda-java-events</artifactId> <version>${aws-lambda-events.version}</version> </dependency> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-lambda-java-core</artifactId> <version>1.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-function-adapter-aws</artifactId> </dependency>
Define the function as beans using @Bean
annotations
@SpringBootApplication public class CloudFunctionsLambdaApplication { public static void main(String[] args) { SpringApplication.run(CloudFunctionsLambdaApplication.class, args); } @Bean public Function<String, String> uppercase() { return value -> value.toUpperCase(); } }
The next important step is to build the jar which needs to be uploaded to AWS lambda. We will use the maven shade plugin to build a fat jar including all the dependencies.
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot.experimental</groupId> <artifactId>spring-boot-thin-layout</artifactId> <version>1.0.28.RELEASE</version> </dependency> </dependencies> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <configuration> <createDependencyReducedPom>false</createDependencyReducedPom> <shadedArtifactAttached>true</shadedArtifactAttached> <shadedClassifierName>aws</shadedClassifierName> </configuration> </plugin> </plugins> </build>
Next lets create the lambda function from AWS console.
Create a Lambda Function:
Test the Function:
Invoke the Function:
For complete source code: https://github.com/MMahendravarman/Springboot_Examples
In this blog using the Spring WebFlux module, we are going to leverage the functional…
RabbitMQ is an open-source message broker software that implements the Advanced Message Queuing Protocol (AMQP).…
Spring Integration is a powerful extension of the Spring Framework designed to support the implementation…
The Spring Cloud Config Client is a component of the Spring Cloud framework that enables…
In Python, handling CSV (Comma Separated Values) files is easy using the built-in csv module.…
Spring Cloud Config Server provides a centralized location for managing and serving configuration information to…