AWS Lambda is a service that allows you to run code without provisioning or managing servers. This article walks through creating an application using Spring Cloud Function.
The “spring cloud function aws lambda example” is a tutorial that will show you how to create AWS Lambda functions from Spring Cloud Functions.
Spring Cloud Functions are used to create Lambda functions in this tutorial. Spring Cloud Functions allow you to isolate code development from the runtime objective by implementing business logic using functions. As a result, the developer may concentrate entirely on logic implementation. The kind of target endpoint, connection, integration, infrastructure, and transportation specifics do not need to be considered by the developer. All Spring Boot capabilities, including as DI, IoC, and auto-configuration, are extensively supported by Spring Cloud Functions.
Let’s begin by building a basic Spring Boot project from the ground up. We will create two Spring Cloud functions in this project, each having the following functionality:
- Take all of the students from the repository and put them in a group.
- Identify pupils by their first and last names.
We’ll next use the jar from this project to construct an AWS Lambda function and test the two Spring Cloud Functions mentioned above.
This project’s file structure is as follows.
To begin, modify the pom.xml file to include the essential maven dependencies.
org.springframework.boot 4.0.0 spring-boot-starter-parent com.javatechie springboot-aws-lambda 2.3.1.RELEASE 0.0.1-SNAPSHOT springboot-aws-lambda Spring Boot 1.8 demo project Hoxton.SR6 1.0.17.RELEASE org.springframework.boot org.springframework.boot org.springframework.boot org.springframework.boot org.springframework.boot org. org.springframework.cloud spring-boot-starter-web com.amazonaws aws-lambda spring-cloud-function-adapter-aws -java-events 2.0.2 aws-lambda com.amazonaws -java-core org.springframework.boot 1.1.0 test org.junit.vintage junit-vintage-engine org.springframework.cloud spring-boot-starter-test spring-cloud-dependencies ${spring-cloud.version} import org.apache.maven.plugins from pom maven-deploy-plugin org.springframework.boot org.springframework.boot org.springframework.boot org.springframework.boot.experimental spring-boot-maven-plugin $wrapper.version org.apache.maven.plugins spring-boot-thin-layout maven-shade-plugin 3.2.4 aws aws aws aws aws springboot-aws-lambda
Create a Student class with the Id, Name, Age, and CGPA characteristics.
Student.java
The following Java class should be placed in the ‘com.appsdeveloperblog.aws.lambda.model’ package. In the Student.java file, paste the following code, which includes the Student class definition, a constructor with all attributes, getters, and setters.
private int id; private String name; private double cgpa; private int age; public Student(int id, String name, double cgpa, int age) this.id = id; this.name = name; this.age = age; this.cgpa = cgpa; return id; public int getId(); this.id = id; public void setId(int id); return name; public String getName(); this.name = name; public void setName(String name) return cgpa; public double getCgpa(); this.cgpa = cgpa; public void setCgpa(double cgpa); public void setAge(int age) this.age = age; public int getAge() return age;
Create a StudentRepo class afterwards and include the logic for doing CRUD actions in it. For demonstration reasons, I’ve included a basic method getAllStudents() in this class.
StudentRepo.java
The StudentRepo.java class may be found in the package com.appsdeveloperblog.aws.lambda.repository. The code below implements the method getAllStudents(), which produces a list of four Student objects that is hard-coded.
Import com.appsdeveloperblog.aws.lambda.model.Student; import org.springframework.stereotype.Repository; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Collectors; import java.util.stream.Collectors; import java.util.stream.Collectors; import java.util.stream.Collect @Repository; Stream class in the open new Student(101, “Chris Robert “, 3.5, 17), new Student(102, “Steve Cullen”, 2.99, 18), new Student(278, “Raj Tiwari”, 3.25, 16), new Student(953, “Min Young”, 3.8, 17) return Stream.of( new Student(101, “Chris Robert “, 3.5, 17), new Student(102, “Steve Cullen”, 2.99, 18), new Student(278, “R if(Collectors.toList()); if(Collectors.toList()); if(Collectors.toList());
Now we’ll develop the two Spring Cloud Functions that we described before.
Spring Cloud Functions: How to Make Them
Spring Cloud functions may be created in two ways.
Method 1: Exposing a Function @Bean
Define two methods called findstudents and findStudentByName in the SpringbootAwsLambdaApplication class’s main() function. They should be of the form FunctionT, R>, with T being the input and R denoting the output. Fill in the implementation logic using the @Bean annotation on each of them, as shown below.
import com.appsdeveloperblog.aws.lambda.model.Student; import com.appsdeveloperblog.aws.lambda.respository; package com.appsdeveloperblog.aws.lambda.respository Import org.springframework.beans.factory.annotation into StudentRepo. Import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure. Autowired; import org.springframework.boot.autoconfigure. Import org.springframework.context.annotation into SpringBootApplication. Import java.util.List; import java.util.function.Function; import java.util.stream; import java.util.function.Function; import java.util.function.Function; import java.util.function.Function; import @SpringBootApplication public class; collectors SpringbootAwsLambdaApplication private @Autowired public static void main(String[] args) SpringApplication.run(SpringbootAwsLambdaApplication.class, args); studentRepo studentRepo; public static void main(String[] args) SpringApplication.run(SpringbootAwsLambdaApplication.class, args); public FunctionString, List> @Bean return (input) -> findstudents() @Bean public FunctionString, List> studentRepo.getAllStudents(); return (input) -> findStudentByName() studentRepo.getAllStudents().stream().filter(st -> st -> st -> st -> st -> st -> st .collect(Collectors.toList()); st.getName().equals(input));
Method 2: Create a class named after the function and implement the Functional interface.
To implement the two functions, create a package called com.appsdeveloperblog.aws.lambda.functions and two class files called com.appsdeveloperblog.aws.lambda.functions. It’s worth noting that the class and function names are identical. To prevent the ‘No function defined’ error when executing the Lambda function, the class name should not include capital letters anyplace other than the first place.
The logic is then implemented by altering the Functional Interface’s apply() method.
Getstudents.java
The code to get the information of all the students in the repository is shown below.
import com.appsdeveloperblog.aws.lambda.model.Student; import com.appsdeveloperblog.aws.lambda.repository; package com.appsdeveloperblog.aws.lambda.functions. Import org.springframework.beans.factory.annotation into StudentRepo. Import org.springframework.stereotype; autowired Import java.util.List; import java.util.function as a component. @Component public class; function Getstudents makes use of List>String>List>List>List>List>List>List>List>List> @Override public List apply; @Autowired private StudentRepo studentRepo (String s) if studentRepo.getAllStudents(); if studentRepo.getAllStudents(); if studentRepo.getAllStudents(); if studentRe
Getstudentsbyname.java
The code below will return a list of all students in the repository with the same name as the supplied input string.
import com.appsdeveloperblog.aws.lambda.model.Student; import com.appsdeveloperblog.aws.lambda.repository; package com.appsdeveloperblog.aws.lambda.functions. Import org.springframework.beans.factory.annotation into StudentRepo. Import org.springframework.stereotype; autowired Import java.util.List; import java.util.function as a component. Import java.util.stream as a function. @Component public class; collectors Getstudentsbyname is a program that allows you to find students by their names. List>String>List>List>List>List>List>List>List>List> = studentRepo.getAllStudents().stream().filter(st -> st -> st -> st -> st -> st -> st -> st -> st -> st -> st -> st -> st -> st -> st -> st -> st -> st -> st -> .collect(Collectors.toList()); st.getName().equals(input));
We must request that the package ‘com.appsdeveloperblog.aws.lambda.functions’ be searched for appropriate beans in order for this technique to operate. It’s as simple as adding one line to the application.properties file, as seen below.
spring.cloud.function.scan.packages=com.appsdeveloperblog.aws.lambda.functions
Create a RequestHandler class that extends the SpringBootRequestHandlerString, Object> once you’ve defined the Spring Cloud functions using one of the preceding approaches.
RequestHandler.java
This class provides an interface for sending requests from AWS Lambda to Spring Cloud Functions. Because the logic is specified in the Function specification itself, this class should be left empty.
import org.springframework.cloud.function.adapter.aws.SpringBootRequestHandler; public class RequestHandler extends SpringBootRequestHandlerString,Object>; package com.appsdeveloperblog.aws.lambda;
That is all there is to it. Using the maven install command, you can now build an executable and use the created jar file to construct a Lambda function. Let’s see whether the Spring Cloud Functions created using the preceding approaches provide the required outcomes.
- In the AWS Management Console, go to All Services and then to Compute Services, then to Lambda.
- Select the Lambda function that was just constructed.
- Set the environment variables under the Configuration tab. The key should be FUNCTION NAME, and the value should be findStudentByName (Function created using the first method)
- After that, run the function with the input “Min Young.” The function has been successfully called, and it returns the JSON string of the student object with the same name as the input.
- Now, change the environment variable to test the Function you made using the second way. Make sure that the FUNCTION NAME parameter is set to getstudentsbyname (lowercase).
- Finally, put the function to the test by passing in “Chris Robert ” as an input. This method is also successfully performed, since it returns the JSON string of the student object with the same name as the supplied input.
This concludes the tutorial. You’ve successfully constructed two Spring Cloud Functions and used AWS Lambda functions to call them.
Spring Cloud Functions is a framework that allows developers to create AWS Lambda functions from Spring Boot application. The “spring cloud function rest api” is the REST API provided by this framework.
Related Tags
- spring cloud function aws lambda api gateway
- spring cloud function aws lambda performance
- spring cloud function aws lambda github
- aws lambda spring boot
- spring-cloud-function-adapter-aws