Running Spring Boot on AWS Lambda lets you combine the power of Java microservices with the scalability and cost-efficiency of serverless computing. With features like Amazon API Gateway, Lambda SnapStart, and native Java support, Spring Boot is now more viable than ever in serverless architectures.
In this guide, you’ll learn:
- How to package Spring Boot as a serverless function
- How to deploy it on AWS Lambda
- Integrate with API Gateway for HTTP access
- Improve cold starts using SnapStart
- Best practices and limitations
Weโll use the base package:com.kscodes.springboot.containers

๐งฐ Prerequisites
Youโll need:
- AWS account
- AWS CLI installed and configured
- Java 17 or 21
- Maven or Gradle
- Docker (optional for local testing)
๐๏ธ Step 1: Create a Spring Boot App with AWS Lambda Adapter
Use AWS Serverless Java Container to adapt Spring Boot for Lambda.
pom.xml
Dependencies:
1 2 3 4 5 6 7 8 9 |
<dependency> <groupId>com.amazonaws.serverless</groupId> <artifactId>aws-serverless-java-container-springboot3</artifactId> <version>2.0.0</version> </dependency> |
Sample Controller:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.kscodes.springboot.containers; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class LambdaHelloController { @GetMapping("/") public String index() { return "Hello from AWS Lambda!"; } } |
๐ฆ Step 2: Create Lambda Handler
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.kscodes.springboot.containers; import com.amazonaws.serverless.exceptions.ContainerInitializationException; import com.amazonaws.serverless.proxy.model.AwsProxyRequest; import com.amazonaws.serverless.proxy.model.AwsProxyResponse; import com.amazonaws.serverless.proxy.spring.SpringBootProxyHandlerBuilder; import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.RequestHandler; public class LambdaHandler implements RequestHandler<AwsProxyRequest, AwsProxyResponse> { private static final SpringBootProxyHandlerBuilder<AwsProxyRequest> handlerBuilder = new SpringBootProxyHandlerBuilder<AwsProxyRequest>() .defaultProxy(); private static final var handler = handlerBuilder .asyncInit() .springBootApplication(ServerlessApplication.class) .buildAndInitialize(); @Override public AwsProxyResponse handleRequest(AwsProxyRequest input, Context context) { return handler.proxy(input, context); } } |
๐งช Step 3: Package for Lambda
Use Spring Boot thin JAR or maven-shade-plugin
to build a single Lambda-compatible JAR.
Build Command:
1 2 3 4 |
./mvnw clean package |
Youโll get a target/serverless-demo.jar
.
โ๏ธ Step 4: Create Lambda Function via AWS CLI
1 2 3 4 5 6 7 8 9 10 11 |
aws lambda create-function \ --function-name springboot-lambda \ --runtime java21 \ --handler com.kscodes.springboot.containers.LambdaHandler \ --memory-size 512 \ --timeout 15 \ --zip-file fileb://target/serverless-demo.jar \ --role arn:aws:iam::123456789012:role/lambda-exec-role |
Or deploy from S3:
1 2 3 4 5 6 7 |
aws s3 cp target/serverless-demo.jar s3://your-bucket/ aws lambda create-function \ --code S3Bucket=your-bucket,S3Key=serverless-demo.jar \ ... |
๐ Step 5: Expose with API Gateway
1 2 3 4 5 6 |
aws apigatewayv2 create-api \ --name springboot-api \ --protocol-type HTTP |
Create integration and route the Lambda handler to a URL endpoint.
Use AWS Console or sam
/terraform
for more flexibility.
๐ Optional: Enable SnapStart
To reduce cold starts for Java functions:
- Deploy function with Java 21.
- Enable SnapStart in AWS Console under Function โ Configuration โ SnapStart.
- Redeploy the published version.
SnapStart drastically reduces startup time for Spring Boot functions.
๐ Monitoring and Logs
- View logs in CloudWatch:
1 2 3 4 |
aws logs tail /aws/lambda/springboot-lambda --follow |
Monitor latency, cold starts, invocations, and errors in AWS Lambda metrics.
๐ก๏ธ Best Practices
Tip | Description |
---|---|
Use SnapStart | Reduces Spring Boot cold starts to milliseconds |
Keep JAR minimal | Remove unused dependencies, enable lazy init |
Limit memory/cpu usage | Helps reduce cost per invocation |
Use Lambda Powertools (Java) | Logging, tracing, metrics libraries from AWS |
Use CI/CD with SAM or Terraform | Automate packaging and deployment |
โ๏ธ When to Use Serverless for Spring Boot?
โ Good for:
- Event-driven microservices
- Low-traffic APIs
- Scheduled jobs (via EventBridge)
- Rapid POC deployment
๐ซ Not ideal for:
- Long-running processes
- Applications needing consistent low latency
- Apps with heavy startup loads (without SnapStart)
๐ง Summary
You just learned how to deploy a Spring Boot application as a serverless AWS Lambda function, configure API Gateway, reduce cold starts with SnapStart, and automate everything with AWS CLI.
This model provides a cost-effective, scalable, and devops-free solution for Spring Boot apps, especially for bursty workloads.