This project demonstrates a serverless AWS Lambda function built using Spring Boot 3.2 and Java 17. It leverages Spring Cloud Function to seamlessly integrate Spring Boot with AWS Lambda runtime.
- ✅ Java 17
- ✅ Spring Boot 3.2.1
- ✅ Spring Cloud Function 4.1.0
- ✅ AWS Lambda Java Core & Events
- ✅ Maven build with Shade plugin
- ✅ Unit tests with JUnit 5
.
├── pom.xml # Maven configuration
├── src
│ ├── main
│ │ ├── java/com/example/lambda
│ │ │ ├── LambdaApplication.java # Spring Boot application
│ │ │ ├── LambdaHandler.java # AWS Lambda entry point
│ │ │ ├── Request.java # Request POJO
│ │ │ └── Response.java # Response POJO
│ │ └── resources
│ │ └── application.properties # Spring configuration
│ └── test
│ └── java/com/example/lambda
│ ├── LambdaApplicationTests.java # Context load test
│ └── ProcessRequestFunctionTests.java # Function tests
└── README.md
- Java 17 or higher
- Maven 3.6 or higher
- AWS CLI (for deployment)
- AWS Account with appropriate permissions
Build the project using Maven:
mvn clean packageThis will create two JAR files in the target directory:
aws-lambda-spring-boot-1.0.0.jar- Regular JARaws-lambda-spring-boot-1.0.0-aws.jar- Shaded JAR with all dependencies (use this for AWS Lambda)
Run all tests:
mvn testYou can test the function locally using the AWS SAM CLI or by creating a simple test harness.
{
"name": "John Doe",
"message": "Hello from Lambda"
}{
"message": "Hello John Doe! Your request has been processed successfully.",
"status": "SUCCESS",
"timestamp": 1706024897113
}Using AWS CLI:
aws lambda create-function \
--function-name spring-boot-lambda \
--runtime java17 \
--role arn:aws:iam::YOUR_ACCOUNT_ID:role/lambda-execution-role \
--handler com.example.lambda.LambdaHandler::handleRequest \
--zip-file fileb://target/aws-lambda-spring-boot-1.0.0-aws.jar \
--timeout 30 \
--memory-size 512 \
--environment Variables={SPRING_CLOUD_FUNCTION_DEFINITION=processRequest}After making changes, update the function:
mvn clean package
aws lambda update-function-code \
--function-name spring-boot-lambda \
--zip-file fileb://target/aws-lambda-spring-boot-1.0.0-aws.jaraws lambda invoke \
--function-name spring-boot-lambda \
--payload '{"name":"John Doe","message":"Test"}' \
response.json
cat response.jsonThe Lambda handler is configured in LambdaHandler.java:
- Handler:
com.example.lambda.LambdaHandler::handleRequest
The function is defined in LambdaApplication.java:
- Function Name:
processRequest - Input:
Requestobject - Output:
Responseobject
Configuration in src/main/resources/application.properties:
spring.application.name=aws-lambda-spring-boot
spring.cloud.function.definition=processRequest- Create a new
@Beanmethod inLambdaApplication.java:
@Bean
public Function<MyRequest, MyResponse> myFunction() {
return request -> {
// Your logic here
return new MyResponse();
};
}- Update
application.properties:
spring.cloud.function.definition=myFunctionSet environment variables in AWS Lambda console or via CLI:
aws lambda update-function-configuration \
--function-name spring-boot-lambda \
--environment Variables={SPRING_CLOUD_FUNCTION_DEFINITION=processRequest,LOG_LEVEL=DEBUG}-
Cold Start: Spring Boot Lambda functions may have longer cold start times. Consider using:
- AWS Lambda SnapStart (for Java 11+)
- Provisioned Concurrency
- Reducing dependencies
-
Memory: Allocate sufficient memory (512MB-1024MB recommended for Spring Boot)
-
Timeout: Set appropriate timeout (30 seconds recommended minimum)
- ClassNotFoundException: Ensure you're using the
-aws.jarshaded artifact - Timeout: Increase Lambda timeout in function configuration
- Out of Memory: Increase Lambda memory allocation
aws logs tail /aws/lambda/spring-boot-lambda --followThis project is open source and available under the MIT License.
Contributions are welcome! Please feel free to submit a Pull Request.