-
Notifications
You must be signed in to change notification settings - Fork 3
Spring Boot on AWS Lambda: From Traditional to Serverless
sankeerthbakki edited this page Mar 22, 2025
·
4 revisions
graph LR
Client -->|HTTP Request| Tomcat
Tomcat -->|Route Request| DispatcherServlet
DispatcherServlet -->|Map to Controller| RestController
RestController -->|Call Business Logic| Service
Service -->|Read/Write Data| Database
- Client sends HTTP request: The client (e.g., browser, mobile app) sends a request to the Spring Boot application.
- Tomcat receives request: The embedded Tomcat server listens for incoming requests.
-
DispatcherServlet routes request: Spring’s
DispatcherServletmaps the request to the appropriate@RestControllerbased on the URL. -
Controller processes request: The
@RestControllerhandles the request, extracts data, and calls the@Servicelayer. -
Service executes business logic: The
@Servicelayer contains the core logic (e.g., database operations, validations). - Response flows back: The response is returned to the client through the same chain.
Key Characteristics:
- Runs 24/7 on Tomcat.
- Uses
@RestControllerfor endpoint mapping. - Scales manually (e.g., Kubernetes clusters).
graph LR
Client -->|HTTP Request| APIGateway
APIGateway -->|Trigger| Lambda
Lambda -->|Parse Event| Handler
Handler -->|Call Business Logic| Service
Service -->|Read/Write Data| Database
- Client sends HTTP request: The client sends a request to the API Gateway.
- API Gateway triggers Lambda: API Gateway validates the request and invokes the Lambda function.
-
Lambda Handler processes event: The
CreateRequestHandlerclass (Entry point: Implements RequestHandler<Input, Output>) parses theAPIGatewayProxyRequestEvent(contains headers, body, path parameters). -
Handler calls business logic: The handler invokes the appropriate
@Servicemethod. -
Service executes business logic: The
@Servicelayer performs the required operations (e.g., database queries). -
Handler formats response: The handler creates an
APIGatewayProxyResponseEventand returns it to API Gateway. - API Gateway sends response: The response is sent back to the client.
Key Characteristics:
- No persistent server – Lambda runs on-demand.
-
Handler class replaces
@RestControllerfor request processing. - Auto-scales with zero configuration.
The entry point for AWS Lambda.
public class CreateRequestHandler implements
RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
// Spring context initialized once per Lambda instance
private static final ApplicationContext context = SpringContext.getContext();
private static final RequestService service = context.getBean(RequestService.class);
@Override
public APIGatewayProxyResponseEvent handleRequest(
APIGatewayProxyRequestEvent event, // Contains request data
Context context // Lambda execution context
) {
// Step 1: Parse event
String requesterId = event.getPathParameters().get("requesterId");
RequestDTO dto = parseBody(event.getBody());
// Step 2: Call Spring-managed service
SaayamResponse<Request> response = service.createRequest(requesterId, dto);
// Step 3: Format response for API Gateway
return new APIGatewayProxyResponseEvent()
.withStatusCode(201)
.withBody(toJson(response));
}
}-
APIGatewayProxyRequestEvent: Contains HTTP request details:event.getBody(); // Request payload (JSON/XML) event.getHeaders(); // Headers (e.g., Authorization) event.getPathParameters();// URL path parameters event.getHttpMethod(); // GET, POST, etc.
-
APIGatewayProxyResponseEvent: Structures the HTTP response.
mvn clean package-
Go to AWS Lambda Console:
- Click "Create Function".
- Choose "Author from scratch".
- Set runtime to
Java 17(or your preferred version).
-
Upload JAR/ZIP:
- Upload the packaged JAR/ZIP file.
- Set the handler class:
com.example.CreateRequestHandler::handleRequest(or your custom handler class).
-
Set Environment Variables:
- Add database details or other configurations.
-
Configure API Gateway:
- Link API Gateway routes (e.g.,
POST /requests) to Lambda.
- Link API Gateway routes (e.g.,
| Aspect | AWS Lambda | Traditional Spring Boot |
|---|---|---|
| Server Management | No server (serverless) | Requires Tomcat server |
| Scaling | Automatic | Manual (e.g., Kubernetes) |
| Cost Model | Pay per request | Pay for server uptime |
| Execution Time Limit | 15 minutes max | Unlimited |
| Cold Starts | Yes (100ms–several seconds latency) | No |