This guide will explore the steps to deploy a Java 21 Spring Boot 3 application on AWS Lambda. We’ll cover everything from setting up your environment to deploying and testing your application, ensuring you’re well-equipped to handle the entire process.
Prerequisites for AWS Lambda Deployment
Before diving into the deployment process, ensure you have the following prerequisites:
- AWS Account: Access to an AWS account with sufficient permissions to create and manage Lambda functions.
- Java 21: Ensure you have Java 21 installed on your machine.
- Maven: Apache Maven is required to build and package your Spring Boot application.
- AWS CLI: The AWS Command Line Interface (CLI) is necessary for deploying and managing AWS resources.
- AWS SAM CLI: The Serverless Application Model (SAM) CLI simplifies building and deploying serverless applications.
Access to AWS, Java 21, and Essential Tools Setup
- AWS Access: Sign in to your AWS Management Console. Ensure your user account has the necessary permissions to deploy Lambda functions.
- Java 21 Installation: Download and install Java 21 from Oracle’s official website.
- Maven Setup: Install Maven by following the instructions on the Maven website.
- AWS CLI Installation: Install the AWS CLI by following the AWS documentation.
- AWS SAM CLI Installation: Install the AWS SAM CLI from the official documentation.
Setting Up the Environment
After installing the necessary tools, configure your environment:
- AWS CLI Configuration: Run aws configure to set up your AWS credentials and default region.
- Java Version Check: Ensure Java 21 is correctly installed by running the java -version.
Creating a Spring Boot 3 Project
Now that the environment is set up, a new Spring Boot 3 project will be created.
Generating a New Spring Boot Project with Maven
- Open your terminal and navigate to your working directory.
- Run the following Maven command to generate a new Spring Boot project:
mvn archetype:generate -DgroupId=com.example -DartifactId=demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false - Navigate to the newly created project directory:
cd demo - Open the pom.xml file and update it to include Spring Boot 3 dependencies.
Integrating AWS Lambda with Spring Boot
Adding AWS Serverless Java Container Support
To run your Spring Boot application on AWS Lambda, you’ll need to add the AWS Serverless Java Container dependency:
- Open the pom.xml file and add the following dependency:
<dependency>
<groupId>com.amazonaws.serverless</groupId>
<artifactId>aws-serverless-java-container-springboot3</artifactId>
<version>1.7.3</version>
</dependency>
Implementing the Lambda Handler Class
Next, create a Lambda handler class to handle incoming requests.
Creating a Lambda Handler for Spring Boot Applications
- Create a new Java class in your project, e.g., LambdaHandler.java.
- Implement the handler as follows:
import com.amazonaws.serverless.proxy.spring.SpringBootLambdaContainerHandler;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
public class LambdaHandler implements RequestStreamHandler {
private static final SpringBootLambdaContainerHandler<?, ?> handler;
static {
handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(Application.class);
}
@Override
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
handler.proxyStream(inputStream, outputStream, context);
}
}
Designing API Endpoints with Controllers
With your Lambda handler set up, it’s time to design your API endpoints.
Defining Microservices and Endpoints in Spring Boot
- Create a new controller class, e.g., HelloController.java.
- Define your API endpoints:
@RestController
@RequestMapping(“/api”)
public class HelloController {
@GetMapping(“/hello”)
public String hello() {
return “Hello, AWS Lambda with Spring Boot 3!”;
}
}
Preparing for Deployment with SAM
Next, prepare your application for deployment using AWS SAM.
Initializing SAM and Creating a template.yaml file
- Run sam init to create a new SAM project. Choose “AWS Quick Start Templates” and “Java 8/11” as the runtime.
- Create a template.yaml file in the root of your project:
AWSTemplateFormatVersion: ‘2010-09-09’
Transform: AWS::Serverless-2016-10-31
Resources:
MySpringBootFunction:
Type: AWS::Serverless::Function
Properties:
Handler: com.example.demo.LambdaHandler
Runtime: java11
CodeUri: ./target/demo-1.0.jar
MemorySize: 512
Timeout: 30
Building and Deploying the Application
It’s time to build and deploy your Spring Boot application to AWS Lambda.
Compiling, Packaging, and Deploying to AWS Lambda
- Run the Maven build command to package your application:
mvn clean package - Deploy the application using SAM:
sam deploy –guided
Follow the prompts to configure your deployment.
Testing and Debugging the Application
After deploying, test and debug your application to ensure everything works as expected.
Verifying Functionality and Resolving Common Issues
- Invoke the Lambda function: Use the AWS CLI or AWS Console to invoke the function and check the logs for any issues.
- Debugging: If you encounter issues, review the CloudWatch logs and adjust your code or configuration as necessary.
Cleaning Up and Next Steps
Finally, clean up your AWS resources and explore further learning opportunities.
Removing Deployed Applications and Further Learning Opportunities
- Remove Resources: Run sam delete to remove all deployed resources.
- Further Learning: Explore additional topics like optimizing Lambda performance, integrating with other AWS services, or deploying a multi-function serverless application.
References
Building Lambda functions with Java
Re-platforming Java applications using the updated AWS Serverless Java Container